I've looked everywhere for a solution, but I just cant get this to work. I looked at the demo code that you've put up and did the same to get the mouse joint to work. However when I touch on a body it just snaps and moves in a completely different direction.
Here is the code I'm using:
Code: Select all
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
cpShape *shape = cpSpacePointQueryFirst(_space, [self convertTouchToNodeSpace:touch], GRABABLE_MASK_BIT, 0);
if (shape) {
cpBody *body = shape->body;
_mouseJoint = cpPivotJointNew2(_mouseBody, body, cpvzero, cpBodyWorld2Local(body, [self convertTouchToNodeSpace:touch]));
_mouseJoint->maxForce = 30000.0f;
_mouseJoint->errorBias = 0.12f;
cpSpaceAddConstraint(_space, _mouseJoint);
return YES;
}
return NO;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
if (_mouseJoint) {
cpVect newPoint = cpvlerp(_mouseBody->p, [self convertTouchToNodeSpace:touch], 0.25f);
_mouseBody->v = cpvmult(cpvsub(newPoint, _mouseBody->p), 60);
_mouseBody->p = newPoint;
}
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
[self ccTouchCancelled:touch withEvent:event];
}
- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event {
if (_mouseJoint) {
cpSpaceRemoveConstraint(_space, _mouseJoint);
cpConstraintFree(_mouseJoint);
_mouseJoint = NULL;
}
}
Thanks
ALI