## calculate the path of a body

Official forum for the Chipmunk2D Physics Library.

### calculate the path of a body

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
dieterweb

Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany

### Re: calculate the path of a body

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/

slembcke

Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

### Re: calculate the path of a body

Cool, I will give it a try tomorrow.

Thx!
dieterweb

Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany

### Re: calculate the path of a body

lol, I wanted to ask the same question too!

Thanks slembcke
bollu

Posts: 37
Joined: Tue Sep 14, 2010 4:33 am

### Re: calculate the path of a body

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.
dieterweb

Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany

### Re: calculate the path of a body

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/

slembcke

Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

### Re: calculate the path of a body

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
dieterweb

Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany

### Re: calculate the path of a body

It's always fascinating reading conversations between you guys

Scott is the tutorial you mentioned for cocos2d posted somewhere?
Charlie_Fulton

Posts: 2
Joined: Sat May 29, 2010 1:22 am