Ideal frames per second

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

I was messing around with high speed bullets (not high enough to be raycasts) and collision detection is a bit off. They are always registered, but sometimes at the wrong place in the object (say I shoot a wall, the collision position is set inside the wall instead of its border).

Upping the Physics fps to 240 kinda fixes this, but I was wondering if there’s any downside to setting the fps so high other than decreased performance? My game uses pixel graphics and a small amount of nodes so its real light.


:bust_in_silhouette: Reply From: Zylann

This is a common problem with game physics called “tunnelling”. At very high speed this can even cause projectiles to pass through targets. This happens especially if your bullets only “check” collision rather than actually simulating the hit. Upping FPS reduces the effect but isn’t a definitive solution.

What’s usually supposed to fix it is called CCD: Continuous Collision Detection, although it takes a performance hit and I believe it doesnt work well yet in Godot.

A workaround in case of fast moving bullets is to make them using raycasts. So instead of waiting for a collision hit, you perform a raycast in the direction of the bullet, on a distance equal to the amount travelled by the projectile during one frame. So each frame, you’ll be checking collisions along a series of connected segments, without gap, so there won’t be “tunnelling”.
If no hit is detected, move the projectile.
At some point, one raycast will hit the target. When that happens, instead of moving the projectile like before, place it directly on the hit position, and make it explode there.
This will not only make it work even at very fast speeds, but will also allow precise impact visuals.
It may require a few code changes.

Another way is to give the projectile an elongated collision shape, trailing behind it to be sure that you won’t pass through the target, but you’d need to make your projectile a rigidbody or a kinematic with move_and_collide so that it simulates the hit (rather than just checking if the shape overlaps with another). I never tried this approach in Godot though.