# Angle Between Two Points

This topic is 2495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites

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

##### Share on other sites
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
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
Thanks for the link blackbook! I actually figured out what my problem was. I was confusing screen location with world location. Rookie mistake.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633704
• Total Posts
3013460
×