Delayed effect in my game

Started by
7 comments, last by Tompa 20 years, 5 months ago
I need a "swamped" effect in my game. What I mean is that when I oush forward the vehicle should not move instantly, but hold for a short while (say 50ms) and then react. How do I do this? Anyone know what I mean? Thanks!
Advertisement
well if you just want to straight up delay movement, then do just that. on the first keypress for movement start a timer of 50ms or whatever. when it gets to zero start accepting input. that's going to just delay you response to keypresses though which isn't the best looking thing in the world.

a better way to do it would be to just use good ol physics for your movement. F = ma. f is the force of the engine, m is hte mass of the truck, a is the acceleration of your machine. (you can easily apply a max velocity to your truck so it doesn't accelerate forever). when you are swamped, just lower the force of the engine. that way you just get really slow acceleration. or you could just lower the max velocity. when you get unswamped, just restore the engine to it's normal force output

you could also (if you're masochistic) do something with springs... maybe have the applier of force on your vehicle be represented by something that pulls on a spring that in turn pulls on your vehicle. the spring will strech out initially until the force of the spring overcomes the extra force of friction applied by the "swamped" effect. just look up some spring physics and the implementation will become clear. i don't think this is a great idea though, i'm just brainstorming out of control for some reason

-me

[edited by - Palidine on November 12, 2003 3:11:58 AM]
Thanks for the answer. I already have some physics in that way, that I have acceleration and so on, so it''s the first case I''m going to choose I guess. Is it possible to just call a delay-function? I have a vague memory that it exists a such function, like delay(50); for a 50ms delay. What I don''t know is what library this would be in or if it will just delay the keboard input or the whole program (The latter seems bad.)
What I''m trying to emulate here is the delay that comes when you remote control something over a far distance.
Anyway, do you know where the function is to be found?
I think any delay() function would delay the entire program. I would suggest the previously explained (and better alternative! of) physics, since you likely also have an underwater flag of some sort to monitor your vehicle''s behavior?



matrix³
[ email | web | apple gl | linux gl | opengl | freeglut | glui | lua ]
As I said, I already have acceleration-physics. What I mean is that when you press forward, nothing should happen until some time has passed.
So there is no way of just (or function for) delaying a bit of code for a given timespan?
Perhaps a way of doing it is to store the movements in a vector, what do you think?
Well obviously, you do not want a delay/sleep type function, since that would halt your entire program. Just make it accelerate really slowly when starting from standing still. I do not really think delays in controls increase game-enjoyment a lot, but that''s your choice.
This is not the right forum for this question, by the way.
You can delay using Sleep(50). But your whole display loop would pause and it might be noticable.

Another way is to init a variable.Something like this.
#define CHECKKEYSTATEEVERYMSEC 10 //how often you check your keystate
int timer;
bool atRest = true;//initially at rest

void WinMain()
{
//init crap
while()
{
/////////////////LOGIC CODE/////////////////////////
if(GetTickCount()-lastTime>CHECKKEYSTATEEVERYMSEC)
{//this way, your display loop is seperate from your logic code, so your acceleration is independant from the framerate.
CheckKeyboard(VK_UP);
lastTime = GetTickCount();
}
///////////////DISPLAY LOOP////////////////////////
display();//this way you can get as many fps as your gfx card can handle.

}

}

void CheckKeyboard(int key)
{//this function is called every , not when u get the message from WndProc(WndProc is slow). You should be maintaning a boolean value of whether your key is being pressed or not.
if(key[VK_UP])
{

if(atRest)
{//if car is not moving
timer=50;
atRest=false;
}
else if(timer>0)
{//atRest is false
timer--;
}
else
{//atRest is false and timer is 0 so we are ready to go
car.accelerate();
}

}

}



Thanks!

This might just do the trick. Will report back if/when I get it to work.

Thanks again!
It works!
Just had to make sure atRest was set to true when I released the button again.

Thankyouthankyouthankyou!

This topic is closed to new replies.

Advertisement