# Angle Between Two Points

## Recommended Posts

ntwiles    145
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:

[code] 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);
}[/code]

What I'm trying to do here is first get the angle in radians using the formula [i]angle = atan(opp/adj), [/i]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?

##### Share on other sites
slicer4ever    6760
[quote name='Ntwiles' timestamp='1329165829' post='4912726']
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:

[code] 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);
}[/code]

What I'm trying to do here is first get the angle in radians using the formula [i]angle = atan(opp/adj), [/i]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?
[/quote]

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.)

##### Share on other sites
ntwiles    145
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.

##### Share on other sites
DarrenHorton    110
Theres a MSDN article on this here

BTW the spritebatch call takes a rotation in float radians you can convert from radians-to degrees via the MathHelper.ToRadians();

##### Share on other sites
ntwiles    145
Thanks for the link blackbook! I actually figured out what my problem was. I was confusing screen location with world location. Rookie mistake.