Skip to main content
gamedev.net
🔒 Locked

Question

Started by nickvbs Dec 22, 2002 at 4:55 AM 3 replies 450+ views
Original Post
nickvbs
nickvbs
Im using the formula to calculate angles of a point r = sqrt(x^2 + z^2) arcsin(z/r) arccos(x/r) Question::How would u calculate the angle if u moved the origins position? U would be calculating the angle of a point based on another point. Imagine this...(my game) enemys are chasing u on foot. To make them face the correct direction there running, u have to rotate them with the correct angle facing the players location. I had to do alot of other added math to make that formula work in the game but it only worked at the origin(0,0,0). Some help would be great! thanx
rmsgrey
rmsgrey
Simplest solution is to move the origin... If you want (xx,yy,zz) to be the origin, then moving the universe (or just the points you''re working with) by subtracting that vector from everything will do it - in other words, replace x with x-xx, y with y-yy, and z with z-zz.
BrennendeKomet
BrennendeKomet
rmsgrey is right. But, actually you don''t have to move the universe You see, while calculationg angles, say arccos(x/r), you must use RELATIVE x and RELATIVE r, but not ABSOLUTE x and ABSOLUTE r! The reason, is that you calculate the angle of bad guy''s rifle "relatively" to the target!

Say bad guy''s coordinate is given by a vector B, while target has vector R. Then, the relative position of bad guy to target is given by B-R, while that of target relatively to bad guy by R-B. That''s all

Good Luck!
C++ RULEZ!!! :)
nickvbs
nickvbs
thanks BrennendeKomet..that really helped

Also..when calculating the arc_cos and arc_sin...they only return the correct angle if its between -pi/2 and pi/2
If Im solving a problem by hand with arc_cos or arc_sin I automatically know what the real angle is but what is a good way to check if it is correct in coding?

if(tan(theta) == z/x) ??

thanks in advance
grhodes_at_work
grhodes_at_work
Arcsin would give a value between -pi/2 and pi/2 (1st and 4th quadrants) while arccos would give a value between 0 and pi (1st and 2nd quadrants). These methods don''t represent angles in the same set of quadrants, and you could use some logic to get the correct angle:

if (sin < 0 && cos < 0) then adjust angle to be in 3rd quadrant
else if (sin > 0 && cos < 0) then 2nd quadrant
else if (sin > 0 && cos > 0) then 1st quadrant
else if (sin < 0 && cos > 0) then 4th quadrant

But why go through the trouble? Why not just use atan2 instead of arcsin and arccos? atan2 gives the angle between -pi and pi, and always gives an angle in the correct quadrant. So, using atan2,

angle = atan2(z, x);

Unless you''re trying to represent a winding count, any angle outside the range -pi to pi represents the exact same orientation as the given angle between -pi and pi, so the number given by atan2 should always be good.


Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

Topic Locked

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

Sign in to reply to this topic.