update cycle

Official forum for the Chipmunk2D Physics Library.
Post Reply
YASHA
Posts: 10
Joined: Thu Jul 28, 2011 10:19 am
Contact:

update cycle

Post 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:)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: update cycle

Post 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/
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
YASHA
Posts: 10
Joined: Thu Jul 28, 2011 10:19 am
Contact:

Re: update cycle

Post by YASHA »

Hmm, no results, maybe chipmunk dont know that my CircleBody is dynamic_body , how to set that?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: update cycle

Post by slembcke »

Did you forget to call cpSpaceAddBody()?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
YASHA
Posts: 10
Joined: Thu Jul 28, 2011 10:19 am
Contact:

Re: update cycle

Post 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)
YASHA
Posts: 10
Joined: Thu Jul 28, 2011 10:19 am
Contact:

Re: update cycle

Post 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);
YASHA
Posts: 10
Joined: Thu Jul 28, 2011 10:19 am
Contact:

Re: update cycle

Post by YASHA »

Wow, nice, you engine is good and simple:)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: update cycle

Post by slembcke »

Thanks. Glad you like it.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests