Skip to main content
gamedev.net
🔒 Locked

Direction(digrees) between two points

Started by Shylock 5 replies 450+ views
Original Post
Shylock
Shylock
I am fairly new to programming, and are still working out some of the useful basics. The one I cannot get is how to work out the digrees direction between two points (point a and b).
allsorts46
allsorts46
Create a right angled triangle with the points as either end of the hypotenuse, then use trigonomitry functions to calculate the angles from whichever end you like.

I would give a proper description but I can't remember right now which function is which, so rather than risk getting it wrong and making it worse, I'll leave it as that. Basically you're gonna need to use atan, asin or acos after doing some division of the height and width of the triangle. Like I said, dont remember which.

I can answer properly tommorow if you need, but I'm sure someone else will have provided an answer by then.



"Because there'll be no safety in numbers,
When the right one walks out of the door"


[edited by - allsorts46 on March 21, 2003 6:04:07 PM]
"The finger of blame has turned upon itself"
TSwitch
TSwitch
It's the arc-cosine of the dot product of the two points iirc.

[edited by - TSwitch on March 21, 2003 6:24:15 PM]
Member of the Unban nes8bit or the White Rhino in my Basement Gets Sold to the Highest Bidder Association (UNWRBGSHBA - Not accepting new members.)Member of the I'm Glad Mithrandir Finally Found an Association that Accepts People with his Past History Association (IGMFFAAPPHA)
misterX
misterX
two points: A(ax, ay) & B(bx, by)
the angle between AB and the x axis is:
angle = asin((by-ay) / L)
angle = acos((bx-ax) / L)
angle = atan((by-ay) / (bx-ax))
where L = ((by-ay)^2 + (bx-ax)^2)^(1/2)

BEWARE: the angle you''ll get will range from -90° to 90°, -pi/2 to pi/2. Meaning that you point could on the "opposite part of the circle". Therefore, a position check is necessary if you desire an angle between 0 and 360.
63616C68h
63616C68h
U(x, y, z) = U(3, 4, 6) //arbitrary
V(x, y, z) = V(6, 8, 1) //U & V are the 2 vectors starting at the origin.

Dot Product:
U.V = U(x)*V(x) + U(y)*V(y) + U(z)*V(z)//the dot product is the distance between the origin and the projection of a vector 90 degrees to another vector. If a leg of a right triange and the hypotenuse were the two vectors, then the length of the leg would be the dot product of the two.
Dot Product in Relation to Angles:
U.V = |U| * |V| * cos(angle_between_UandV)
Doing some basic algebra yields:
cos(angle) = |U|*|V|/U.V
******cos-1(|U|*|V|/U.V) = angle ********

Magnitude:
|U| = magnitude or "distance" of vector U.
|U| = sqrt(U(x)2 + U(y)2 + U(z)2)
...basically just pythagorean theorem in 3 dimensions.
A and B are legs of a right triangle, and C is the hypotenuse.
A2 + B2 = C2
So C equals the square root of {A squared plus B squared}.

[edited by - 63616c68h on March 21, 2003 6:23:37 PM]
Keep coming back, because it's worth it, if you work it, so work it, you're worth it!
Shylock
Shylock
Give me a few mins to read through it slowly and I should understand. Thanks guys and/or/not gals.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.