News/Home

Evochron

Arvoch

Forums

Others

About

Contact

Add to
favorites
StarWraith 3D Games - Game Development Articles Section

Using Time for Object Movement and Rotation

   One of the most important aspects to a 3D game is accurate and consistent movement for all objects. Without it, the speed at which objects move and rotate will vary depending on the framerate. This creates major problems in multiplayer games and in overall gameplay consistency. If the game operates in slow motion for one player, it will likely be easier to succeed then for a player using a more powerful system that achieves a higher framerate. In multiplayer, if objects don't move at the same speed on every connected system, then they will jump around. So it's critical that all in-game objects move and rotate at the same speed, regardless of whether your game runs at 10 frames per second, or 100.

   To provide consistent object movement, you'll need to constantly keep track of time and adjust speed based on the difference from a target rate. The first step is to assign two variables to keep track of time. One will track the time of the last screen update, the other will track the current time. In DarkBasic, it will look like this:

sync
oldtime=currtime
currtime=timer()


   In this example, 'oldtime' will continually equal the value of the time at the last screen update while 'currtime' will equal the current time value. From there, you'll want to compare the difference between the two. This will give us a value that we can use to compare with a predefined speed we want to use. To establish that predefined value, you need to decide what speed you want everything to progress at. For this example, we'll use a target value of 30 frames per second. It won't matter if the framerate goes above or below that speed, our routine will adjust the movement factor as needed. 30 frames per second means that each frame will take 33.3 milliseconds (we'll round to 33 for our example). So 33 is the target time value we want for objects to move and rotate at a specific speed. This means that our adjustment value should equal 1.0 if the time between frames is 33 milliseconds. But if the time between frames is 99 milliseconds, we know that the frame rate has dropped to 10 and our adjustment factor needs to be 3.0 to move an object three times as far for each frame in order to cover the same distance that the object would move if the framerate was 30. It's also a good idea to soften the adjustment result by adding the old difference value to the new one, then dividing by two. This helps prevent excessive twitching if the game pauses for a brief amount of time between two frames or if the value constantly changes by a small amount (which does frequently happen). Also check for values that exceed the limits of what should be received to prevent inaccurate movement. Here is what the routine would look like in DarkBasic to accomplish this:

sync
oldtime=currtime
currtime=timer()
timediff=currtime-oldtime
if timediff<200 and timediff>0 then timefactor#=(timefactor#+(timediff/33.0))/2.0 else timefactor#=1.0


   Then when we want to move an object, we simply multiply its speed rate by the time adjustment factor. So if our object is moving at a speed setting of 5.0, our routine would look like this:

objspeed#=5.0
move object 1,objspeed#*timediff#
sync
oldtime=currtime
currtime=timer()
timediff=currtime-oldtime
if timediff<200 and timediff>0 then timefactor#=(timefactor#+(timediff/33.0))/2.0 else timefactor#=1.0


   If the framerate is 30, then the time factor value will equal 1.0 and our object will move at exactly 5.0. If the framerate is 10, then the time factor value will equal 3.0 and our object will move 15.0, but the distance perceived by the player will be the same. We just have to move the object 3 times as far if the frame rate is only 1/3 of what it should be to move exactly 5.0. Likewise, if the framerate exceeds 30, then the adjustment factor will drop below 1.0 to move the object at a slower rate to cover the same distance over time. You'll need to apply the same adjustment to object rotations, counter variables, and any other gameplay aspect that relies on movement or increasing/decreasing values (such as a health meter or energy recharging system).

   Using this concept, you can provide the same consistent gameplay performance across a wide range of systems.

___________________________________

Copyright © 1999-2005 StarWraith 3D Games LLC.
All Rights Reserved.
All trademarks are the property of their respective companies


Alternate route: spacecombat.htm
Alternate route: spacecombat.org
Alternate route: evochron.com

Game Quick Links: