vendredi 1 juillet 2016

Calculating target point given start point, angle and length GOING TO OPPOSITE DIRECTION


I've angle,length of line and start point given and I want to calculate target point to move my object here. I'm following this post and some others:

cocos2d calculate destination point given start point, angle and distance

Problem : For the 1st and fourth quadrant , it's giving me correct angle. But for 2nd and 3rd quadrant , it's going exact opposite direction. I couldn't figure out issue. This image may make it clear :

enter image description here

Green arrows are showing my intended direction and red arrows show where it actually go.

My code so far :

    float m = (targetPosition.y - initialPosition.y) / (targetPosition.x - initialPosition.x);
    angle = atan (m) * 180 / PI;
    int lineLength = 200;
    float radiansAngle = CC_DEGREES_TO_RADIANS(angle);

    Vec2 endPoint;
    endPoint.y = sinf(radiansAngle) * lineLength + initialPosition.y;
    endPoint.x = cosf(radiansAngle) * lineLength + initialPosition.x;

Aucun commentaire:

Enregistrer un commentaire