Question about angles

Moderator
Posts: 682
Joined: 2002.11
Post: #1
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
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
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);
Quote this message in a reply
Moderator
Posts: 682
Joined: 2002.11
Post: #3
Thank you very much! METAL has the same command.

My web site - Games, music, Python stuff
Quote this message in a reply
Member
Posts: 114
Joined: 2002.08
Post: #4
Metal:


x3=x1-x2
y3=y1-y2

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((x1-x2)/(y1-y2))


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 this message in a reply
Member
Posts: 142
Joined: 2002.11
Post: #5
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 = (point1y-point2y) / (point1x-point2x)
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 this message in a reply
Member
Posts: 142
Joined: 2002.11
Post: #6
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 Rasp). Secondly, abs (if its like it is in C) returns an int so you're inadvertantly rounding your value.
Quote this message in a reply
Member
Posts: 114
Joined: 2002.08
Post: #7
Sometimes you will in fact end up with a negative number

x1=50
x2=60

x3=x1-x2

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."
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Squaring a negative number will give you a positive number. Square-rooting 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.
Quote this message in a reply
Member
Posts: 114
Joined: 2002.08
Post: #9
Correct
Blush

"Most nutritionists say that Twinkies are bad. But they're not, they're very very good."
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Angles and Matrices and Whatnot merrill541 5 3,467 Jun 2, 2009 04:13 AM
Last Post: unknown
  Finding Rotational Vectors and Angles Nick 9 4,735 Mar 25, 2005 12:14 AM
Last Post: tigakub