# Calculate the angle between two points.

Hi there folks.

I know this isn’t a maths forum, and I’m sure this must have been asked before, and yes I’ve searched, but I can’t find a working answer. I need to calculate the angle between two points. For this example we could assume the following:
Character A is at position 0,0.
Character B is at position 6,4.
And I need the angle from A to B.

Normally we would calculate the vector, but in this case it’s obviously 6,4. Or 3,2.
So if I Atan2(4,6) I should get the radians.
Then If I r*180/Pi I should get the degrees.

``````Dim x As Single = 6
Dim y As Single = 4
Dim r As Single = Math.Atan2(y, x)
Dim d As Single = r * (180 / Math.PI)
``````

I have checked this with converters and calculators and the result is 33.69 degrees, give or take a couple of thousandths. I don’t dispute the results, but it’s not what I was expecting. Given the starting vector of 6,4 I was expecting a degrees value of over 90, probably around 110 (on screen direction)
Now, if I add 90 degrees to the result I get 123, which is round about what I was expecting, but as the values change they drift away from my required results.
If I start with a vector calculation from another quadrant (other direction) I get results that confuse me more!
I have tried negative values for different quadrants and still I’m confused.

I’m sure I’m missing something so idiotically simple. Could someone please help my befuddled brain?

Not sure I follow your logic there. 0,0 to 6,4 is going to be a 30 degree angle (give or take):

If you do Math.Atan2(4,6), then you are saying “What is the angle of a vector starting where I am standing, and going to the right 6 units, and up 4 units”

You said:

“Given the starting vector of 6,4”

That’s not true, the starting point is (0,0), and goes out to (6,4)

Hmm.

Does r*(180/Math.Pi) always return an angle in the positive quadrant?

180 and Math.Pi are constants, so whether that formula is positive or negative depends on whether r is positive or negative.

Whether Atan2 returns positive or negative, I am actually not sure where it flips over from positive to negative. You’ll have to look at the docs or fire up the debugger.

edit:

If by positive you mean top/right quadrant, no, that value has a period of 360 degrees, so it just depends on what r is.

I got it sorted out in the end, I had to rotate the value returned by 90 degrees to get everything to compute properly, then fiddle about with the ‘turning’ I had to perform to overcome traversing 2pi to 0 and vice versa, but it’s working. I’d post the code, but I’m not sure it would make much sense without the entire app.

Thanks for your input guys.