September 28, 2015

Improving Unity's physics engine PhysX to achieve higher accuracy

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

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. 

  • 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

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

June 19, 2015

How to tell stories with data and what's the future of journalism?

Pulitzer-prize winning journalist and editor of the New York Times data journalism website The Upshot, David Leonhardt, shares the tricks of the master storyteller's trade. In conversation with Google News Lab data editor Simon Rogers, he shows how data is changing the world - and your part in the revolution.

Key points
  • Journalism is not in decline. Journalism (at least American journalism) is better today than it has ever been - even as little as 10 years ago. Yes there are challenges, and the business model is changing. But journalism is still keeping people informed about the world and has not been replaced by click-bait articles. 
  • Why journalism is better today than 10-20 years ago:
    • Journalism is more accurate than it used to be (but not perfectly accurate). One reason is that it is easier to change inaccurate information in articles when the articles are digital, like spelling errors, compared with printed articles. It is also easier for the audience to interact with articles and journalists today when everything is digital. The audience can improve the articles. 
    • The tools and techniques for telling a story has improved. It is today easy to create interactive visualizations, like maps and let the map zoom in on the area, and give the reader different information, depending on where the reader is living. These techniques didn't exist 20 years ago.
    • Journalists are using better data than before. As long as the journalists are using the data in the correct way, the result is better than it used to be. 
    • The audience for ambitious journalism is larger than it was just a few years ago. People from across the globe can read the New York Times. 
  • The most articles in the New York Times are not traditional articles with blocks of text - they are interactive visualizations, essays, Q&A's, and videos. But they are not click-baits - they are about serious topics and the people behind them have put a lot of effort into them. The smartest and clearest way to tell a story isn't anymore the traditional article.
  • New York Times is sometimes writing 2 articles, one traditional with just text and a similar article with more visualizations. In one example, the article with the more visualizations got 8 times the traffic compared with the traditional article with just text.
  • Journalists are becoming more and more specialized within a certain area.
  • You can probably find big opportunities within local news, but only if you are using data. 

June 18, 2015

How to make better predictions and decisions


I've read a book called The signal and the noise: Why so many predictions fail - but some don't by Nate Silver. The basic idea behind the book is that ever since Johannes Gutenberg invented the printing press, the information in the world has increased, making it more and more difficult to make good predictions because of the noise. Moreover, the Internet has increased the information overload, making it even harder to make good predictions. A lot of people are still making what they think are good predictions, even though they shouldn't make predictions at all (*cough* economists), because it is simply impossible to predict everything. 
What most people are doing when trying to predict something from the information available, like a stock price, is to pick out the parts they like while ignoring the parts they don't like. If the same person is trying to predict if he/she should keep a position in let's say Tesla Motors, then the person will read everything that confirms that it is a good idea to keep that position and hang out with people with the same ideas, while ignoring the facts that maybe Tesla Motors's stock is a bubble. 
You may first argue that only amateurs pick out the parts they like while ignoring the parts they don't like. But if you can't remember the 2008 stock market crash, The signal and the noise includes an entire chapter describing it. It turned out that those who worked in the rating agencies, whose job it was to measure risk in financial markets, also picked out the parts they liked, while ignoring the signs that there was a housing bubble. For example, the phrase "housing bubble" appeared in just eight news accounts in 2001, but jumped to 3447 references by 2005. And yet, the rating agencies say that they missed it.

Another example is the Japanese earthquake and following tsunami in 2011. The book includes an entire chapter on predicting earthquakes. It turns that it is impossible to predict when an earthquake will happen. What you can predict is that an earthquake will happen and with which magnitude it might have. The Fukushima nuclear reactor had been designed to handle a magnitude 8.6 earthquake, in part because the seismologists concluded that anything larger was impossible. Then came the 9.1 earthquake. 
The Credit Crisis of 2008 and the 2011 Japanese earthquake are not the only examples in the book:
It didn't matter whether the experts were making predictions about economics, domestic politics, or international affairs; their judgment was equally bad across the board.  
The reason why we humans are bad at making predictions is because we are humans. A newborn baby can recognize the basic pattern of a face because the evolution has taught it how. The problem is that these evolutionary instincts sometimes lead us to see patterns when there are none there. We are constantly finding patterns in random noise.

So how can you improve your predictions?
Nate Silver argues that we can never make perfectly objective predictions. They will always be tainted by our subjective point of view. But we can at least try to improve the way we make predictions. This is how you can do it:
  • Don't always listen to experts. You can listen to some experts, but make sure the expert can really predict what the expert is trying to predict. The octopus who predicted the World Cup is not an expert, and neither can you predict an earthquake. What you can predict is the weather, but the public is not trusting weather forecasts. This could sometimes be dangerous. Several people died from the Hurricane Katrina because they didn't trust the weather forecaster who said a hurricane was on its way. Another finding from the book is that weather forecasters on television tend to overestimate the probability of rain because people will be upset if they predict sun and then it is raining, even though the forecast from the computer predicts sunny weather.  
  • Incorporate ideas from different disciplines and regardless of their origin on the political spectrum.
  • Find a new approach, or pursue multiple approaches at the same time, if you aren't sure the original one is working. Making a lot of predictions is also the only way to get better at it.
  • Be willing to acknowledge mistakes in your predictions and accept the blame for them. Good predictions should always change if you find more information. But wild gyrations in your prediction from day to day is a bad sign, then you probably have a bad model or whatever you are predicting isn't predictable. 
  • See the universe as complicated, perhaps to the point of many fundamental problems being inherently unpredictable. If you make a prediction and it goes badly, you can never really be certain whether it was your fault or not, whether your model is flawed, or if you were just unlucky. 
  • Try to express you prediction as a probability by using Bayes's theorem. Weather forecasters are always using a probability to determine if it might rain the next week, "With a probability of 60 percent it will rain on Monday the next week," but they will not tell you that on television. The reason is that even though we have super-fast computers it is still impossible to find out the real answer, as explained in a chapter in the book. If you publish your findings, make sure to include this probability, because people have died when they have misinterpreted the probability. A weather station predicted that a river would rise with x +- y meters. Those who used the prediction though the river could rise with x meters, and it turned out the river rose with x+y meters, flooding the area.    
  • Rely more on observation than theory. All models are wrong because all models are simplifications of the universe. One bad simplification is overfitting your data, which is the act of mistaking noise for signal. But some models are useful as long as you test them in the real world rather than in the comfort of a statistical model. The goal of the predictive model is to capture as much signal as possible and as little noise as possible.  
  • Use the aggregate prediction. Quite a lot of evidence suggests that the aggregate prediction is often 15 to 20 percent more accurate than the individual prediction made by one person. But remember that this is not always true. An individual prediction can be better and the aggregate prediction might be bad because you can't predict whatever you are trying to predict. 
  • Combine computer predictions with your own intelligence. A visual inspection of a graphic showing the interaction between two variables is often a quicker and more reliable way to detect outliers in your data than a statistical test.

This sounds reasonable? So why are we seeing so many experts who are not really experts? According to the book, the more interviews that an expert had done with the press, the worse his/her predictions tended to be. The reason is that the experts who are really experts and are aware of the fact that they can't predict everything, tend to be boring on television. It is much funnier to invite someone who says that "the stock market will increase 40 percent this year" than someone who says "I don't know because it is impossible to predict the stock market."
So we all should learn how to make better predictions and learn which predictions we should trust. If we can, we might avoid another Credit Crisis, another 9/11, another Pearl Harbor, another Fukushima, and unnecessary deaths from another Hurricane Katrina.