//Let's pretend this is as a triangle i_ //imaginary |.\_ p\..\_ //player ...\__.......m //mouse Here is my attempt to implement the algorithm : a=1; //a is the side that goes from player crds to imaginary point b=sqrt((mouse_x-xplayer)*(mouse_x-xplayer)+(mouse_y-yplayer)*(mouse_y-yplayer)); //b is the other side of the triangle, it goes from player crds to mouse crds c=sqrt((mouse_x-xplayer)*(mouse_x-xplayer)+(mouse_y-(yplayer-1))*(mouse_y-(player-1))); //c is the side opposite to the angle, it goes from i to m angle=acos((c*c)/(2.0*a*a*b*b)); if(mouse_x<xplayer) angle+=pii; |

# Implementing cosine algorithm to angle checking

Started by Afterlife, Oct 06 2001 03:52 AM

3 replies to this topic

###
#1
Members - Reputation: **122**

Posted 06 October 2001 - 03:52 AM

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

Sponsor:

###
#2
Members - Reputation: **122**

Posted 06 October 2001 - 06:59 AM

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

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

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

###
#4
Staff Emeritus - Reputation: **1739**

Posted 06 October 2001 - 08:11 AM

The angle between two vectors is the scalar (dot) product of the vectors divided by the product of the vector magnitudes.ie

cos(theta) = (

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

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

|B| = sqrt( B.x

**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 - PJust 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.x

^{2}+ A.y^{2});|B| = sqrt( B.x

^{2}+ B.y^{2});