Archived

This topic is now archived and is closed to further replies.

BlackEye

Bet this comes up alot!!!

Recommended Posts

how would you get the angle between two points? Points are each an X,Y coordinate. ? Iam plaing with trigonometry bet getting a big head ache! i have this to start with..

float DistanceToPoint(float X,float Y)
{
	return sqrt((X*X) + (Y*Y));
}

float AngleToPoint(cords MyPoint, cords DestPoint)
{

	float Opp = (DestPoint.X - MyPoint.X);
	float Hyp = DistanceToPoint(Opp,(DestPoint.Y - MyPoint.X));

	return asin(Opp/Hyp);


}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi, you cant really find the angle between 2 two-dimensional points, it would have to be two lines, not two points. I assume you mean the angle between the lines connecting both points to the origin.

This is fairly trivial using the dot product. You can show that the dot product of two vector A and B is equal to


A.B = |A||B|cos(theta)


By using the Laws of cosines(google this if you want to see how its done). |A| is the magnitude of A


|A| = square root(Ax^2 + Ay^2)


That is the 2 dimensional form. Its simply the square root of the sum of the square of each x and y components.

A.B is the dot product, and is equal to


A.B = AxBx + AyBy


In two dimensions. Since you know A and B, you can work out everything in the original equation except theta, which is the angle between the two vectors, solving the equation will give you theta.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
do a search of this forum and you will find at least one thread describing why there is no such thing as an angle between points.

ANGLES ARE BETWEEN LINES. Lines are made up of points. Decide which lines you are talking about and you''ll suddenly be able to solve it with trig and right angle triangles.

Share this post


Link to post
Share on other sites
Mate i do understand i need 2 lines!
Sorry, i just need to figure out which and if 1 algorithm will solve my proble for 2 the 2nd object beed in either 4 sectors of my x,y

I have drew a pretty diagram to help things!!

http://www.2ndchancegames.co.uk/angles2.jpg

thanks again.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use the formula i gave above, where A is the line connecting the point to the origin, and B is the unit vector representing the y axis (0,1)

so you just solve

A.B = |A||B|cos(theta)

theta = inverse cosine( A.B / |A||B| )

simplify a little

theta = inverse cosine( AxBx + AyBy / sqrt(Ax^2 + Ay^2)*sqrt(Bx^2 + By^2))

since Bx is 0 and By is 1

theta = inverse cosine( Ay / sqrt(Ax^2 + Ay^2) )

plug in your values for Ax and Ay (which in this case are just the coordinates of the point) and solve.

Just to prove that it works, we can try it for the vector (1,0) .. which is the unit vector that represents the positive x axis

We have Ax = 1 and Ay = 0.. which gives use theta = inverse cosine(0) .. which has a solution of 90º, as expected.

Share this post


Link to post
Share on other sites
Do a search for polar coordinates.

They describe a point(r,theta). r is a distance from the orign(0,0) and theta is an angle between some ray(mostly the x-axis) and the point you want to plot.

With simple cos/sin functions you can find the angle you need

Just google

Share this post


Link to post
Share on other sites
Using AP's method, you'll get only one of two possible answers. In your example with the guy who's 8 feet behind and 3 feet to your right, you'll get an angle implifying that the guy is 8 feet in FRONT of you instead of behind. To find the other possible solution, subtract theta from 360:

thetaTwo = 360 - theta

[edited by - Goldfish on October 21, 2003 3:57:50 PM]

Share this post


Link to post
Share on other sites
Ok, here is what i have done. Get ready to cry !!
I need to refresh on dot product so thanks for the advice, but this works for me so far at the moment.
Please begin telling me how bad it is !!..


float AngleToPoint(cords MyPoint, cords DestPoint)
{
int extra = 0;

if((MyPoint.X <= DestPoint.X)&&(MyPoint.Y > DestPoint.Y))
{

float Opp = ( MyPoint.X - DestPoint.X );
float Adj = ( MyPoint.Y - DestPoint.Y );

Adj = Adj*(-1);
return RadiansToDegree(atan(Opp/Adj));
}
else if((MyPoint.X < DestPoint.X)&&(MyPoint.Y <= DestPoint.Y))
{
float Opp = ( MyPoint.Y - DestPoint.Y );
float Adj = ( MyPoint.X - DestPoint.X );

return (RadiansToDegree(atan(Opp/Adj))+90);
}
else if((MyPoint.X >= DestPoint.X)&&(MyPoint.Y < DestPoint.Y))
{

float Adj = ( MyPoint.Y - DestPoint.Y );
float Opp = ( MyPoint.X - DestPoint.X );

Opp = Opp*(-1);

return (RadiansToDegree(atan(Opp/Adj))+180);
}

else ((MyPoint.X > DestPoint.X)&&(MyPoint.Y >= DestPoint.Y))
{
float Opp = ( MyPoint.Y - DestPoint.Y );
float Adj = ( MyPoint.X - DestPoint.X );

Opp = Opp*(-1);
Adj = Adj*(-1);

return (RadiansToDegree(atan(Opp/Adj))+270);
}

}

Share this post


Link to post
Share on other sites
BlackEye,

Hey, just wanted to point out that there is a forum search feature, see the link in the upper right corner of this screen. Its okay to ask questions that are commonly asked (as this one is), but you may save yourself some time (and save gamedev some bandwidth) if you search the archives first. I''m certain this particular question was asked within the past week. It comes up very often, .

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Goldfish
Using AP''s method, you''ll get only one of two possible answers. In your example with the guy who''s 8 feet behind and 3 feet to your right, you''ll get an angle implifying that the guy is 8 feet in FRONT of you instead of behind. To find the other possible solution, subtract theta from 360:

thetaTwo = 360 - theta

<SPAN CLASS=editedby>[edited by - Goldfish on October 21, 2003 3:57:50 PM]</SPAN>


Well if you want to be pedantic about it.. There are an infinite number of solutions, 90, 270, 450, 630 .. etc

Share this post


Link to post
Share on other sites