Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
67 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Limiting Movements
 Determining the
 Difference

 Moving the
 Character


 Printable version
 Discuss this article
 Source code

Moving the character

Now that we have our current movement value, it’s time to put it into the by replacing the old movement code with the new.

if ( buffer[DIK_UP] & 0x80 )
{
  //old code
  //xpos -= (float)sin(heading*piover180) * 0.05f;
  //zpos -= (float)cos(heading*piover180) * 0.05f;
  //new code
  xpos -= (float)sin(heading*piover180) * movementval;
  zpos -= (float)cos(heading*piover180) * movementval;
  ...
}

These changes can easily be applied for moving in all directions.

Modifying head movement

As a final change, I also changed the speed of the head bob. The default change is 10 units every frame, so at 80 frames a second, the movement is 800 degrees per second, which is quite a few of cycles (360 degrees in a circle). At a reasonable frame rate of 40 frames per second the head would bob only 400 degrees. The goal is to vary how much the angle is changed every frame based on the frame rate. To accomplish this, multiply the number of degrees you want the bob movement to change in one second by the number of seconds between frames. At 80 frames per second, there are .0125 seconds between each frame (as previously mentioned). If you want the head to move 400 degrees every second, then multiply 400 degrees by the .0125 seconds between each frame, and you arrive at the number of degrees to perform the head bob movement for the frame.

walkbiasangle+=(400.0f*secsperframe);

In this example the change in the angle would be 5 degrees every frame. At 80 frames per second, the total degrees would be 400 degrees every second.

In conclusion, I cannot say if this is the absolute best way to handle character movement, but it’s a method that has worked for me thus far. I would like to thank Jeff Molofee and Justin Eslinger for their work on the original tutorial. If you have any questions or comments, feel free to e-mail me at ricart3@tcnj.edu.