February 11, 2019

Tesla Motors Simulator - 2019 update 1

0 comments
New year and new Tesla Motors Simulator updates are rolling out. In the last update I managed to introduce a bug where the car continued to accelerate for some short time even though you didn't press the gas pedal button. This happened at higher speeds and you felt it most when driving the Roadster 2020. I fixed this bug in this update with maybe 10 lines of code, but it introduced some other inaccuracies, so I had to change the parameters in all cars to make them have the same performance as the real cars.

In the future I might build my own "wheel collider" to have more control over it. I'm currently using Unity's built-in and it's working like a black box where you put in some values and then some values comes out and no-one really know what's going on inside of the box.


In the last update I removed the destruction system that deforms the car when colliding with something. The reason was that I'm using a simplified destruction system and the destruction is sometimes very inaccurate looking from what it would have looked like in real life. But people were sad about it, so I decided to implement it again. I also added dramatic sparks to make it look cooler when colliding. Other games, like BeamNG have more realistic destruction system, which I believe is called soft-body physics. But realism means slower performance and the destruction system I'm using is fast. But I might implement a better one in the future.


The third thing I added was a simple container trailer that the Semi can drag around. The trailer is still a little buggy here and there, sometimes it flips when colliding with some small object, but it's working overall like a real trailer would. I also realized I could take this trailer and add it to my pathfinding algorithm for self-driving cars, so it becomes self-driving trucks!

February 7, 2019

Steering Behaviors - or how not to evacuate a building

0 comments
One good book I read is The Unthinkable: Who Survives When Disaster Strikes - and Why. It discusses an aircraft accident where many people died even though the computer simulations said they should have survived. What went wrong? The answer is that the computer simulations assumed people move in the same way as water.
For a long time, engineers assumed people would move out of a building like water. They would fill the space they had, coursing down the staircases and flowing out to safety like a river of humanity. Buildings were constructed accordingly. The problem is, people don't move like water.

So if you are trying to model real people, you have to take pain and fear into consideration. People make decisions, they stumble and fall, and they fill up space unevenly. On their way out, they pause to rest when they can. But if you are just trying to make a simple game, you can treat people like water. Treating people like water in games is also known as Steering Behaviors. These individual behaviors are supposed to work like Lego bricks - you can combine several of them to get the final character behavior you need.

Let's say you have a character that's following a path. It will work fine until the character is meeting another character moving in the opposite direction? One solution now might be to calculate a new path with some pathfinding method like A* (A star). But this will take some unnecessary computation power. A better way is to use these steering behaviors, which will make the character avoid the other character and then continue following its path.

The following shows a combination of the behaviors:
  • Seek - move along the flowfield direction
  • Solid obstacle avoidance - is in this case finding the closest point on the wall (the walls are just lines), and if the character is predicted to collide with the wall it should steer away from the closest point)
  • Moving obstacle avoidance - is in this case moving a character away from the character it's colliding with. So it's not a steering behavior but a commonly used cheat to avoid intersecting characters

While researching steering behaviors I learned that the most difficult of the behaviors is avoiding moving obstacles, so there are multiple algorithms trying to deal with the problem. 

Algorithm 1. The most simple obstacle avoidance behavior is trying to just avoid the most dangerous obstacle by predicting where it will be in the future, while ignoring all others.


Algorithm 2. Is trying to avoid all of the dangerous obstacles by predicting where they will be in the future, and then scale the prediction so that obstacles closer to our character are considered more dangerous. 


Algorithm 3. This one has a name and it's Power Law. It's similar to Algorithm 2, but is also calculating an energy. As you can see, the characters are finding their way through the congestion in a slightly more realistic fashion.


Algorithm 4. RVO. This is what Unity is using to make characters avoid each other on their navmesh. I never implemented this behavior but might do it in the future.

As you can see, some of the characters are intersecting with each other. This is a commonly used cheat and the idea is to avoid congestion, you shrink the radius of each character until it's not anymore stuck. This cheat was used in the game Planet Coaster (as explained in this article) and they sold millions of copies, so it should be allowed in your game as well!  

Adventures making a custom navmesh in Unity

1 comments
I was watching this talk by Oskar Stålberg, who is one of the creators behind a cool indie game called Bad North. In it he said he had fun making a custom "navmesh," and since I had no experience whatsoever from navmeshes, I decided to make my own navmesh. I've earlier got questions if my pathfinding-for-cars algorithm is working on Unity's navmesh (or any navmesh) and I've up until now said "I have no idea" because I wasn't sure how a navmesh is working.

