calculate the path of a body

Official forum for the Chipmunk2D Physics Library.
Post Reply
dieterweb
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany
Contact:

calculate the path of a body

Post by dieterweb »

Hi Scott,

is there a way to calculate the movement for a body in the coming n seconds? Collisions must not been taken into account.

Thomas
Visit our game Blog: [url]http://zombiesmash.gamedrs.com[/url] or follow us on twitter: [url]http://twitter.com/zombiesmash[/url]
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: calculate the path of a body

Post by slembcke »

I'm finishing up a Cocos2D tutorial that does just that:

Code: Select all

	cpBody body = *(cage.body.body);	
	cpPolyShape shape = *((cpPolyShape *)cage.shape.shape);
	shape.shape.body = &body;
	
	cpVect gravity = space.gravity;
	
	int count = 0;
	for(int i=0; i<300; i++){
		cpBodyUpdatePosition(&body, FIXED_TIMESTEP);
		cpBodyUpdateVelocity(&body, gravity, 1.0f, FIXED_TIMESTEP);
		
		if(cpSpaceShapeQuery(space.space, (cpShape *)&shape, NULL, NULL)){
			// draw shape
			break;
		}
		
		if(i%10==0){
			// draw dot
		}
	}
This is more or less the same code that we use in Twilight Golf to plot the path of the ball and it's first collision point.

It copies the body and the shape onto the stack and steps it forward. If you don't want the collision point, you can just leave out the shape and the shape query.

If you are using custom position or velocity functions you can do this:

Code: Select all

body.position_func(&body, dt);
body.velocity_func(&body, gravity, damping, dt);
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
dieterweb
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany
Contact:

Re: calculate the path of a body

Post by dieterweb »

Cool, I will give it a try tomorrow.

Thx!
Visit our game Blog: [url]http://zombiesmash.gamedrs.com[/url] or follow us on twitter: [url]http://twitter.com/zombiesmash[/url]
bollu
Posts: 37
Joined: Tue Sep 14, 2010 4:33 am
Contact:

Re: calculate the path of a body

Post by bollu »

lol, I wanted to ask the same question too!

Thanks slembcke :)
dieterweb
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany
Contact:

Re: calculate the path of a body

Post by dieterweb »

The code is working as long as I do not use any damping in the space. I added space->damping to the call of cpBodyUpdateVelocity. In this case the calculated path is not the same as the one simulated.
Visit our game Blog: [url]http://zombiesmash.gamedrs.com[/url] or follow us on twitter: [url]http://twitter.com/zombiesmash[/url]
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: calculate the path of a body

Post by slembcke »

Oh, I forgot about that. The damping value is pre-calculated in cpSpaceStep() like this:

Code: Select all

	cpFloat damping = cpfpow(space->damping, dt);
That way it is not dependent on the size of the timestep.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
dieterweb
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany
Contact:

Re: calculate the path of a body

Post by dieterweb »

ok, that fixed the damping problem. Now I have another one :)

Calculating the path works for simple things now. But I it does not work for some bodies with custom velocity functions. I am calling the function when calculating the future path.

Here is my function I use to simulate a boomerang movement:

Code: Select all

void cpBodyUpdateVelocity_boomerang(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
{
	cpBodyUpdateVelocity(body, gravity, damping, dt);

	ZXEntityBoomerang *boomerang = (ZXEntityBoomerang*)((ShapeData*)body->data)->entity;
	if([boomerang flying])
	{
		cpBodySetAngVel(body, 60 * [boomerang sign]);
		cpBodyActivate(body);
		cpBodyApplyImpulse(body, [boomerang returnVect], cpvzero);
		boomerang.returnVect = cpvrotate([boomerang returnVect], cpvforangle(CC_DEGREES_TO_RADIANS(1.6 * [boomerang sign] * 60.0 * dt)));
	}	
}
The calculated path is not even near the real path.

I think I know the reason: The impulse I add every step does not depend on the timestep.

FIXED
Visit our game Blog: [url]http://zombiesmash.gamedrs.com[/url] or follow us on twitter: [url]http://twitter.com/zombiesmash[/url]
Charlie_Fulton
Posts: 2
Joined: Sat May 29, 2010 1:22 am
Contact:

Re: calculate the path of a body

Post by Charlie_Fulton »

It's always fascinating reading conversations between you guys :)

Scott is the tutorial you mentioned for cocos2d posted somewhere?
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests