kinematicbody2d - how to detect collision without moving?

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

good day, everybody. I’m trying to make a game similar to pong, and I need to animate the paddle everytime the ball collides to it.

this is the paddle code.

func _physics_process(delta):
position.x = clamp(position.x, 40, 140)
var collision = move_and_collide(velocity * delta)
if collision:

The problem with move_and_collide() is that it only plays the animation once it detect a collision WHILE moving. Is there anyway to set kinematicbody2d collision detection to true without moving? Or do I have to change the node type?

What is the Node you use for the ball?

p7f | 2019-01-23 18:16

Have you looked at the test_move() function? You can take a transformation, feed it into this function, and test whether the KinematicBody2D collides without moving anything.

Ertain | 2019-01-23 19:05

The paddle and the ball use kinematicbody2D

Kompaktive | 2019-01-23 23:21

I read the document and test_move() might be what I wanted, but I have no idea how to use it.

Kompaktive | 2019-01-23 23:26

:bust_in_silhouette: Reply From: Kompaktive

After a countless try, I finally figured it out.

var collision = move_and_collide(velocity * delta)
position.x = clamp(position.x, 40, 140)
if collision:
if test_move(transform, Vector2(1,0)):

I added another condition using test_move() and now the the paddle’s animation plays whenever the ball bounces on it whether it is moving or not. Thanks guys for the hint.

Great that you found a solution. I was going to suggest you to change paddle or ball to Area2D, or adding an Area2D as child of paddle. But whatever works for you is good! please, select your answer so others can see problem is solved!

p7f | 2019-01-24 11:26

You’re welcome, Kompaktive.

Ertain | 2019-01-24 16:38

@p7f I tried the first one: changing either one of them to Area2D doesn’t work. The animation plays, however the ball get pass through the paddle instead of colliding physically.

Haven’t tried the second one, it might have worked as well. Thanks!

Kompaktive | 2019-01-25 04:56