What is a navmesh?
Once upon a time it was common to divide the area, where a character in a game can walk around, into a grid, like a chessboard. The problem with this method is that if you have a big map, you will get a large chessboard, making pathfinding very slow. A better way (in some cases) is to make a navigation mesh (navmesh). You do this by dividing the area into convex shapes, such as triangles, meaning that you will hopefully end up with fewer triangles than cells in the chessboard. This will make pathfinding more efficient because the pathfinding algorithm has to investigate fewer nodes.

How can you learn how to make a navmesh?
To learn how to make a navmesh, I googled "Unity navmesh tutorial." But all tutorials I found were about how Unity's own navmesh system is working - not how to make your own custom navmesh. But why are there almost no tutorials on navmeshes? The reason is not that finding your way around a navmesh is difficult - the reason is that it's difficult to create the shapes, like the triangles, that makes up the navmesh itself. So after much research, I learned that if you want to learn how a navmesh is working, you have to learn the following areas:
  1. How to create the navmesh itself
  2. How to find the shortest path on a navmesh
  3. How to make characters follow the shortest path

How to create the navmesh itself
A navmesh consists of connected convex shapes. The reason they have to be convex is that if you are standing inside of a convex shape, you can walk in a straight line to any other point in that shape. If the shape had been concave you would have to do some local pathfinding inside of the concave shape. These shapes are in many cases just triangles, but some merge these triangles into other convex shapes, such as rectangles, to make pathfinding more efficient.

The problem is: how can you create these connected triangles from the game level you have? You might have these houses and you want some character to find the shortest path between the houses. I believe the answer to that question is Constrained Delaunay Triangulation, which is a big topic, and I've written a tutorial on it here: Constrained Delaunay Triangulation. So now you have something that looks like this:


How to find the shortest path on a navmesh
You have these connected triangles, and now you want to find the shortest path from one triangle to another triangle. To do that you connect the center of each triangle with the center of each neighbor, and then you just run some pathfinding algorithm, like A* (A star), on that graph. When the character is moving between each triangle, it should not move to the center of the next triangle, but the closest point on the edge shared by the current triangle and the next triangle.


The shortest path on a navmesh can be simplified by using a line-of-sight algorithm - the character doesn't have to navigate between the triangles if it can move in a straight line to the goal triangle. But how can you detect if a character can see the goal point it wants to reach? The answer is by using a line-line intersection algorithm. If you draw a straight line from the character to the position it wants to reach, and if this line is not intersecting with any of the triangles edges that are obstacle (has no neighbor), then the character can move in a straight line to the position it wants to reach.

How to make characters follow the shortest path
Now you know which triangles the character has to pass to reach the goal destination. To make it reach the destination, you should learn a set of algorithms called Steering Behaviors. This is what Unity is using to make the characters find their way around their navmesh system. For example, they are using an algorithm called RVO to prevent characters from colliding with each other.


More than pathfinding
Navmeshes are useful for other areas than just pathfinding for characters. While researching navmeshes I read that you can use them to simulate flow. When simulating flow in games you move a texture by using a color and the color determines in which direction the texture should move. For example, if the color is red, then the texture moves in a certain direction. This makes it look like the water is flowing. This colored texture is called flowmap, but they are almost impossible to paint by hand, and what if the flow is suddenly changing direction while the game is running? With a navmesh, you can create dynamic flowmaps.

To make this work you should connect the vertices of each triangle in the navmesh, and then run a flowfield algorithm to find the shortest path from each node to the destination in the triangle you want to reach. As said before, you can use the line-of-sight algorithm to detect if a node can see the goal position. Each node will now get a direction and this direction is used to set the vertex color of the mesh. You can access this vertex color in a shader and thus get the flow direction. Moving the texture in the direction of the flow is kinda tricky and this is a good article on the subject.

This is the dynamic flowmap that determines the direction of flow:


...and then you can simulate something like smoke on the navmesh:


...or water:


Read more
This was just a short introduction, so if you want to learn more about navmeshes, you should read:

February 6, 2019

The art of fake volumetrics - or how to make fluffy red pandas

