Skip to main content
GameDev.net gamedev.net
🔒 Locked

Angles from vectors

Started by bjmumblingmiles Feb 20, 2003 at 7:07 PM 7 replies 400+ views
Original Post
bjmumblingmiles
bjmumblingmiles
I posted two days ago about using atan and asin...and I thought I had it working but im still not doing something right. I havent had very much math, I am in my first geometry class now. So can someone tell me the best way to take a point A and a point B in 2d space and find the angle of the vector from A to B? I''m doing this for a simple tracking AI, one that takes a target and steers towards it. any help would be appreciated. Brian
Brian J
Ronin Magus
Ronin Magus
Try using the dot product of the vectors.

From 3d math primer for graphics and game development, a book I HIGHLY recommend:

quote:

p. 59

The dot product is related to the angle between two vectors. The dot product is equal to the product of the magnitudes of the vectors and the cosine of the angle between the vectors.:

a dot b = ||a|| * ||b|| * cos(theta)

Solving for theta:

theta = arccos((a dot b) / ||a|| * ||b||)



Now, to do that math, know that ||v|| is the magnitude of the vector v. The magnitude in 2d math is basically sqrt(pow(x, 2) + pow(y, 2)), better known as the distance formula. There are two vectors in these equations, a and b.

To take the dot product of two vectors a and b (written as a dot b), take the sum of the products of the corresponding elements in the vectors.

a = {1, 2}
b = {3, 4}

a dot b = (1 * 3) + (2 * 4) = 11

I hope I haven't given completely wrong advice, please someone correct me if I'm wrong!


aut viam inveniam aut faciam

MoonStar Projects

[edited by - Ronin Magus on February 20, 2003 8:21:23 PM]
bjmumblingmiles
bjmumblingmiles
I hate to be annoying, but I really want to solve the problem with atan! I just dont understand why it wont work...what I''m seeing now is that it works, but only for 45 degrees of Quadrant I...heres my code:

nTargetAngle = RAD_TO_DEG(atanf(sx/-sy));

RAD_TO_DEG is just a simple macro that multiplies the result by 180/pi

Brian
Brian J
Bashar
Bashar
tan(x) = sin(x)/cos(x)
or
tan(x) = opp/adjacent, so I presume your sx and sy are are difference in the x and y values, respectively.

do this...given the points A and B:

opp = B.y - A.y
adj = B.x - A.x
angle = RAD_2_DEG(atan(opp/adj))

Now, the angle returned should be in the 1st quadrant, or 0 < angle < 90 degrees, so you''ll have to watch for the sign of both opp and adj to know what quadrant you''re in.

Seeing as how you''re taking your first geometry class, you must never have touched trigonmetry. Either way, I applaud your efforts...

Hope that helps, best of luck
People fear what they don''t understand, hate what they can''t conquer!
bjmumblingmiles
bjmumblingmiles
I''m doing that exactly, and its giving me the following values as I move the two points: 1, skips to 45, 63-90...its skipping a whole lot. any ideas? is my triangle backwards maybe?

brian
Brian J
bjmumblingmiles
bjmumblingmiles
Every 2d game I''ve played has has some sort of following/tracking algorithm...how can this be so hard? I cant find any information on it either. If anyone knows anything please help me, this is driving me nuts!

Brian
Brian J
CProgrammer
CProgrammer
This is one possible way to get the rotation:


roty = atan(((x1-x2)/(z1-z2)))*(180.0f/D3DX_PI);
if(z2 > z1)
{
roty+=180;
}


-CProgrammer
My Website
bjmumblingmiles
bjmumblingmiles
CP, shouldnt those z''s be y''s? if not, i''m in 2d, and I have no z''s in the equation...

Brian
Brian J
CProgrammer
CProgrammer
Sorry didn''t know it was 2s. However you can just exchange the z''s with y''s. My code example is actually 2d just as you want it. I just used it in a 3d app with a perspective from the top. I used it to rotate the NPC''s so they look at the player and offcourse follow him(a little extra code for that) when the player gets too close.

So use this to rotate an object to make it look at another object in your 2d-game. Making the object follow should be easy having the angle.

rot = atan(((x1-x2)/(y1-y2)))*(180.0f/D3DX_PI);
if(y2 > y1)
{
rot+=180;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.