Search code examples
iphoneioscocos2d-iphoneangledirection

cocos2d calculate exit point based on initial point and an angle of the gun


I'm having difficulties here with calculating kind of trajectory without any physics engine.

What I'm trying to achieve is to move a cannon to a certain point. I have the cannon's initial position, angle between the cannon and the touch point + distance I want to travel.

The goal here is to calculate the exit point based on the starting point, angle and distance.

Scheme: http://dl.dropbox.com/u/39096612/testcase.png

Thats how I rotate the cannon while moving the finger (behind it, not in front):

CGPoint location = [touch locationInView:[touch view]];
CGFloat angle    = CC_RADIANS_TO_DEGREES(ccpToAngle(ccpSub(cannon.position, location)));
angle += 90;

cannonRotation = angle;

Usually its done with this, but it wont work for me (may be I'm doing it wrong...), really need your help here.

ccp(x + distance * cos(a), y + distance * sin(a))

Solution

  • exitPoint = ccpAdd(cannon.position , ccp(cannon.position.x - touch.x , cannon.position.y - touch.y));