Sign in to follow this  

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

This topic is 4715 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

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]

Share this post


Link to post
Share on other sites
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[i] < min[i])
dist += (min[i] - C[i]) * (min[i] - C[i]);
else if (C[i] > max[i])
dist += (C[i] - max[i]) * (C[i] - max[i]);
}

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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4715 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this