Advertisement

time-based modelling

Started by January 05, 2002 04:38 PM
2 comments, last by untalkative_monkey 23 years, 1 month ago
Hey all. I''m trying to figure out how to use time-based physics modelling as opposed to frame-based. Here''s my game loop: while(TRUE) { static int lastTime; int time = GetTickCount() - lastTime; lastTime = GetTickCount(); //do everything... } Now, when would I use time in the equations? Only for movement? Or would I have to use time in calculating acceleration, velocity, gravity and so forth? A little help would be greatly appreciated ...go on and live with no regrets, you only have one life...
...go on and live with no regrets, you only have one life...
Depends on how anal you are.

You should apply it to anything that is "added to". Theoretically, you ought to have some sort of curved response to things like acceleration (and acceleration of acceleration, etc) but practically, I don''t think it''s worth the trouble.

Keep all your forces in terms of units per second and then everything will make sense in your head when you need to apply them.



Advertisement
Here's how I'm currently doing it for the 2D overhead asteroids type game I'm working. I'm not sure this is the best way, but it seems to work for me. The problem I ran into was when I tried to directly translate physics equations into useable code that would work in my game. This is what I came up with, maybe it can be of some insight.. I'd appreciate any comments. I've left out big chunks of the code that weren't relevent.
        ...float cX; //change in Xfloat cY; //change in Yfloat priorX; //previous change in X, used to restrict speedfloat priorY; //previous change in Yfloat velocity = 5.0f //velocity = change in position / change in timewhile(!CL_Keyboard::get_keycode(CL_KEY_ESCAPE)){     long int   time1 = CL_System::get_time();     long int timerdiff = (CL_System::get_time() - time1);     float time = timerdiff / 1000.0f; //to put it into seconds     float displacement = velocity * time;     double rad;.....						if(CL_Keyboard::get_keycode(CL_KEY_UP))		{ 		        rad = warbird->getAngle() * 0.01745;			cX += sin(rad) * displacement;			cY -= cos(rad) * displacement;		}		if(CL_Keyboard::get_keycode(CL_KEY_DOWN))		{			rad = warbird->getAngle() * 0.01745;			cX += sin(rad) * -displacement;			cY -= cos(rad) * -displacement;		}		                if(abs(cX) > 5.0f)			cX = priorX;		if(abs(cY) > 5.0f)			cY = priorY;		                priorX = cX;		priorY = cY;                warbird->addX(cX);                warbird->addY(cY);									warbird->draw();...		        



Edited by - eotvos on January 10, 2002 6:59:04 PM
Just keep physics in a seperate thread with a fixed timestep. Otherwise, use the formulae you learned in first-year physics. They all express things in terms of time!

This topic is closed to new replies.

Advertisement