Tricks of the Windows Game Programming Gurus:Playing God: Basic Physics Modeling

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

This is why everything doesn’t get sucked into the ground. Okay, now that we know that, let’s derive the motion equations of this block mass. First, we lay down an x,y coordinate system on the incline plane with +X parallel to the plane and in the downward sliding direction; this helps the math. Then we write the equilibrium equations for the x and y axes. For the x-axis we know that the component of gravity pushing the block is

    force due to gravity = m*g*sin θ.

And the force due to friction holding the block from sliding is

    force due to friction = -η*μs.

The negative sign is because the force acts in the opposite direction. When the object isn’t sliding we know that the sum of these forces are equal to 0. Mathematically, we have

    force due to gravity + force due to friction = 0

Or, the sum of forces in the x-axis is

    Σ Fx = m*g*sin θ - η*μs = 0.

We have to do the same for the y-axis, but this is fairly easy because the only forces are the weight of gravity and the normal force:

    Σ Fy = η - m*g*cos θ

All right, so all together we have

    Σ Fx = m*g*sin θ - η*μs = 0.

    Σ Fy = η - m*g*cos θ = 0.

But, what is η? From Σ Fy, we once again see that:

You can just average μk and μs and use that value in all the calculations. Because you’re making video games and not real simulations, it doesn’t matter if you oversimplify the two frictional cases into one, but if you want to be correct, you should use both frictional constants at the appropriate times.

    η - m*g*cos θ = 0.

Hence,

    η = m*g*cos θ,

therefore we can write:

    Σ Fx = m*g*sin θ - (m*g*cos θ)*μs = 0.

This is what we need. From it we can derive the following results:

    m*g*sin θ = (m*g*cos θ)*μs

    μs = (m*g*sin θ)/(m*g*cos θ) = tan θ

Or canceling out the m*g and replacing sin/cos by tan,

    θcritical = tan-1 μs

Now listen carefully. This tells us that there is an angle called the critical angle (θcritical) at which the mass starts to slide. It is equal to the inverse tangent of the static coefficient of friction. If we didn’t know the frictional coefficient of a mass and some incline plane, we could find it this way by tilting the plane until the mass starts to move. But this doesn’t help us with the x-axis, or does it? The equation tells us that the mass won’t slide until the angle θcritical is reached. When it is reached the mass will slide governed by:

    Σ Fx = m*g*sin θ - (m*g*cos θ)*μs

Well, almost… There is one detail. When the mass starts to slide, the difference is m*g*sin θ - (m*g*cos θ)*μs > 0, but in addition we need to change the frictional coefficient to μk (the coefficient of kinetic friction) to be totally correct!

    Fx = m*g*sin θ - (m*g*cos θ)*μk

With all that in mind let’s compute the final force along the x-axis. We know that F=m*a, therefore:

    Fx = m*a = m*g*sin θ - (m*g*cos θ)*μk

And dividing by m we get:

    a = g*sin θ - (g*cos θ)*μk

    a = g*(sin θ - μk*cos θ)

You can use this exact model to move the block mass, that is, each cycle you can increase the velocity of the block in the positive X-direction by g*(sin θ - μk*cos θ). There’s only one problem: This solution is in our rotated coordinate system! There’s a trick to getting around this: You know the angle of the plane, and hence you can figure out a vector along the downward angle of the plane:

    xplane = cos θ

    yplane = -sin θ

    Slide_Vector = (cos θ, -sin θ)

The minus sign is on the Y-component because we know it’s in the –Y direction. With this vector we can then move the object in the correct direction each cycle—this is a hack, but it works. Here’s the code to perform the translation and velocity tracking:

    // Inputs

    float x_pos      = SX, // starting point of mass on plane

          y_pos      = SY,

          y_velocity = 0,  // initial y velocity

          x_velocity = 0,  // initial x velocity

          x_plane    = 0,  // sliding vector

          y_plane    = 0,

          gravity    = 1,  // do want to fall too fast

          velocity   = INITIAL_VEL, // whatever

         // must be in radians and it must be greater

         // than the critical angle

         angle      = PLANE_ANGLE, // compute velocities in x,y

         frictionk   = 0.1; // frictional value

    // compute trajectory vector

    x_plane = cos(angle);

    y_plane = sin(angle); // no negative since +y is down

    // do slide loop until object hits

    // bottom of screen at SCREEN_BOTTOM

    while(y_pos < SCREEN_BOTTOM)

         {

         // update position

         x_pos+=x_velocity;

    y_pos+=y_velocity;

         // update velocity

          x_vel+=x_plane*gravity*(sin(angle) - frictionk *cos(angle));

          y_vel+=y_plane*gravity*(sin(angle) - frictionk *cos(angle));

         } // end while

The point of physics modeling sometimes is just to understand what the underlying physics are so you can model them in a somewhat convincing manner. In the case of the incline plane, basically all that math just boiled down to the concept that acceleration is a function of the angle (we knew this from common sense). However, in Volume II of the book I’m going to cover much more realistic physics using numerical integration, and in those cases, you need to know the real models and real forces on everything.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.