0 votes

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?

asked Mar 15 in Engine by Dupy (15 points)

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

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

1 Answer

0 votes
Best answer

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)
answered Mar 19 by njamster (8,132 points)
selected Mar 23 by Dupy

Thank you, that solve my problem !

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.