Angle Between Two Points

Started by
3 comments, last by ntwiles 12 years, 2 months ago
Hey guys. I'm working on a method to point an object towards a vector, and I'm getting some frustrating trouble. My math is rusty, but I'm pretty confident that I have the formula right and that I'm either converting something wrong or misunderstanding some method. Here's the function I'm working on:

public void pointAtLocation(Vector2 location)
{
double ydif = location.Y - this.y;
double xdif = location.X - this.x;
float atan = (float)Math.Atan(ydif/xdif);
this.angle = atan * (180f / 3.140f);
}


What I'm trying to do here is first get the angle in radians using the formula angle = atan(opp/adj), then convert that to degrees (spriteBatch.draw takes degrees, right?)

I'm testing it by pointing to the mouse position, and I'm getting erratic results. I expected to have it work in quadrant 1 and have to do some tweaking to get the other three to work, but it doesn't seem to be working anywhere. Can someone show me the mistake I'm making?
Advertisement

Hey guys. I'm working on a method to point an object towards a vector, and I'm getting some frustrating trouble. My math is rusty, but I'm pretty confident that I have the formula right and that I'm either converting something wrong or misunderstanding some method. Here's the function I'm working on:

public void pointAtLocation(Vector2 location)
{
double ydif = location.Y - this.y;
double xdif = location.X - this.x;
float atan = (float)Math.Atan(ydif/xdif);
this.angle = atan * (180f / 3.140f);
}


What I'm trying to do here is first get the angle in radians using the formula angle = atan(opp/adj), then convert that to degrees (spriteBatch.draw takes degrees, right?)

I'm testing it by pointing to the mouse position, and I'm getting erratic results. I expected to have it work in quadrant 1 and have to do some tweaking to get the other three to work, but it doesn't seem to be working anywhere. Can someone show me the mistake I'm making?


two things:
1: if your two points share the same x, then you are dividing by 0
2: use atan2, as such: atan2(ydif, xdif) //yes, y first, it's much safer for angles between two points(and doesn't have to worry about dividing by zero.)
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
I switched out the Atan function for Atan2, good point! Does Atan2 contain functionality to avoid divide by zero errors or is that something I'll need to catch myself?

Of course I still need help with the original issue.
Theres a MSDN article on this here

http://create.msdn.com/en-US/education/catalog/sample/chase_evade

you can download the source code and take a look

BTW the spritebatch call takes a rotation in float radians you can convert from radians-to degrees via the MathHelper.ToRadians();
Thanks for the link blackbook! I actually figured out what my problem was. I was confusing screen location with world location. Rookie mistake.

This topic is closed to new replies.

Advertisement