Posted December 05, 2021 by mattflat
#Unity #physics #collsion
If you have used the physics of Unity, you might have noticed that it has some problems to correctly calculate the collision of rotating objects (especially if the pivot of the rotation is quite far away, e. g. a pendulum). I realized this when I tried to make a tiny platformer in which the only thing you can do is rotating the whole level. The objective was to move a ball towards a target area, but it was frequently flying through the (rotating) level elements.
This made the simple game idea a bit harder to solve. A not so obvious workaround was to not rotate the level. Instead, everything just stays in place, a virtual reference frame with a virtual rotation is defined and you have to fake everything else. For example, the background, the camera und the direction of gravity rotate with the virtual reference frame.
But that's not all. You also have to apply forces to the ball the create the illusion that it is moving in a rotating and accelerated reference frame. In physics, these are called ficticious forces, hence the name of the game. 3 ficticious forces are relevant in this case:
With all these forces correctly calculated (just stick to the formulas from a physics textbook), the illusion is complete and you can play a game with correct collisions of seemingly rotating objects (that are, in reality, not rotating at all). As long as the rotation speed is not to high, the simulation within the game is quite correct (but there are still some numerical errors; I assume that the finite length of a simulation step is the reason for this).
You can try it here in your browser: https://mattflat.itch.io/ff
Most levels show the "faked" illusion, but levels 3 and 4 show how confusing the reality in the coordinate system of Unity is. This might help to understand what is really going on to cheat Unity's physics in this 5 minute game.