Game Development Community

dev|Pro Game Development Curriculum

Improved getAnglesFromVector

by Jay Barnson · 09/02/2005 (5:24 pm) · 13 comments

The "getAnglesFromVector" function sacrifices accuracy for speed. This isn't a problem if a rough estimate is all you need - but it's horribly noticeable if you are using it to, say, point a camera at a moving target.

The problem is that it the pitch calculation doesn't use the true horizontal distance - it just takes takes the x or y component, whichever is more significant. The end result is that you get kind of an octagonal approximation of pitch, not a true smooth pitch calculation.

So, for your enjoyment, here's a new, improved - somewhat slower but more accurate version. If you want to keep access to the old version, just rename this to "newGetAnglesFromVector" or something.

This change goes into mathUtils.cc, at approxmately line 80:

void getAnglesFromVector( VectorF &vec, F32 &yawAng, F32 &pitchAng )
{
    yawAng = mAtan( vec.x, vec.y );

    if( yawAng < 0.0 )
    {
         yawAng += M_2PI;
    }

    // Begin modified code

    F32 horizontalValue, verticalValue;
    F32 dist = mSqrt((vec.x * vec.x) + (vec.y * vec.y));
    horizontalValue = 1.0f;
    verticalValue = vec.z / dist;
    pitchAng = mAtan( fabs(verticalValue),fabs(horizontalValue));

    // End Code Modification

    if( vec.z < 0.0 )
    {
        pitchAng = -pitchAng;
    }

}

About the author

Jay has been a mainstream and indie game developer for a... uh, long time. His professional start came in 1994 developing titles for the then-unknown and upcoming Sony Playstation. He runs Rampant Games and blogs at Tales of the Rampant Coyote.


#1
09/02/2005 (8:55 pm)
OMG this may be the thing I need for the annoying little problem with the AITurrets with the Turret Resource.
#2
09/02/2005 (9:08 pm)
Little soon to say it solved my problem but it looks damn promising. I will test further on Tues.

Thanks big time!
#3
09/07/2005 (1:12 pm)
So? Did it work?
#4
09/07/2005 (1:20 pm)
Oh yes. Sorry. I just got to doing some stuff and forgot. The turret Resource uses getAnglesFromVector(). It was as you described throwing off the pitch. Thank you very much.
#5
09/08/2005 (9:49 pm)
Why are you declaring horizontalValue as 1.0f then running fabs on it? Why not just replace any usage of horizontalValue with 1.0f?
#6
09/09/2005 (6:26 am)
I apologize for not submitting this patch 2 years ago.

The updated turret resource has had this fix in it for a while though...
#7
09/12/2005 (1:49 pm)
Hmm our team must have implemented the old version.
#8
10/13/2005 (12:30 pm)
Huh - Mike K., I think that's kruft from something else I was doing when I was playing with this function. There's no reason to call fabs on horizontalValue.
#9
09/08/2007 (11:08 pm)
I just spent about three hours tracking down this bug and when it came down to this function I don't have the slightest clue how to fix it. Luckily I'm good with a search engine and very quickly found the answer. My camera is working perfectly now.

I have Torque 1.5.2 why hasn't this been implemented yet???
#10
09/29/2007 (8:02 pm)
works fine for TGEA 1.0.3 too.
if you are still around... thankyou Jay.
appreciated.
#11
10/13/2007 (5:56 pm)
Wonderful, used this with TGEA 1.03 fixed the issue I was having with a client side camera system.
#12
12/15/2007 (7:13 am)
That's a really good fix
But why are you using fabs(verticalValue)? That is actually removing the sign information that the last if (...) is correcting. Since mAtan is referencing the system atan2, you can remove the fabs in the mAtan args and then remove the if (vec.z) at the end.
#13
03/15/2008 (3:30 pm)
Is anybody else finding that this causes Torque to crash with the turret resource?