Skip to main content
gamedev.net
🔒 Locked

Finding an angle between 2 objects (2D)

Started by Bluefox Mar 19, 2003 at 8:32 AM 3 replies 250+ views
Original Post
Bluefox
Bluefox
Im trying to find the angle from the gunship to the beacon, using the atan2() funtion to get the angle, so it can turn round and face it. The values being returned to me dont make much sense, do I need to convert to degrees - if so how? new_x = GunShip->x - Beacon->x; //x distance new_y = GunShip->y - Beacon->y; //y distance GunShip->new_direction = atan2(new_y,new_x);
Stary
Stary
Yes, you need to convert it to degrees. As you''ll find if you ever looked in a manual or help page or documentation, the atan2 function returns a value in the range [-pi, pi]. If you don''t know how to convert radians to degrees, google will serve you well.
poisonfruitloops
poisonfruitloops
hi i''ve think i have sumthin i''ve been using that may be of help...

i -think- there may be a bug in it sumwhere but here it is:


angle = atan(fabs(relativeObject.X) / fabs(relativeObject.Y)) * (180 / pi);

if (relativeObject.X > 0 && relativeObject.Y > 0)
// front-right
returnAngle.set(angle, 0, 0);
else if (relativeObject.X > 0 && relativeObject.Y < 0)
// back-right
returnAngle.set(180 - angle, 0, 0);
else if (relativeObject.X < 0 && relativeObject.Y < 0)
// back-left
returnAngle.set(180 + angle, 0, 0);
else if (relativeObject.X < 0 && relativeObject.Y > 0)
// front-left
returnAngle.set(360 - angle, 0, 0);


u may need to hack it up a little but i hope it helps
Bluefox
Bluefox
Thanks for the replies, you both been helpful.
JohnBolton
JohnBolton
quote:
Original post by poisonfruitloops
angle = atan(fabs(relativeObject.X) / fabs(relativeObject.Y)) * (180 / pi);



Don''t use atan -- you will get divide-by-0. Use atan2 .
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.