20091228

Game Physics - Momentum & Friction

                One of the natural progressions in game development is going from a locked speed to a momentum based system.  Originally, you might have had code that made your ship move forward by 2 pixels every time someone pressed forward.  It’s easy, done in a line of code, and you can forget it.  But when it comes to upgrading your character, ship or vehicle, you are now faced with a lot of hard coded upgrades.  Instead of moving 2 pixels, now you move 4 pixels.  Etc… 

               
                I remember the idea of adding physics as one of those near-impossible tasks, primarily because I didn’t understand how it worked.  Below, is the basic idea.  To start with, I’ll commonly refer to the word “cycle”, which means that the game makes its decisions/changes, draws the frame, then moves on to the next.  Each game loop is a cycle. 


                During each cycle, I check if the forward key is pressed.  If it is, instead of moving the ships location by two pixels, I add 0.3 to a variable called momentum.  So if I went from still, to pressing the button, the first cycle would move the ship by 0.3 pixels.  During the second cycle, I add another 0.3 to the momentum, increasing it to point 0.6.  That means this ship is moved 0.3, then 0.6, and next it will move 0.9, then 1.2, 1.5, 1.8 etc…   This means that it smoothly increases.  Suppose you are in a car.  You want to be moving 35, but from 0, it doesn’t just happen the instant you touch the gas pedal.  The car gradually accelerates.  First moving only at 3 feet / second, then 6 feet / second, etc…  until you reach 35.


// Game Loop
{
                If UpArrow is Pressed, Momentum = Momentum + 0.3;
                Position = Position + Momentum;           
}


                At this point, in your game, it will increase in speed until you let your finger off of the forward button.  It will coast at that speed.  As with proof of concepts, this brings up another challenge, the ship keeps moving faster when you hold the button.  The speed gets unreasonable pretty quick.  What you need to do is add a top speed variable.  Let’s say TopSpeed = 2.  Every time before you add the momentum to the location, check if the momentum is greater than TopSpeed.  If it is, then set Momentum = TopSpeed.  This way the ship never moves more than 2 pixels per cycle.


//Settings
TopSpeed = 2;

//Game Loop
{
                If UpArrow is Pressed, Momentum = Momentum + 0.3;
                If Momentum > TopSpeed, Momentum = TopSpeed;
                Position = Position + Momentum;
}


                A quick recap so far, you can add a Force variable, and when you press to go forward, it adds that amount to the momentum.  This is now a variable you can set per vehicle, calling it torque, or how fast you accelerate.  Your TopSpeed variable, which is pretty obvious, which is another easily upgradable value for your individual vehicles. 


                So next, lets add Friction.  Friction isn’t necessarily something you need to add, but its excellent for
cars, boats, planes, etc… where you want the vehicle to gradually lose speed when the vehicle isn’t having the gas pedal pressed.  Essentially, you add create a variable called friction, and it’s value will be 1 meaning no friction, or 0 meaning completely blocked.   A good starting friction is probably 0.85.  At the beginning of every cycle, multiply your Momentum by your Friction.  Momentum * 1 = 100% Momentum.  Momentum * 0.85 = 85% Momentum. 


//Settings
TopSpeed = 2;
Friction = 0.85;

//Game Loop
{
                If UpArrow is Pressed, Momentum = Momentum + 0.3;
                Momentum = Momentum * Friction;
                If Momentum > TopSpeed, Momentum = TopSpeed;
                Position = Position + Momentum;
}


                Friction is easily applied as an Aerodynamic feature for each of your vehicles, which is also easily changeable now, for each different vehicle in the game. 


                There are a lot of directions this can be taken, but I wanted to cover some others, like Force/Torque, Vectors and Gravity, in future articles.  This is an article that is meant to be practiced.  The basics are here.

1 comment:

Dan Violet Sagmiller said...

As later realized, Momentum is not the best word here, (force * mass), as mass is not involved. However, Force is. Please use "Force" instead of "Momentum".

:D