calculate the path of a body
-
- Posts: 176
- Joined: Fri Feb 27, 2009 7:12 am
- Location: Germany
- Contact:
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
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]
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: calculate the path of a body
I'm finishing up a Cocos2D tutorial that does just that:
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
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
}
}
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 176
- Joined: Fri Feb 27, 2009 7:12 am
- Location: Germany
- Contact:
Re: calculate the path of a body
Cool, I will give it a try tomorrow.
Thx!
Thx!
Visit our game Blog: [url]http://zombiesmash.gamedrs.com[/url] or follow us on twitter: [url]http://twitter.com/zombiesmash[/url]
-
- Posts: 37
- Joined: Tue Sep 14, 2010 4:33 am
- Contact:
Re: calculate the path of a body
lol, I wanted to ask the same question too!
Thanks slembcke
Thanks slembcke
-
- Posts: 176
- Joined: Fri Feb 27, 2009 7:12 am
- Location: Germany
- Contact:
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.
Visit our game Blog: [url]http://zombiesmash.gamedrs.com[/url] or follow us on twitter: [url]http://twitter.com/zombiesmash[/url]
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: calculate the path of a body
Oh, I forgot about that. The damping value is pre-calculated in cpSpaceStep() like this:
That way it is not dependent on the size of the timestep.
Code: Select all
cpFloat damping = cpfpow(space->damping, dt);
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 176
- Joined: Fri Feb 27, 2009 7:12 am
- Location: Germany
- Contact:
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:
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
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)));
}
}
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]
-
- Posts: 2
- Joined: Sat May 29, 2010 1:22 am
- Contact:
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?
Scott is the tutorial you mentioned for cocos2d posted somewhere?
Who is online
Users browsing this forum: No registered users and 3 guests