setImpulse vs setVelocity: different path results

Discuss new features and future development.

setImpulse vs setVelocity: different path results

Postby BLWNKL » Fri Sep 03, 2010 4:58 pm

Hi all,
I'm having a problem with changing something from using "setVelocity" to a more correct approach using "setImpulse".
Well, at least I "think" it's more correct, since I don't really want a constant velocity; I want friction etc to be taken into account in the behavior. So, in my previous approach, I did this (in my "Ball" class, of which _body is a cpBody, etc):

-(CGPoint)getSpeedFrom:(CGPoint)startPos toLocation:(CGPoint)endPos
CGPoint velocity = cpvsub(endPos,startPos);
velocity = cpvnormalize(velocity);
velocity = cpvmult(velocity, _pixelsPerSecond);
return velocity;
_body->v = [self getSpeedFrom:cpv(_xFrom, _yFrom) toLocation: cpv(_xTo, _yTo)];
...subsequent "cpSpaceStep" iterations, etc. upon FIREing the ball.

// This worked fine, in terms of "accuracy" since I specified the "_xTo" and "_yTo" directly. But as I said, it applied constant
// velocity, which is not what I wanted. SO, I replace that approach with:

CGPoint startpoint = [ball location];
CGPoint endpoint = [aimpoint location];
NSLog(@"SHOOT: startpoint = %f,%f, endpoint = %f,%f", startpoint.x, startpoint.y, endpoint.x, endpoint.y);

// convert CoreGraphics location to Chipmunk coordinates
startpoint.x = CG_X_2Ch(startpoint.x);
startpoint.y = CG_Y_2Ch(startpoint.y);
endpoint.x = CG_X_2Ch(endpoint.x);
endpoint.y = CG_Y_2Ch(endpoint.y);
NSLog(@" in Chipmunk: startpoint = %f,%f, endpoint = %f,%f", startpoint.x, startpoint.y, endpoint.x, endpoint.y);
cpVect target = cpvsub(cpv(endpoint.x, endpoint.y), cpv(startpoint.x, startpoint.y));
NSLog(@"target pt = %f,%f", target.x, target.y);
cpVect targetWithAccel = cpvmult(target, _hitSpeed);
NSLog(@"targetWithAccel pt = %f,%f (hitSpeed = %f)", targetWithAccel.x, targetWithAccel.y, _hitSpeed);
cpVect targetW = cpBodyLocal2World([aimpoint GetBody], targetWithAccel);
NSLog(@"targetW pt = %f,%f", targetW.x, targetW.y);
cpVect r = cpvzero;
cpBodyApplyImpulse(_body, targetW, r);
2010-09-03 14:39:37.000 xtest[15547:20b] FIRE: startpoint = 99.000000,299.000000, endpoint = 99.262161,102.464157
2010-09-03 14:39:37.000 xtest[15547:20b] in Chipmunk: startpoint = -1.000000,-99.000000, endpoint = -0.737839,97.535843
2010-09-03 14:39:37.001 xtest[15547:20b] target pt = 0.262161,196.535843
2010-09-03 14:39:37.002 xtest[15547:20b] targetWithAccel pt = 0.646466,484.639496 (hitSpeed = 2.465909)
2010-09-03 14:39:37.002 xtest[15547:20b] targetW pt = -0.091373,582.175354
// HERE'S the problem:
The behavior that I get when running this differs from the original approach, in that it seems the IMPULSE drives the ball to a slightly different target (aim) point in the space than what I would observe just setting the velocity; this results in the first collision point being very slightly different in the two scenarios. Since the targetW point is in "world" coordinates, I'm not sure what the exact relationship is to the Chipmunk space, so I am a bit stuck as to what to verify. Note: I am not using OpenGL, cameras, viewports, scrolling, or anything obscure. This is plain-vanilla 2D iPhone app using CoreGraphics. Any help is greatly appreciated!
Last edited by BLWNKL on Sun Sep 05, 2010 5:12 pm, edited 1 time in total.
Posts: 3
Joined: Sat Jun 05, 2010 10:13 am

Re: setImpulse vs setVelocity: different path results

Postby BLWNKL » Sat Sep 04, 2010 4:15 pm

BTW, I forgot to mention that the _body that I'm using as an "aimpoint" is not added to the simulation space.
This is because I don't intend for it to participate in collisions. Not sure if this would pose any problem with the vector calc for the impulse, but thought I'd mention it.
Posts: 3
Joined: Sat Jun 05, 2010 10:13 am

Re: setImpulse vs setVelocity: different path results

Postby slembcke » Sat Sep 04, 2010 9:38 pm

I'm not really sure what you are trying to accomplish here. 'target' looks like it's supposed to be an offset in world, 'targetWithAccel' the same but muliplied by some constant, and then 'targetW' is the absolute offset treated as a local offset and then converted to a world offset. I'm guessing that last part to get 'targetW' was not what you meant, but I'm still not quite sure what you are trying to do. It seems like you are trying to do something roughly equivalent to the first, but in a more complicated way.

Also, "world" coordinates is just another name for absolute coordinates.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! ->
User avatar
Site Admin
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Return to Features/Development

Who is online

Users browsing this forum: No registered users and 1 guest