Question about angles
If I have 2 points and I want to find out at what angle one point is to another, how would I do this? I'm only in Algebra I and I use METAL BASIC. I need to know this so I can have AI go towards a point on a screen.
My web site  Games, music, Python stuff
I don't know about METAL, but in C you'd do:
Code:
dx = p2.x  p1.x;
dy = p2.y  p1.y;
angle = atan2(dy, dx);
Metal:
x3=x1x2
y3=y1y2
hyp=sqr(abs(x3^2+y3^2))
then:
x4=(x3/hyp)*speed
y4=(y3/hyp)*speed
You can do it a little different using the "tan" abd "atn" functions, but it works in radians (i.e. 6.28=360 degrees ).
So:
angle=atn((x1x2)/(y1y2))
Then:
x4=cos(angle)*speed
y4=sin(angle)*speed
Pretty sure I got that right. Just did another round of it myself. It hard to grasp at first, but thereafer its like riding a bike. I had to learn it for "Microbian", and since it was due for uDevgames, I was in quite a press to get it out early.
x3=x1x2
y3=y1y2
hyp=sqr(abs(x3^2+y3^2))
then:
x4=(x3/hyp)*speed
y4=(y3/hyp)*speed
You can do it a little different using the "tan" abd "atn" functions, but it works in radians (i.e. 6.28=360 degrees ).
So:
angle=atn((x1x2)/(y1y2))
Then:
x4=cos(angle)*speed
y4=sin(angle)*speed
Pretty sure I got that right. Just did another round of it myself. It hard to grasp at first, but thereafer its like riding a bike. I had to learn it for "Microbian", and since it was due for uDevgames, I was in quite a press to get it out early.
"Most nutritionists say that Twinkies are bad. But they're not, they're very very good."
Quote:Originally posted by OneSadCookie
I don't know about METAL, but in C you'd do:
Code:
dx = p2.x  p1.x;
dy = p2.y  p1.y;
angle = atan2(dy, dx);
Does atan2 compensate for the limited range of inverse tangent? If it doesn't you'll find the code doesn't work. If you have problems, use my code:
[SOURCECODE]
Procedure findAngleForPoints(int point1x,int point1y,int point2x,int point2y)
float angle, tanangle
tanangle = (point1ypoint2y) / (point1xpoint2x)
if point1x < point2x
angle = invtan(tanangle)
else
angle = invtan(tanangle)+180
end if
end Proc (angle)
[/SOURCECODE]
This is in degrees btw. If metal uses radians, change the 180 to pi.
Quote:Originally posted by Dr. Light
Metal:
hyp=sqr(abs(x3^2+y3^2))
You shouldn't need to call abs(). First of all, x3^2 + y^2 will always be a positive number because any number squared will be positive (except maybe imaginary numers ). Secondly, abs (if its like it is in C) returns an int so you're inadvertantly rounding your value.
Sometimes you will in fact end up with a negative number
x1=50
x2=60
x3=x1x2
so x3=10
Sqauring a negative number will return "nan" known as "Not a Number". And, Your just using the hypotenuse to get the distance, so you don't have to worry about the negatives. abs() in METAL work like this abs(10.234)=10.234. The int() will do this int(10.234)=10.
I just ran into a pile of trouble with the "nan" problem, so I know that this is a correct way to do it without any problems.
x1=50
x2=60
x3=x1x2
so x3=10
Sqauring a negative number will return "nan" known as "Not a Number". And, Your just using the hypotenuse to get the distance, so you don't have to worry about the negatives. abs() in METAL work like this abs(10.234)=10.234. The int() will do this int(10.234)=10.
I just ran into a pile of trouble with the "nan" problem, so I know that this is a correct way to do it without any problems.
"Most nutritionists say that Twinkies are bad. But they're not, they're very very good."
Squaring a negative number will give you a positive number. Squarerooting a negative number will give you NaN.
atan2 compensates for the limited range of atan by using the signs of the arguments to determine the quadrant. The code I originally posted is straight from the atan2 man page's example on converting rectangular coordinates to polar.
atan2 compensates for the limited range of atan by using the signs of the arguments to determine the quadrant. The code I originally posted is straight from the atan2 man page's example on converting rectangular coordinates to polar.
Correct
"Most nutritionists say that Twinkies are bad. But they're not, they're very very good."
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Angles and Matrices and Whatnot  merrill541  5  3,703 
Jun 2, 2009 04:13 AM Last Post: unknown 

Finding Rotational Vectors and Angles  Nick  9  5,071 
Mar 25, 2005 12:14 AM Last Post: tigakub 