calculate the path of a body

Official forum for the Chipmunk2D Physics Library.

calculate the path of a body

Postby dieterweb » Wed Dec 22, 2010 9:50 am

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: http://zombiesmash.gamedrs.com or follow us on twitter: http://twitter.com/zombiesmash
dieterweb
 
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany

Re: calculate the path of a body

Postby slembcke » Wed Dec 22, 2010 11:25 am

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/
User avatar
slembcke
Site Admin
 
Posts: 4086
Joined: Tue Aug 14, 2007 7:13 pm

Re: calculate the path of a body

Postby dieterweb » Wed Dec 22, 2010 1:20 pm

Cool, I will give it a try tomorrow.

Thx!
Visit our game Blog: http://zombiesmash.gamedrs.com or follow us on twitter: http://twitter.com/zombiesmash
dieterweb
 
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany

Re: calculate the path of a body

Postby bollu » Fri Dec 24, 2010 10:43 pm

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

Postby dieterweb » Mon Dec 27, 2010 1:03 pm

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: http://zombiesmash.gamedrs.com or follow us on twitter: http://twitter.com/zombiesmash
dieterweb
 
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany

Re: calculate the path of a body

Postby slembcke » Mon Dec 27, 2010 2:00 pm

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/
User avatar
slembcke
Site Admin
 
Posts: 4086
Joined: Tue Aug 14, 2007 7:13 pm

Re: calculate the path of a body

Postby dieterweb » Tue Dec 28, 2010 8:11 am

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: http://zombiesmash.gamedrs.com or follow us on twitter: http://twitter.com/zombiesmash
dieterweb
 
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany

Re: calculate the path of a body

Postby Charlie_Fulton » Sat Jun 18, 2011 11:22 pm

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


Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 2 guests