I think that there are 2 possible ways, but you forgot to mention one crutial thing - does the object have to move, or it will simply teleport to location?
Assuming it will teleport, its very easy:
Code: Select all
CGPoint whereToMove = CGPointMake(myTouch.x, myTouch.y);
body.pos = cpv(whereToMove.x,whereToMove.y);
Assuming it will have to move all the way until it reaches end, that's somehow trickier. I actually don't know formula for that (sorry, maybe some smart person will), but from point of view of the programmer, i'd do following:
1. calculate distance between center of body and myTouch - using pythagoras
Code: Select all
length = sqrt((myTouch.x - body.x)^2 + (myTouch.y - body.y)^2)
2. apply impulse of force F into body, using vector body->myTouch
Code: Select all
cpv vector = cpvnormalise(myTouch.x - body.x, myTouch.y - body.y));
vector = cpv(vector.x * F, vector.y * F);
-> use method to apply impulse to body (cpBodyApplyImpulse(cpBody *body, cpVect j, cpVect r) or applyImpulse in Obj-C, profit
Now force F is what I'm missing, I don't know how to calculate that.. Assuming you have weight of body, length, you can calculate that for sure
Happy googling, hope it will help.