Adding Realism to Your Game Programming in C++
Making the motions of objects in your game realistic and believable is one of the keys to a good, well-designed game that others will enjoy playing. Fast moving cars that instantly turn a corner, weapons whose projectiles always hit the target no matter what, avatars that jump across wide chasms defying gravity — you’ve seen them and probably like me say something like, “Now that’s impossible!” And we’re right. An alien space ship flying a thousand miles and hour instantly does a ninety degree turn to come after you — oh no! The g-forces on the crew are gargantuan (if you don’t know that word, look it up).
Realism in gaming is all about following the known laws of motion, the physics at work in our universe, the universe we all know on a daily basis. Granted, the physics and math levels needed to fully understand and develop such real-world solutions can be very challenging, perhaps mind-boggling to those who have had little exposure to basic physics and calculus. However, in my book, Game Theory Programming in C++, I do all that work, giving you more or less canned implementations that you can tweak and use in your games to add higher levels of realism. Let’s look at what I’m talking about.
Suppose that we were driving down the Interstate at sixty miles per hour. We drive at this constant rate for an hour (thanks to cruise control). How far will we have gone? If you answer sixty miles, you are correct. That is one of the basic laws of motion at work.
Distance traveled = velocity times time
We can refine it a bit further. If we are initially at location X, then after that hour of driving, our new location is at X + 60 miles.
Notice here that I used the word velocity and not speed. What’s the difference? Speed is just a number, 60 mph. It does not have any direction associated with it. Where would we be, distance wise, if during that hour of driving, I drove around in a large circle, such as the Interstate loops around large cities? My final location could well be right back where I started from. A vector is both a speed and a direction, that is, we say it has a magnitude plus a direction. Physics of motion is heavily involved with vectors.
So our first key equation gives us our new position:
new position = original position + amount of time * (velocity)
Newton’s Third Law of Motion is: For every action there is an equal, but opposite reaction. For every force acting on a body, there is an equal and opposite reacting force. Suppose you are standing in line to buy a ticket for a show and someone comes up behind you and gives your back a push (force). Which way does your body tend to go? Forward. Suppose someone on your right gives you a shove (force), your body tends to go to the left. Now suppose you are simultaneously shoved both from behind and from the right? You tend to go off at an angle, to the left and forward. Vectors at work here.
This vitally important relationship is summed up as: (* indicates multiplication)
Force = Mass * Acceleration
though it is usually written as: F = m * a.
Of course, the acceleration is a vector, a magnitude with a direction, as is the force being applied. But just what is this acceleration anyway? It is the rate of change of velocity over time, enter calculus. It is usually written this way.
a = dv / dt, where dv is the change in velocity and dt is the interval of time.
For example, your car is going sixty miles per hour and you run into a brick wall. Your velocity goes to zero during a duration of let’s say a second. What’s the result? The acceleration on your body is terrific! Splat! Forget about air bags this time.
So where does all this get us? To create realism in an object’s motion, we must take into account all of the forces acting on the object, such as friction, air resistance, and so on. This gives us one of the fundamental principles:
Principle 6: The resultant force is the vector sum of each individual force acting on the object.
This is written: m * a = sum of all F
or rewriting it:
a = sum of all F / m
Okay, okay, so where does this get us? How does this help us to make realistic motions of our game objects? We resort to a calculus principle. To find the overall result, we integrate the equations of motion over time. Huh? It’s okay if you haven’t had calculus. I take care of the details for you. The principle is simple. Take the overall time span and divide it into tiny, infinitesimal units, called delta t. For each tiny interval of time, we know or can estimate the total forces acting upon an object at that point. Given the forces, we can find the resulting acceleration: a = sum of all F / m. Now knowing the acceleration during that infinitesimal bit of time, we can then find the velocity and distance traveled during that small period of time from: a = dv / dt, rewritten as dv = a * dt
From dv, we get the new position: new position = original position + dt * dv
This is saying that we can accurately plot the path that an object will take through time, viola realistic motion!
Just how small does this infinitesimal unit of time need to be? Ah, this is an adjustable parameter. If you are doing a fast action type of game, then the time interval might be quite small. The smaller, the more accurate the simulation, but the more calculations needed to determine the series of successive locations for the object and thus the more calculation time needed, which can slow down the entire game. One adjusts the actual amount of time to meet the needs of playability of the game.
Keeping it simple, the overall process is then as follows:
1. Calculate the object’s mass properties.
2. Find and codify all forces that are acting upon the object at this instant in time.
3. Vector sum all forces.
4. Solve the equation of motion for acceleration.
5. Integrate with respect to our small unit of time to find the velocity.
6. Integrate with respect to our small unit of time to find the new position.
7. Check for and handle any collisions, if this has meaning in your game.
8. Display or use the new position in some manner.
In my book, Game Theory Programming in C++, I handle the physics and math for a wide range of objects and situations, both in 2-d and in 3-d simulations. Yes, the physics and math can get pretty involved, but I’m taking care of those details for you, presenting you with easy to use, complete coding that you can cannibalize and use in your own games.
Above all, have fun developing your game.