Page 1 of 1

update cycle

Posted: Thu Jul 28, 2011 10:25 am
by YASHA
Hi , i am using Chipmunk engine during one day :D , and i have trouble with "for" cycle

for(cpFloat time = 0; time < 2; time += timeStep)
{
..........
}

with that cycle my programm hang, but without this cycle game doesnt game physics, how to modernize this cycle? i use graphics game engine - HGE(haaf game engine) and Chipmunk, programing languege is C++, OS - Windows

Sorry for my english:)

Re: update cycle

Posted: Thu Jul 28, 2011 11:56 am
by slembcke
If your game runs at a fixed framerate, (always at 30 or 60fps, etc) you can just do cpSpaceStep(space, 1.0/60.0), etc and that will probably work fine.

If you needed the physics to run smaller timesteps, you can do this:

Code: Select all

	int steps = 3; // step the physics 3 times every frame at 60fps
	cpFloat dt = 1.0f/60.0f/(cpFloat)steps;
	
	for(int i=0; i<steps; i++){
		cpSpaceStep(space, dt);
	}
I do those in a lot of my simple games and it works ok.

The best way to do it is to let your framerate and physics rate be different. Here is a good tutorial on how to do that:
http://gafferongames.com/game-physics/f ... -timestep/

Re: update cycle

Posted: Thu Jul 28, 2011 12:48 pm
by YASHA
Hmm, no results, maybe chipmunk dont know that my CircleBody is dynamic_body , how to set that?

Re: update cycle

Posted: Thu Jul 28, 2011 9:39 pm
by slembcke
Did you forget to call cpSpaceAddBody()?

Re: update cycle

Posted: Fri Jul 29, 2011 2:12 am
by YASHA
No i have written that : ballBody = cpSpaceAddBody(space, cpBodyNew(mass, moment));

i have written my code with Hello World Chipmunk(in official documentation)

Re: update cycle

Posted: Fri Jul 29, 2011 2:32 am
by YASHA
OH my game is work, my variable timeStep=1.0f/60.0f was wrong, and i try to set in "hands" cpSpaceStep(space,1.0f/60.0f);

Re: update cycle

Posted: Fri Jul 29, 2011 3:38 am
by YASHA
Wow, nice, you engine is good and simple:)

Re: update cycle

Posted: Fri Jul 29, 2011 8:07 am
by slembcke
Thanks. Glad you like it.