Unity is a popular game engine with a built-in physics engine called PhysX. Like all other physics engines, PhysX uses numerical integration techniques to simulate real world physics. Force and movement calculations can get pretty complicated, so in most cases it will be impossible to calculate the exact movements. This is the reason why the physics engine uses numerical integration techniques to approximately integrate the equations of movements.

The problem with several of the available numerical integration techniques is that the result in not always 100 percent accurate, because the game also has to run fast on your computer. Most game engines prefer less accuracy, but more faster games because the player will not notice the difference anyway. But what if you are going to make a game that requires more accurate movements, like a sniper rifle.

Low accuracy is a problem I had a few days ago when I simulated bullet trajectories. First I calculated the angle to hit the target, then I fired a bullet with Unity's physics engine, and then I noticed that the bullet didn't hit the target. First I thought I had made an error in the calculation of the angle, but then I realized that the reason the bullet didn't hit the target was because of limitations of the physics engine.

So which integration method is PhysX using? The answer (according to my research on the Internet) is that no one really knows for sure. So let's make an experiment to find out! The first integration method I used was Euler Forward, and you can see the result here:

You can see that the trajectory line when using Euler Forward is overshooting the target and is not following the same path as the bullet, which is using PhysX. So let's try Backward Euler:

You can see that the trajectory line undershoots the target, but follows the same path as the bullet would have (if you compare with the bullet in the image that uses the Euler Forward to calculate the trajectory line). So there's a chance that PhysX is using Backward Euler to calculate all physics. But we are still not hitting the target. So let's try Heun's Method:

You can see that we now hit the target with the trajectory line. But to improve the bullet trajectories, you have to write your own physics engine so that the bullets are also using Heun's Method instead of Backward Euler (or whatever method PhysX is actually using). If you would like to learn how, I've written a tutorial: How to make realistic bullets in Unity.

## No comments:

## Post a Comment