|
Implementing cosine algorithm to angle checking
c>2 = a>2 * b>2 * 2 * cos(angle), Can someone tell how it's done properly? I have two points and I need to check the angle between them compared to 0 degrees. I imagined a triangle that has player's coords as one point, mouse coords as another one and a imaginary point 1 unit up from player coords. Example :
The result is no where near what I expected. Anyone know how to implement it the right way or tell me another easier way?
Edited by - Afterlife on October 6, 2001 10:53:41 AM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
I think the formula you're looking for is:
a² = b² + c² - 2bc * cos(angle)
Where 'a', 'b' and 'c' are the side lengths, and the angle is opposite 'a'. Someone correct me if I'm wrong, it's pretty late![](smile.gif)
Argh -- what's the superscript tag? Rrgh. Let's see if the ANSI superscripted two works.
Edited by - Dracoliche on October 6, 2001 2:01:00 PM
a² = b² + c² - 2bc * cos(angle)
Where 'a', 'b' and 'c' are the side lengths, and the angle is opposite 'a'. Someone correct me if I'm wrong, it's pretty late
![](smile.gif)
Argh -- what's the superscript tag? Rrgh. Let's see if the ANSI superscripted two works.
Edited by - Dracoliche on October 6, 2001 2:01:00 PM
Oh yes indeed. I remembered the formula incorrectly :| Thanks
*need to find a school and fast*
Edit : also spotted another error in the code, but now it works
Edited by - Afterlife on October 6, 2001 2:59:00 PM
*need to find a school and fast*
Edit : also spotted another error in the code, but now it works
Edited by - Afterlife on October 6, 2001 2:59:00 PM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
The angle between two vectors is the scalar (dot) product of the vectors divided by the product of the vector magnitudes.ie
A *B = |A ||B |cos(theta)
cos(theta) = (A *B ) / (|A ||B |)
Assuming the player is at a point P, the mouse is at M and we choose a point up from the player U, your two vectors are
A = U - P
B = M - P
Just in case you''re rusty on vector math, the differences are:
U - P = (U.x - P.x, U.y - P.y);
M - P = (M.x - P.x, M.y - M.x);
The scalar product is:
A * B = (A.x * B.x) + (A.y * B.y)
And the magnitudes are:
|A| = sqrt( A.x2 + A.y2 );
|B| = sqrt( B.x2 + B.y2 );
A *B = |A ||B |cos(theta)
cos(theta) = (A *B ) / (|A ||B |)
Assuming the player is at a point P, the mouse is at M and we choose a point up from the player U, your two vectors are
A = U - P
B = M - P
Just in case you''re rusty on vector math, the differences are:
U - P = (U.x - P.x, U.y - P.y);
M - P = (M.x - P.x, M.y - M.x);
The scalar product is:
A * B = (A.x * B.x) + (A.y * B.y)
And the magnitudes are:
|A| = sqrt( A.x2 + A.y2 );
|B| = sqrt( B.x2 + B.y2 );
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement