Search code examples
javascriptgame-physicstrigonometry

find position of a point with origin, angle and radius


im stuck with a trigonometry problem in a javascript game im trying to make. with a origin point(xa,ya) a radius and destination point (ya,yb) I need to find the position of a new point.

//calculate a angle in degree
function angle(xa, ya, xb, yb) 
{
    var a= Math.atan2(yb - ya, xb - xa);
    a*= 180 / Math.PI;

    return a;
}


function FindNewPointPosition()
{
    //radius  origine(xa,xb)  destination(ya,yb)
    var radius=30;
    var a = angle(xa, xb, ya, yb);

    newpoint.x = xa + radius * Math.cos(a);
    newpoint.y = ya + radius * Math.sin(a);

    return newpoint;
}

Imagine a image because I dont have enough reputation to post one : blue square is the map (5000x5000), black square (500x500) what players see (hud). Cross(400,400) is the origin and sun(4200,4200) the destination. The red dot (?,?) indicate to player which direction take to find the sun .. But sun and cross position can be reverse or in different corner or anywhere !

At the moment the red dot do not do that at all ..

Tks for your help.


Solution

    1. Why did you use ATAN2? Change to Math.atan() - you will get angle in var A
    2. Where you have to place your red dot? inside hud?

    Corrected code https://jsfiddle.net/ka9xr07j/embedded/result/

    var obj = FindNewPointPosition(400,4200,400,4200); - new position 417. 425