January 19, 2015

How to design realistic high-speed bullets in Unity

Let's say you have made a target in Unity and now you want to fire a bullet against that target.

You have two options to choose from. The first option is to completely ignore gravity and fire a ray against the target and see if it hits (You can maybe calculate the theoretical time it takes for the bullet to hit the target before you fire the ray). This will work fine if you're not firing at distant targets because gravity will not make a large difference over small distances. But ignoring gravity is pretty lame.

When I first faced this problem I thought I had to calculate the theoretical bullet trajectory curve (after going down to the basement and bring up my old books on mechanics and physics). Then I realized that I should use Unity's built-in physics engine.

But there is always a but. When you fire a high-speed bullet in Unity (using the physics engine) you will notice that it will not always hit the target. The reason is that a game is similar to a movie. A movie consists of a number of images per second, and it is the same with a game. So your bullet will not fly continuously like in the real world, because it will jump between each frame to its next position. It will look like this (each white ball is the bullet's position each frame):

But solving this problem is easy. What you need to do is to fire a ray (1 frame before the last bullet's position) against the last bullet position and see if it hits anything. So this system will be delayed by 1 frame, but no-one will notice that because there are at least 30 of them per second. 

If you want to be even more accurate you can interpolate between these positions and add more bullet positions. I believe the easiest method here is using De Casteljau's algorithm. The result will look like this:

If you want the trajectory curve to be even more smooth you can use De Casteljau's algorithm again and again and again...

Looks interesting? You can test it here.

December 31, 2014

Books I've read in 2014

It's New Year's Eve! One thing you have to do on New Year's Eve is to change your footer to 2015, and another thing is to try to sum up your year. This is the books I've read in 2014. Luckily, I've saved all of them in my Goodreads account, so it was easier than usual to remember the books. 

  1. The Art of Game Design: A Book of Lenses
  2. The Engineer: Follow Elon Musk on a journey from South Africa to Mars
  3. The Obstacle Is the Way: The Timeless Art of Turning Trials into Triumph
  4. The Rise of Superman: Decoding the Science of Ultimate Human Performance
  5. Zero to One: Notes on Startups, or How to Build the Future
  6. Daily Rituals: How Artists Work
  7. Stjärnklart
  8. Skunk Works: A Personal Memoir of My Years at Lockheed
  9. Ordets makt och vanmakt - mitt skrivande liv
  10. The Animator's Survival Kit: A Manual of Methods, Principles, and Formulas for Classical, Computer, Games, Stop Motion and Internet Animators
  11. Det Nya Afrika
  12. No Surrender: My Thirty-Year War
  13. The Immortal Game: A History of Chess, or How 32 Carved Pieces on a Board Illuminated Our Understanding of War, Art, Science and the Human Brain
  14. Full Catastrophe Living: Using the Wisdom of Your Body and Mind to Face Stress, Pain, and Illness
  15. The Code Book: The Science of Secrecy from Ancient Egypt to Quantum Cryptography
  16. Ultimate Sales Machine
  17. Growth Hacker Marketing: A Primer on the Future of PR, Marketing, and Advertising
  18. Från Holmes till Sherlock
  19. How to Fail at Almost Everything and Still Win Big : Kind of the Story of My Life
  20. Predictably Irrational: The Hidden Forces That Shape Our Decisions
  21. SuperFreakonomics: Global Cooling, Patriotic Prostitutes And Why Suicide Bombers Should Buy Life Insurance
  22. Think Like a Freak
  23. Savage Continent: Europe in the Aftermath of World War II
  24. Big Data: A Revolution That Will Transform How We Live, Work, and Think

December 28, 2014

Tesla Motors Simulator Update - Model X


The first car made by Tesla Motors was the Roadster (right), the second car was the Model S (middle), and the third car will be Model X (left). That car is not yet ready for delivery (I think they have some problems with the doors) so you have to wait unity 2015/2016 before you can drive one - until today when I added the Model X to my Tesla Simulator.

Model X might be a big car (7 people can sit in it), but it was not that difficult to make a 3D-model in Blender. One reason was that I didn't have to make any rear-view mirrors because Model X has cameras instead of traditional mirrors. 

Also, the Model X and the Model S are almost the same car, so I could reuse the wheels and I could recall some of the design features from the time when I made the Model S.

Another thing I learned was to improve the performance and looks of the lightning while night-driving. I have previously used a "real" light source, but I noticed that most games, including Grand Theft Auto, use a texture.

Looks interesting? You can test it here

December 22, 2014

Tesla Motors Simulator Update - Model S Textures

There's a saying in the 3D modeling industry: "Mesh is only one letter away from Mess!" A mesh is the name of the 3D model and they tend to become a bit messy as you add more and more details. Also, if you are creating a 3D model for a game, you can't add too many details or the game will run really slow. So in the end you will end up with a mesh that's both messy and boxy. Luckily, you can hide most flaws with textures. 

To learn both Blender and Unity, I decided to develop a simulator where you can drive electric cars from Tesla Motors. I have experience from modeling vehicles in 3D, but not so much experience from painting textures for these models. This weekend I decided to finally learn how to add textures. This is the result:

And this is the result in Blender:

Looks interesting? You can test it here.

December 10, 2014

Tesla Motors Simulator Update - Roadster

The car company Tesla Motors was founded in 2003 with the vision to build hundreds of thousands of electric cars. But the problem with that vision was that it is super-expensive to build so many cars, so they couldn't afford and neither had they the experience they would need. To solve this problem they decided to begin with a few (but expensive) electric cars. The result was the Roadster, which I've now added to my Tesla Motors Simulator. (The model is not 100 percent complete yet so I will improve it in the future).

I've also noticed that it's easy to get stuck when you are driving either the Roadster or the Model S, so I added a hovering function. I believe the co-founder of Tesla Motors, Elon Musk, believes that we in the future will have flying cars.

While experimenting I also tested to make a monster truck version: