September 28, 2015

Improving Unity's physics engine PhysX to achieve higher accuracy

0 comments
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.

September 22, 2015

Random Show Episode 29

0 comments
A new episode of the Random Show with Kevin Rose (founder of Digg) and Tim Ferriss (author of The 4-Hour Workweek) is out! This is episode 29.


Lessons learned
  • Kevin Rose's new watch-blog-company, Hodinkee, has a "small" but engaged audience. They had 1.3 million user sessions in a month from users that have opened the app more than 200 times. By the way, the name is not Swedish for small watch, at least I've never heard of it and I've been speaking Swedish for more than thirty years. According to Google translate, the word "hodinke" is "watch" in Czech/Slovak. 

Recommendations
  • Kevin Rose recommended the book The Okinawa Program, even though he also said some parts of the book were rubbish, and he also recommended eating Germinated brown rice with seaweeds, sesame, and eggs. 
  • It was difficult to hear, but I think Tim Ferriss recommended The Age of Miracles Animal Rescue if you want to adopt a dog.
  • Tim Ferriss recommended the podcast player app Overcast and playing Tetris. Why Tetris you might ask? The reason is that he interviewed Jane McGonigal who argues that playing Tetris a short time after a traumatic event will minimize the risk of post-traumatic stress. He also recommended the book Anything you want

Recommendations If you want to watch the rest of the episodes, you can find them here: The Random Show with Kevin Rose and Tim Ferriss.

September 13, 2015

Simulation of a forest fire in Unity

2 comments
This summer I decided to learn more about rockets and found an online course called Differential Equations in Action by Udacity. The first and second lessons in that course teaches you how to bring back the unfortunate Apollo 13 space ship from space to Earth. You will also learn about ABS brakes and how many people that how to be vaccinated to stop an outbreak of an epidemic. 
But sixth lesson is all about forest fires, and you will learn how to simulate a forest fire in Python. The differential equations include change in temperature from:
  • Heat diffusion
  • Heat loss
  • Wind speed
  • Combustion of wood      
I thought the simulation of a forest fire was really interesting, but it was boring to simulate it in Python. Wouldn't it be more interesting to see the forest burn in real time? Yes it would! So I decided to make a forest fire in Unity
It was really easy to translate the code from Python to Unity and make a real time simulation. The code in Python used a method called Euler forward to solve the differential equations, and Euler forward is working really well in Unity. This is the result:

The temperature at the start of the fire is about 700°C and the surrounding temperature is set to 37°C.

After about 2 minutes the fire has spread towards north-west, because the wind speed is north-west. The core temperature is now about 2500°C and the fire covers an area of  50*50 meters.

After 20 minutes the fire covers the entire area it can cover in the simulation. Notice that the forest that's not north-west of the fire has not ignited. The temperature at the edge is still between 100°C and 200°C, so you don't want to be there, but the wood has not ignited.

After 1 hour, 20 percent of the wood has gone up in flames. The trees change shape after a certain amount of wood has burned up. The core temperature is now 4000°C. 

Still smoking after 2 hours, but the core temperature has gone down to 2500°C, so the fire is dying.

After 5 hours, 70 percent of the wood has gone up in flames. The core temperature has gone down to 500°C.

The outer part of the forest fire that's not in the wind-direction has now stopped burning.

After 6 hours and 12 minutes, the last part of the forest has finally stopped burning. 

...or if you are more interested in a video (not the same fire as in the images)


Looks interesting? You can test it here: Fore Fire Simulator