Jump to content
  • Advertisement
Sign in to follow this  
Ultra The Vampire

2 questions (speed, state machines)

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how does speed work in games? i thought it was just the XYZ values being passed are more (e.g running gives +5 to Y every frame) but that causes stuttering in my game. how is speed done so smoothly in most games? also i wanted to know how a program can running a loop or two while not even being in the main loop (maybe i'm wrong on that one) and still be able to run the main loop? (e.g. a enemy walks around sniffing for humans, and alien flys around shooting fireballs, while the world is being rendered) i heard something about finite state machines is that what i'm looking for? if so, can someone direct me to a good tutorial to learn them, thank you.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Ultra The Vampire
how does speed work in games? i thought it was just the XYZ values being passed are more (e.g running gives +5 to Y every frame) but that causes stuttering in my game. how is speed done so smoothly in most games?

You can keep track of the time elapsed since the previous update was done. If you multiply this with your velocity vector, the velocity can be interpreted as being per second and the elapsed time scales the velocity such that the correct amount is applied. Example:

elapsedTime = 0.3s.
velocity = 1 unit/sec.
newPos = oldPos + elapsedTime * velocity


Quote:
Original post by Ultra The Vampire
also i wanted to know how a program can running a loop or two while not even being in the main loop (maybe i'm wrong on that one) and still be able to run the main loop?

A single-threaded game cannot do this -- a single-threaded program is always at one and only one position in your program's code. However the effect can be achieved by using step-functions, i.e., letting each unit update its knowledge and execute actions in sequence:

// Main loop:
while( true )
{
enemey->SnifALittleForHumans()
alien->FlyAroundAndShootABallIfYouLike()
}

Finite state machines can be used here, but usually are used within the mentioned procedures to have an explicit concept of state in an (AI) actor. In the main game loop state is usually implicit.

Greetz,

Illco

Share this post


Link to post
Share on other sites
On a single threaded machine, you can do whats called a Fixed-Logic system, where you update the game at regular intervals, but draw as many frames as possible. I don't like these very much, but I'll show you how they work.

Lets say my Fixed Logic system runs at 30FPS, that means roughly every 33ms I have to update it.

void gameloop()
{
int last_clock;
int next_clock;
int curr_clock;
int percentage;
while(1)
{
last_clock = getTimer();
next_clock = last_clock + 33;
updateLogicSystem();
curr_clock = getTimer();
do
{
percentage = (curr_clock - last_clock) * 100 / 33;
updateVideoSystem( percentage );
curr_clock = getTimer();
}
while( curr_clock < next_clock );
}
}


So, every 33ms, rather than moving something five pixels away, you set their target destination 5 pixels away. Then, during the updateVideoSystem() calls, you use the percentage to work out how far between a sprite should be between it's original destination and the target destination.

This system I've lined outs has its faults, so, you'll have to tinker with it a lot to get it to work right.

Share this post


Link to post
Share on other sites
Quote:
Original post by Illco
Quote:
Original post by Ultra The Vampire
how does speed work in games? i thought it was just the XYZ values being passed are more (e.g running gives +5 to Y every frame) but that causes stuttering in my game. how is speed done so smoothly in most games?

You can keep track of the time elapsed since the previous update was done. If you multiply this with your velocity vector, the velocity can be interpreted as being per second and the elapsed time scales the velocity such that the correct amount is applied. Example:

elapsedTime = 0.3s.
velocity = 1 unit/sec.
newPos = oldPos + elapsedTime * velocity


won't that cause the npc to go slow at first and infinitly fast after a while? and still cause stuttering?

[Edited by - Ultra The Vampire on September 13, 2005 10:29:24 AM]

Share this post


Link to post
Share on other sites
Nope, since the value of elapsedTime is very small, often smaller than what the screen is able to update because of screen refreshment limitations, the movement will be very smooth. I.e. it is imporant that you store the position and velocity of an object in floats, not ints.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!