Search code examples
cocos2d-iphone

cocos2d calculate destination point given start point, angle and distance


Quick one I think. 2d problem in Cocos2d and xcode.

I have

CGPoint currPoint;
float lineLength;
float angle;

Now, I need to find the point that is lineLength away from currPoint at angle Degrees.

Tried to search, but the answers i have found aren't quite what I was looking for. Would appreciate anyone pointing out the (I assume) pretty simple math I have overlooked.


Solution

  • From the top of my head:

    CGPoint endPoint;
    endPoint.x = sinf(CC_DEGREES_TO_RADIANS(angle)) * lineLength;
    endPoint.y = cosf(CC_DEGREES_TO_RADIANS(angle)) * lineLength;
    endPoint = ccpAdd(currPoint, endPoint);
    

    Not sure where the vector points to, if it may be rotated by 90, 180 or 270 degrees. If so, just add/subtract that amount from angle.