0 votes

I'm making a simple Ping Pong game controlled by the mouse. I've got all the code worked out for allowing mouse movement and collisions with objects but when I squish the ball into a wall it either clips through the player or the wall.

my code so far

extends KinematicBody2D

export (int, 0, 200) var inertia = 100

var velocity = Vector2.ZERO

func physicsprocess(_delta: float) -> void:

var direction: Vector2 = position - get_global_mouse_position()

move_and_slide(-direction*10, Vector2.UP, false, 4, PI/4, false)

velocity = move_and_slide(-direction*10)

for index in get_slide_count():
    var collision = get_slide_collision(index)
    if collision.collider.is_in_group("Ball"):
        collision.collider.apply_central_impulse(-collision.normal * velocity.length() * inertia)
Godot version 3.3.4
in Engine by (14 points)

1 Answer

0 votes

I'm going to assume that your paddle is a rectangle shape collision and that this squishing is happening if you move up or down while the center of the ball is within the length of the paddle.

One solution would be to use a capsule shape collision for the paddle, so the rounded corners would make it extremely unlikely for a squish to happen.

Another solution would be to make the ball "heavier" than the paddle, that is, if the paddle could move freely, it wouldn't be able to budge the ball. In other words, if the ball is in the way of the Y movement, it's the paddle that no longer moves.

by (292 points)
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.