Fast moving objects in game physics


I want to share a a tip here that could serve as an advice for those trying to make any game that have fast moving objects (like balls, bullets, etc).


Use. The. Freaking. Ray.Cast/Shape.


Seriously. You're going to get a much more stable experience when it comes to collision detection.

The first versions of this game showed a very buggy game play due to the ball going through the walls, shaking when trying to resolve collisions, etc.

After using raycasting family of techniques I manage to get a more reliable game play, exactly how I envisioned in my mind.

For Godot Engine (3.5 and also 4.x), it doesn't really work to set CCD (Continuos Collision Detection) as it is notoriously known for not working for quite some time. Also, using the Raycast2D node is not precise enough so I had to use `intersect_ray/intersect_shape` from the Physics server API to solve it in the definitive way.

Basically I used 3 ray functions for this game:

1) Raycast from the ball to any wall to avoid tunneling.
2) Rayshape for the player when dashing to avoid tunneling through the ball
3) Raycast from the player to the ball to resolve the collision normal and bounce the ball to the right direction (Rayshapes doesn't give any normal back)

Probably I could get away with a more simpler setup but for now, it's good enough.
That is executed every frame. In my machine I can get a stable 75 frames per second which is more than I need. :) Let's see how that scales when I add all 4 players to the scene though...

Files

ball-tech-demo-web.zip Play in browser
Version 57 Feb 21, 2024

Leave a comment

Log in with itch.io to leave a comment.