Page 1 of 1

calculate the path of a body

Posted: Wed Dec 22, 2010 9:50 am
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

Re: calculate the path of a body

Posted: Wed Dec 22, 2010 11:25 am
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);

Re: calculate the path of a body

Posted: Wed Dec 22, 2010 1:20 pm
by dieterweb
Cool, I will give it a try tomorrow.

Thx!

Re: calculate the path of a body

Posted: Fri Dec 24, 2010 10:43 pm
by bollu
lol, I wanted to ask the same question too!

Thanks slembcke :)

Re: calculate the path of a body

Posted: Mon Dec 27, 2010 1:03 pm
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.

Re: calculate the path of a body

Posted: Mon Dec 27, 2010 2:00 pm
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.

Re: calculate the path of a body

Posted: Tue Dec 28, 2010 8:11 am
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

Re: calculate the path of a body

Posted: Sat Jun 18, 2011 11:22 pm
by Charlie_Fulton
It's always fascinating reading conversations between you guys :)

Scott is the tutorial you mentioned for cocos2d posted somewhere?