How to code a snooker pool collision between balls on the table ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Aljabri-Salman

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.?

:bust_in_silhouette: Reply From: im_not_a_robot

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.

Indeed that was easy. Thanks

Aljabri-Salman | 2020-06-10 19:39