Vector angle: why this code?

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Vector angle: why this code?

Post by Klaim »

From https://bitbucket.org/sinbad/ogre/src/4 ... eVector2.h :

Code: Select all

/**      Gets the oriented angle between 2 vectors.
                @remarks
                        Vectors do not have to be unit-length but must represent directions.
                        The angle is comprised between 0 and 2 PI.
                */
                inline Ogre::Radian angleTo(const Ogre::Vector2& other) const
                {
                        Ogre::Radian angle = angleBetween(other);
                
                        if (crossProduct(other)<0)                      
                                angle = (Ogre::Radian)Ogre::Math::TWO_PI - angle;               

                        return angle;
                }
Can someone explain to me what does this code and why it is necessary?

Code: Select all

if (crossProduct(other)<0)                      
                                angle = (Ogre::Radian)Ogre::Math::TWO_PI - angle;    
Because I reported this function in a project I m working on and if I dont comment those line I got inversed angle values when other.x > 0 ...

I suppose this is easy geometry but I cant figure what the hell this bit of code is doing to this angle.
User avatar
Waruck
Goblin
Posts: 210
Joined: Mon Dec 12, 2011 12:52 pm
Location: Germany
x 34

Re: Vector angle: why this code?

Post by Waruck »

the Function angleBetween() returns a value between 0 and pi (exakt opposit) because it is not directed.
so on a clock if a points to 12, b points to 3 and c points to 9 then a.angleBetween(b) would be the same as a.angleBetween(c): pi/2.

angleTo() returns a value between 0 and 2pi (complete circle) because the angle is directed (right-handed). so in the above example a.angleTo(b) would still be pi/2 but a.angleTo(c) would be 3/2 pi. The check crossProduct()<0 checks if the angle is suppost to be larger than pi (this is due to the definition of the cross product)
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Vector angle: why this code?

Post by Klaim »

Thanks it's more clear now!
Post Reply