Search code examples
cocos2d-x

How can I find end point using start point, angle and distance


I have following metrics

Vec2 start = Vec2(20, 20);
float angle = 64.0f;
float distance = 500.0f;

And I want to find end point but i couldn't figured out


Solution

  • How can I find end point using start point, angle and distance?

    Vec2 start     = Vec2(20, 20);
    float angle    = 64.0f;
    float distance = 500.0f;
    

    To prepare for the operation, you have to include

    #include <math.h>
    

    First you have to transform the angle from Degree to Radians, because for the following functions the angle is required in radians:

    float angle_rad = angle * 3.1415927f / 180.0f;
    

    Then you have to calcualte the direction Unit vector. This can be done by the Trigonometric functions Consine (cos) and Sine (sin):

    Vec2 dir_vec(cos(angle_rad), sin(angle_rad));
    

    The end point is the start point plus the the direction unit vector (dir_vec) multiplied by the distance:

    Vec2 end = start + distance * dir_vec;