Advertisement

angle between 2d points

Started by February 25, 2003 12:47 PM
3 comments, last by venomelektro 22 years ago
Hello, I want to find the angle than exists between 2 2d points , I already have the distance but I dont find how to calculate the angle dx = x1 - x2; dz = z1 - z2; di = (float)sqrt(dx*dx+dz*dz); I tryed angle = (dz<0) ? ( (float) ( ( acos(dx/di)/3.1415 ) * 180.0f ) + 90.0f ) : ((float)((acos(dx/di)/3.1415)*180.0f)-90.0f ) but I ''ve not right ( xcuse for my poor math ...) Anyone can help ? Thanx
Two points don''t make an angle... If you ment the angle between the line that''s defined by the two angles and another line, you need another line. For the x and y axis this is simple:

tan(angle_with_x_axis) = (y2-y1)/(x2-x1) and
tan(angle_with_y_axis) = (x2-x1)/(y2-y1)

for two lines I think there is a more efficient formula then this one, but here is one posibility:

tan(angle_of_two_lines) =
(y12-y11)/(x12-x11) - (y22-y21)/(x22-x21)

where the first index is the line and the second the point.

hope I helped

Marty
_____ /____ /|| | || MtY | ||_____|/Marty
Advertisement
There is no angle between 2 points.

You need at least three points for an angle.

Unless you''re trying to get an angle between two points from some reference point...

              *(point 2)             /             /           /          / (point1)*-----------*(point we are measuring from)


You''re going to have to explain what you are trying to do before I can help you out...
~~~~~Screaming Statue Software. | OpenGL FontLibWhy does Data talk to the computer? Surely he's Wi-Fi enabled... - phaseburn
ok, thanx for replies


I '' m building a 2d game ,

I have one ennemy with 2d position (x,y) and a player with (x,y) coordinates too

I calculate the distance between the two objects and If it is less than a constant value ,

the ennemy begin shooting the player ,

and this momment I need to set an angle so I can move the shoot throw a line between ennemy and player

all the shoots goes each frame like this :

tir[loop].x += (float) speed * (float)sin( tir[loop].angle *0.0174532925f);

tir[loop].y += (float) speed * (float)cos( tir[loop].angle *0.0174532925f);


Just need to find the way to calculate angle


Do you want to find an angle between a line passin trough two points and a horizontal line.

That's basic trigonometry

If points are :
P1 (x1,y1) and
P2 (x2,y2) then

Angle = arctan ( (y2-y1)/(x2-x1) )



[edited by - A Guy from CRO on February 25, 2003 4:41:14 PM]
-----------------Always look on the bright side of Life!

This topic is closed to new replies.

Advertisement