Skip to main content
gamedev.net
🔒 Locked

Which angle?

Started by Mr_Ridd 5 replies 450+ views
Original Post
Mr_Ridd
Mr_Ridd
Howzit I wrote this in Java: Math.toDegrees(Math.atan(Math.toRadians(x2-x1)/Math.toRadians(y2-y1))); Which angle is it calculating, I need to get the angle that my character needs to rotate by. Shot
The ability to succeed is the ability to adapt
Mr_Ridd
Mr_Ridd
So what must I do?
The ability to succeed is the ability to adapt
oliii
oliii
Math.toDegrees(Math.atan( (x2-x1) / (y2-y1) ));


but I''ll probably do instead

Math.toDegrees(Math.atan2(y2-y1, x2-x1));
Everything is better with Metal.
Mr_Ridd
Mr_Ridd
I''ve got a 3D model. When I click on the screen I want the model to turn and face the direction that I clicked. I therefore have to get the angle.
The ability to succeed is the ability to adapt
phaelax
phaelax
Use this method to get the angle in degrees between two points. Result will be between 0 and 360, or -1 if the points are the same.

distance.X = target.X - you.X
distance.Y = target.Y - you.Y


/* Returns the angle between two points given
* the X and Y distance between those two points.
* Returns -1 if the two points are at the same
* coordinates.
*/
public static double atanfull(double x, double y)
{
if (x == 0 && y == 0)
return -1;

double angle;

angle = 90 - Math.toDegrees(Math.atan(y/x));

if (x == 0)
if (y < 0)
angle = 180;
else
angle = 0;

if (x < 0)
angle += 180;

return angle;
}

[edited by - Phaelax on March 2, 2004 9:59:59 PM]

Topic Locked

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

Sign in to reply to this topic.