0 votes

Hi all,

I am making Pong.

When my ball hits a bat, it works perfectly, as long as the bat is not moving.

If the bat is moving though, the ball pushes the bat backwards.

I'm hoping someone can play the Beta of the game, see what I mean and offer some advice?

My Ball is a RidigBody2D with a CollissionShape2D as a child. As shown:
enter image description here

My Bat is a KinematicBody2D with a CollissionShape2D as a child. As shown:
enter image description here

The bat movement code is like so:

bat.move(Vector2(0,newPosition))

The game can be downloaded in Beta for:
Android
Mac
Windows

Note I haven't tested the Windows build.

TIP: To really see it in action, make one player a CPU player. This is because I have the CPU adding a bit of "spin" to the ball by moving quickly at the last second, just before the ball hits the bat. That allows you to see the problem amplified as the bat is moving so quickly at this point.

Any help would be GREATLY appreciated.

in Engine by (823 points)

2 Answers

+1 vote
Best answer

Without playing the game, I think your problem is like this
https://godotengine.org/qa/8820/disable-effecting-kinematic-collisions-forces-colliding?show=9031

Your moving body needs to be different from the ball hitting body if you want to use move

I put that again here...

Do this:

KinematicBody2D (+script, same mask as wall layer if you use it to limit movement)
|-Sprite
|-CollisionShape2D (if you use it to limit movement)
|-KinematicBody2D (same layer as ball mask)
         |-CollisionShape2D
by (7,834 points)
selected by

Thank you for your help (again).

That worked perfectly. It took me a bit of experimenting as it's a bit new to me but I now have one Kinematic controlling the collision with the wall (and handling movement) and another with the ball. No more ball pushing the bat. Great stuff, much appreciated.

+1 vote

It is because you move your bat with the move(<Vector2>) function which moves your body until colliding with something.
You could simple use set_pos(<Vector2>) to prevent this behavior.

by (702 points)

Thanks for the answer. I wanted to keep the move() function, so I experimented with the other answer and that worked. Appreciate your response though...

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.