0 comments
Most objects in games tend to be kinda flat. The reason is that all objects in games consists of triangles and each triangle takes some time to display on the screen, so you should use as few of them as possible. But what if you need to display something that's not a flat surface, such as hair or smoke? Then you need to use a trick, which I don't think has a name, but we can call it fake volumetrics (I've heard the name "quad stacks" or "shells"). This is how it works:

Let's say you want to make a fluffy red panda:


This panda is flat, while real red pandas tend to be fluffy. To make it less flat you need to fake fur because you cant make each fur straw because it's way too complicated for games. You do this by rendering the panda multiple times, and each time you grow the size of the panda by making the mesh larger along the normals of each triangle. This is done step-by-step around 10 times. Each step you also change the transparency of the fur, so fur closer to the body is less transparent than the fur furthest away from the body. You now get a fluffy panda:


To avoid getting fur in the eyes and the nose you can simply use a texture telling the algorithm not to grow the triangles around the eyes and the nose.

With the same technique, you can make fake volumetric smoke/fog. You begin with a simple plane at the bottom, and then you add multiple planes on the top of the original planes while changing the transparency the higher up you are. And you end up with this:


If you need a better explanation of how to make fake volumetric smoke, you can watch this YouTube video: UE4 VFX For Games: Brute Force Volumetric Gases - Ground Fog & Vapor. The problem with this technique is that if you zoom in really close, you can see the individual fog planes:


To avoid this you can either add more planes, or make sure the player is staying far enough away from the smoke!

February 2, 2019

Lessons learned from the Paradox podcast - Season 4

0 comments
Paradox Interactive is a company that's both developing and publishing games. In their podcast they talk about the business of video games, and if you want to listen to it on your own, you should search for "The Paradox Podcast" on iTunes or wherever you can find popular podcasts. I've earlier summarized season 1season 2, season 3, and this is season 4:

S04E01
  • The successful game RimWorld is a perfect "paradox game" they never published/developed because RimWorld is a combination of the games Factorial (manufacturing), Don't Starve (survival), and The Sims (social). 
  • Paradox has acquired the game Prison Architect, which is a game first released in 2015. They acquired it from the company that developed it, so they didn't acquire the developer itself. Prison Architect is a good game because it's a sandbox where you can create your own stories, so Paradox believes they can come up with new content in a similar way as Microsoft is still making money from Minecraft by updating it. Prison Architect is obviously not as big as Minecraft, but both games are similar to each other. Paradox also plans to develop other "Architect" games, such as Ski resort Architect. 
  • Paradox has an idea that to provide free updates to an existing game, they can sell paid updates (DLCs) to game and that will also pay for some free updates to the same game. A seven year old Paradox game is still getting free updates because some people pay for the paid updates. 
  • There's currently a war between developers if they should put their game on the Steam store or on the Epic store. A better idea is to focus on making a good game and put it on a store, and competition between stores is good.
S04E02
  • Farming Simulator might sound like a stupid game idea - who would want to spend time drive around in a tractor? But it turns out people like to farm, the company behind the game announced they will turn farming into an e-sport, so what you think is boring can be fun if you make the boring parts fun to play.
  • The quality of games released every year is increasing because people can now go to school and learn game development, which is not something you could some years ago. But releasing a good-looking game with no bugs is no longer enough - it also has to be fun to play. And you also need to figure out a way to market the game, which is what Paradox can do for you. 
S04E03
  • Paradox has recruited Rod Humble to lead a new Paradox studio in US called Tectonic. He has made 200 games (including Sims 3) and has 30 years of experience. 
  • Rod Humble believes in "The Valve style" when making games. Valve is a successful game developer and they believe in: 
    • Small groups
    • Each group members should be specialized in one area while still having a broad range of skills
    • Small amount of documentation (1 page is enough)
    • Moving fast by prototyping ideas without first having to ask if you are allowed to do it and then some manager has to make that decision one month from now 
  • Game developers make art - they sell a fantasy, telling a dream. A game is not like toothpaste which you actually need. 
S04E04
  • An interview with Wendy Young and her experience of working through the ups and downs in the game industry. 
S04E05
  • How can you reach a new type of audience that plays another type of game than the audience you already have? Just because you make a game that attracts that type of audience does't mean they will automatically be aware of your game. What Paradox did to make the new audience aware of the new game was to launch a dating app which is an odd thing to do if you are a game developer. But it would make the new audience be aware of the game. 
  • To measure if your marketing idea was successful, you measure of many people found what you wanted them to find. For example, how many watched the trailer, how many media sites wrote about the game, how many signed up for the newsletter, how many pre-ordered the game? But it's still difficult to really measure if it was a success if you have nothing to compare the numbers against.     

This article will be updated as they release new episodes!