How to keep a rigid body from clipping through tilemap

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

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 _physics_process(_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)
:bust_in_silhouette: Reply From: MisterMano

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.