+1 vote

I am new to godot and have been trying to practice by creating something similar to snooker game. I was able to code the collision between the cue ball and the walls but I am not able to do collisions between other target balls. Basically how would you do the collision for snooker game?
my code so far for the cue ball:

extends KinematicBody2D

var velocity = Vector2()
var collision
func _ready() -> void:
    pass # Replace with function body.

func _physics_process(delta: float) -> void:
    velocity.x = lerp(velocity.x, 0.0, 0.1)
    velocity.y = lerp(velocity.y, 0.0, .1)
    if collision:
        velocity = velocity.bounce(collision.normal)
    collision = move_and_collide(velocity)

func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton and event.pressed:
        velocity.x = 70
        velocity.y = -60
        collision = move_and_collide(velocity)

How to calculate the bounce velocity if a ball is moving and the other is stationery and how to do if they both are moving.?

in Engine by (16 points)

1 Answer

+2 votes
Best answer

Sounds like you’re trying to reinvent the physics system. You will have a much easier time implementing the snooker balls as RigidBody2D and the walls as StaticBody2D -- the physics system will handle the complicated math.

Godot Physics Introduction

I hope this is what you need for your project.

by (54 points)
selected by

Indeed that was easy. Thanks

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.