About precision with _on_body_enter/exit

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Frog
:warning: Old Version Published before Godot 3 was released.

I’ve been messing around with godot, getting the hang of how things work. Got the pong example to work and I’ve been playing with it. I want to get some sort of hit detection going so I got the _on_body_enter/exit signals working but they seem to be really imprecise. Pics related

Is there a way to get that more precise?

body enter : https://ibb.co/gdcm5k
body exit : https://ibb.co/iKo4rQ
main window : https://ibb.co/g1XxBQ

Not sure that would fix your issue, but make sure not to scale the CollisionShapes. Currently in your screenshot the CollisionShape for testArea is scaled on the X axis (albeit only by a very small ratio, so may not be too bad, but as a rule of thumb you should keep it as 1:1).

Maybe share a zip of your project so that we can help debug it?

Akien | 2017-04-28 06:50

I thought only the capsule and circle collision shapes had to have a 1:1 scaling factor.

Frog | 2017-04-28 19:12

:bust_in_silhouette: Reply From: Nathan Lovato

That looks normal to me:
1- Body enter - If 2 collision shapes intersect, there’s a collision. In your case, it’s expressed as “entering” the body.
2- Body exit - If 2 collision shapes don’t intersect, then there’s no collision. If there was a collision on the last frame and there’s none on the current one, you exit the body.

When you move an object, it will basically move once per frame. With a speed of, say, 1800px/s, this means the sprite will jump in roughly 60 pixels increments on each frame.
That’s where the feel of imprecision comes from. In slow or simple games, it often doesn’t matter. To solve that though, you can:
1- Break down the movement on each frame and check for collisions on every step. Not recommended as this can make it harder to debug the game + this is costly in terms of performances.
2- Project paths of motion and see if they intersect. You’d use the Raycast2d node for that, as it’s designed for that very purpose.

What do you intend to use the area for? It’s only meant to detect if the player enters a certain area, not to make it collide and stop or bounce.

It’s mostly to get the hang of how things work for now but ultimately I want to compute the angle the ball bounces off of the paddles relative to where it hits. I was thinking of having some sort of power ups down the line as well.

I got something kind of working but kind of not at the same time using a triangle collision shape on the paddles with the physic engine. Problems were that the ball would get inside the paddles on the edges and I wasn’t satisfied with the angle anyway.

Frog | 2017-04-28 19:21

if ball goes too fast you may want to add extra elements for collision prediction or tunneling corrections, may be better to use a RigidBody with CCD and other bodies instead of areas in that case.

eons | 2017-04-29 02:00

What is a CCD?

Frog | 2017-04-29 05:09

CCDs are modes you can set on the RigidBody nodes to change the way they detect collisions. From the docs:

CCD_MODE_CAST_RAY = 1 — Enables continuous collision detection by raycasting. It is faster than shapecasting, but less precise.

You don’t want to use Area2d to work with motion and bounces. Area2d are for static areas. On the other hand, if you detect the collision with a staticbody, godot will place your ball the right way on the paddle when you use the move() method, and you’ll get a collision normal, allowing you to calculate the bounce.
Or you can work with rigidbodies.

By the way if this answers your initial question, don’t forget to set this to answered! Thanks.

Nathan Lovato | 2017-04-29 05:26

I use moving areas all the time but for slow things and predictable movements (so the area could be as large as the speed).

CCD uses techniques to prevent tunneling, is something available on most engines, but it can be done by hand too.

Using a RigidBody you can use a custom integrator to get the body state and give it your own physics reactions, like set/reset speeds on different situations.

eons | 2017-04-29 15:56