Archived

This topic is now archived and is closed to further replies.

Craazer

objects direction calculation

Recommended Posts

Craazer    122
Doesn any one know how to do this? if i have object whit 360 degrees its postion is x y and i want it to face x2 y2 so how do i calculate it?

Share this post


Link to post
Share on other sites
JohnBSmall    881
Short answer:
angle = atan((y2 - y) / (x2 - x)); 
Post if you want the long answer (ie if you don't understand the trigonometry behind it)

John B

[edited by - JohnBSmall on October 19, 2002 5:55:10 PM]

Share this post


Link to post
Share on other sites
Craazer    122
quote:
Original post by JohnBSmall
Short answer:
angle = atan((y2 - y) / (x2 - x));  
Post if you want the long answer (ie if you don''t understand the trigonometry behind it)

John B

[edited by - JohnBSmall on October 19, 2002 5:55:10 PM]


nope im afraid i dont know about it. that atan returns shometing like 1.383852

so what do i do whit that value?

Share this post


Link to post
Share on other sites
Craazer    122

This code doesnt work:


  
double radiant = atan(( (des_y - y) / (des_x - x) ));

int direction = (radiant * 180 / PI); // 0 - 360



that gives me really wierd direction, what did i do wrong?

Share this post


Link to post
Share on other sites
Deyja    920
It gives you the angular difference between the two orientations. If you just want a new angle, use atan(des_y/des_x).

And; the answer will be between 0 and 180 (or 90 and 270?), not 0 and 360. Add 180 if des_y is negative (unless I''m wrong, of course.)

Share this post


Link to post
Share on other sites
Zipster    2359
The range of tan-1 will be the same as the [periodic] domain of tan. Thus, tan-1 will return a value between -PI/2 and PI/2. However, since the quotient of two negative numbers has the same sign as the quotient of two positive numbers (and +/- has the same sign as -/+), you still have to determine whether the result could simply be a reference angle, and that the object could really be in quadrant 2 or 3.

[edited by - Zipster on October 22, 2002 7:00:29 PM]

Share this post


Link to post
Share on other sites
Nypyren    12065

        
float ToAngle(float x, float y)
{
if (x == 0)
{
if (y == 0) return .0f;
else if (y > 0) return PI/2;
else return PI*3/2;
}
else if (x > 0)
{
return atanf(y/x);
}
else
return PI+atanf(y/x);
}


I'm surprised that noone caught the divide-by-zero possibilities.

Pass this function (x2 - x, y2 - y) for your purposes.

This returns angle in radians, 0 if the angle is undefined (x=0 and y=0)

To go to degrees, multiply the return value by 57.2958 (that's short for 180/PI)

[edited by - Nypyren on October 22, 2002 11:23:40 PM]

Share this post


Link to post
Share on other sites
Craazer    122
Thank you for help, i think i got it work now.

conclusion:

  
double radiant = atan2(( (des_y - y) / (des_x - x) ));int direction = (radiant * 180 / PI);

//this gives 180 - -180

// so i just make 360 from thoes values

// just bad that the ''north''(0) is right


Share this post


Link to post
Share on other sites