How to extract the angle of rotation of a 2x2 matrix?

Started by
3 comments, last by Riddle 19 years, 3 months ago
Hello guys! I have two unsolved questions. A) How can I test if an axis aligned recteangle and a circle intersect? My approch returns false if the circle is inside the recteangle. C#-Code.( Generic, so I have to use functions( calculator ) for numeric operations.

        #region Intersects( Circle<Type, Calc> circle, Vector2<Type, Calc> recPosition )

        /// <summary> Tests if the given circle intersects the rec. </summary>
        /// <param name="circle"></param>
        /// <param name="recPosition"> Position of the rec. </param>
        /// <returns> True if they intersect, else false. </returns>
        public bool Intersects( Circle<Type, Calc> circle, Vector2<Type, Calc> recPosition )
        {
            Type x0 = c.Sub( recPosition.x, circle.Position.x );
            x0 = c.Mul( x0, x0 );

            Type x1 = c.Add( recPosition.x, c.Sub( Width, circle.Position.x ) );
            x1 = c.Mul( x1, x1 );

            Type y0 = c.Sub( recPosition.y, circle.Position.y );
            y0 = c.Mul( y0, y0 );

            Type y1 = c.Add( recPosition.y, c.Sub( Height, circle.Position.y ) );
            y1 = c.Mul( y1, y1 );


            if( c.IsLess( c.Sqrt( c.Add( x0, y0 ) ), circle.Radius ) )
                return true;

            if( c.IsLess( c.Sqrt( c.Add( x0, y1 ) ), circle.Radius ) )
                return true;

            if( c.IsLess( c.Sqrt( c.Add( x1, y0 ) ), circle.Radius ) )
                return true;

            if( c.IsLess( c.Sqrt( c.Add( x1, y1 ) ), circle.Radius ) )
                return true;

            return false;
        }

        #endregion Intersects( Circle<Type, Calc> circle, Vector2<Type, Calc> recPosition )



B) How can I calculate the angle to rotate an object from the identity matrix to the object's orientation? ( 2x2 matrix ) Is this correct:

        #region Angle

        /// <summary> Returns the angle to the Idendity-Matrix. </summary>
        /// <returns></returns>
        public Type Angle
        {
            get
            {
                return calc.Acos( calc.Div( m00, calc.Sqrt( 
                       calc.Add( calc.Mul( m00, m00 ), calc.Mul( m01, m01 ) ))));
            }
        }

        #endregion Angle



Thank you very much! :) [Edited by - Riddle on January 19, 2005 1:00:40 PM]
Advertisement
Question A:

This has been discussed recently, along with code and an explanation of the algorithm. But with search down I'm not sure how you'd find it. So:

bool IntersectCircleAABB(const Vector2& C, float r, const Vector2& min, const Vector2& max){    float dist = 0.0f;        for (int i = 0; i < 2; i++)    {        if (C < min)            dist += (min - C) * (min - C);        else if (C > max)            dist += (C - max) * (C - max);    }        return dist <= r * r;}


That's off the top of my head, so it's not guaranteed to be error-free.

Question B:

This question probably has an easy answer, but looking at your code I'm not sure what your terms mean. Do you mean that given a 2x2 rotation matrix in 2D, you want to extract the angle of rotation?
A:
Thank you very much! Works wonderfully! :)

B:
Yes, the amount I have to rotate an object ( Eg. with glRotate() ) from the original Data( Identity-Matrix ) to the rotated Matrix of the Object. ( 2x2 Matricen )

Help please! :)
Thank you very much!
Rotation matrix for conterclockwise rotation(y to up, x to right) is
a b
c d
where
a=cos(angle),b=-sin(angle)
c=sin(angle),d=cos(angle)


So you can get angle using just
atan2(c,a)

You might need to flip sign if you want clockwise rotation.
Thank you. :)

My version and your version return exactly the same but different signs. Your version seems to be faster! :)


Everyting works fine now. I have solved all other problems regarding matricen.

This topic is closed to new replies.

Advertisement