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);

...

OUTPUT:

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!