• FEATURED

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

## Angle Between Two Points

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Ntwiles  Members

Posted 13 February 2012 - 02:43 PM

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?

### #2slicer4ever  GDNet+

Posted 13 February 2012 - 02:56 PM

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.

### #3Ntwiles  Members

Posted 13 February 2012 - 03:00 PM

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.

### #4blackbook  Members

Posted 13 February 2012 - 03:42 PM

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();

### #5Ntwiles  Members

Posted 13 February 2012 - 06:17 PM

Thanks for the link blackbook! I actually figured out what my problem was. I was confusing screen location with world location. Rookie mistake.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.