How make a pong paddle pushed by ball

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

Hello everyone !
I make my first step in 3D game by making a pong with a little twist : player can rotate her paddle thank to left and rigth key, to give additional velocity when hitting the ball, and the ball can rotate a paddle by pushing the paddle on the impact point.

I was thinking of doing it by creating a rigid body for the paddles, and a kinematicBody for the ball. When the ball hits a paddle, in its code, I look to see if the object that collided is a rigidBody, and if so, I apply an impulse on the collision point with the normal racquet normal * the speed of the ball.

But the racket doesn’t move.

I tried to move a rigidBody with a kinematic on another project (a platform game where a player can move a ball), and it works, the rigidBody move.

Do you have an idea?

Can you provide the relevant portion of your code? Is the collision detected correctly?

njamster | 2020-03-19 15:51

The collision is detected yes.

func _physics_process(delta: float) -> void:
	var collision = move_and_collide(velocity * delta, false)

	if collision:
		if collision.collider is RigidBody:
			collision.collider.apply_impulse(collision.position, -collision.normal * 100)
			velocity = velocity.bounce(collision.normal)
		else:
			velocity = velocity.bounce(collision.normal)

Rigidbody is the paddle

Dupy | 2020-03-19 16:39

:bust_in_silhouette: Reply From: njamster

You’re using the position-information of a KinematicCollision2D:

The point of collision, in global coordinates.

However, you use it as the offset-vector for apply_impulse:

The position uses the rotation of the global coordinate system, but is centered at the object’s origin.

So you need to calculate the offset yourself:

var offset = collision.position - collision.collider.global_position
collision.collider.apply_impulse(offset, -collision.normal * 100)

Thank you, that solve my problem !

Dupy | 2020-03-23 10:57