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

*ignore me* i figured it out

This topic is 5112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

my trig is very rusty. I need to be able to calculate the degrees of an angle between known points on a graph. (for making characters face a specific direction in degrees in a game, for example, character a is at 100,500 and character b is at 500,750 assuming 0 degrees is looking straight down the positive x axis, how many degrees does character a need to turn from 0 to face character b? ). I have this formula, but i'm not sure what syntax to use for visual C, or if its really even the formula I need. even if you dont know the syntax, I'd still like to know the right formula. degrees= cos-1(ux*vx+uy*vy/|u|*|v|) ?? please help thanks! NM i figured it out [edited by - deanx2578 on December 13, 2003 6:18:26 PM]

Share this post

Link to post
Share on other sites

angle = cos-1( U . V / (|U| . |V|));

angle is in radians.

your formula is mostly correct (convert degrees to angles), but it will give you an angle in the range [0, 180].

there is a better way

radians = tan-1(u ^ v, u * v);

u ^ v is the cross product
u * v is the dot product

u ^ v = (u.x*v.y - u.y*v.x);
u * v = (u.x*v.x + u.y*v.y);

so, in c++,

float degrees = ((float) atan2(ux*vy - uy*vx, ux*vx + uy*vy)) * (180.0f / pi);

atan2 takes care of finding which quadrant to use, where acos() doesn''t.

Share this post

Link to post
Share on other sites
thanks. thats pretty much how i did it except i had to alter it to this for my program.

hero[0].dir= abs(atan2(hero[0].world_pos_z - hero[hero[0].target].world_pos_z, hero[0].world_pos_x - hero[hero[0].target].world_pos_x)*180/PI-180);

Share this post

Link to post
Share on other sites