Sign in to follow this  
thestonefox

Problem with rotating 2d rectangle with a matrix

Recommended Posts

Hi all, I'm trying to work out my new bounding box when I rotate my sprite however, my matrix rotation for my current bounding box seems to be playing up, it seems to work when rotating from 0 to 90 degrees but then goes weird between 180 to 270 (then ok again back at 360/0) here is an example. *=sprite image .=my debug image that uses the bounding box to draw at 0 degrees rotation the items are painted as ** .. ** .. ** .. ** .. at 90 degrees **** .... **** .... Now it goes weird at 180 and looks like this ** .... ** .... ** ** As you can gather, my image has rotated correctly but my bounding box is still at a 90degree rotation Here is my code for rotation: public void SetRotation(float degrees, bool radian) { if (radian) { rotation = degrees; } else { rotation = Helper.ChangeRotationByDegrees(degrees); } rotationMatrix = Matrix.CreateTranslation(new Vector3(-center, 0.0f)) * // Matrix.CreateScale(block.Scale) * would go here Matrix.CreateRotationZ(rotation) * Matrix.CreateTranslation(new Vector3(position, 0.0f)); // Calculate the bounding rectangle of this block in world space Rectangle blockRectangle = CalculateBoundingRectangle(GetBounds(), rotationMatrix); width = blockRectangle.Width; height = blockRectangle.Height; center = new Vector2(width / 2, height / 2); if (this.GetType().Name.Equals("Player")) { Console.WriteLine(rotationMatrix); Console.WriteLine("rotation:" + Helper.GetRotationByRadians(rotation) + " block:" + blockRectangle + " width:" + width + " height:" + height + " bounds:" + GetBounds()); } } Here is my code for getting my bounding box public Rectangle GetBounds() { //Ensure the bounds are top, left, width, height and not the center of the object Rectangle bounds = new Rectangle((int)(position.X - center.X), (int)(position.Y - center.Y), (int)width, (int)height); return bounds; } And here is the calculateboundingrectangle code taken from the XNA pixel perfect tutorial code public Rectangle CalculateBoundingRectangle(Rectangle rectangle, Matrix transform) { // Get all four corners in local space Vector2 leftTop = new Vector2(rectangle.Left, rectangle.Top); Vector2 rightTop = new Vector2(rectangle.Right, rectangle.Top); Vector2 leftBottom = new Vector2(rectangle.Left, rectangle.Bottom); Vector2 rightBottom = new Vector2(rectangle.Right, rectangle.Bottom); // Transform all four corners into work space Vector2.Transform(ref leftTop, ref transform, out leftTop); Vector2.Transform(ref rightTop, ref transform, out rightTop); Vector2.Transform(ref leftBottom, ref transform, out leftBottom); Vector2.Transform(ref rightBottom, ref transform, out rightBottom); // Find the minimum and maximum extents of the rectangle in world space Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop), Vector2.Min(leftBottom, rightBottom)); Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop), Vector2.Max(leftBottom, rightBottom)); // Return that as a rectangle return new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y)); } Here is the output from me running the rotation code { {M11:1 M12:0 M13:0 M14:0} {M21:0 M22:1 M23:0 M24:0} {M31:0 M32:0 M33:1 M34:0} {M41:-32 M42:-64 M43:0 M44:1} } rotation:0 block:{X:-64 Y:-128 Width:64 Height:128} width:64 height:128 bounds:{X:-32 Y:-64 Width:64 Height:128} { {M11:7.54979E-08 M12:1 M13:0 M14:0} {M21:-1 M22:7.54979E-08 M23:0 M24:0} {M31:0 M32:0 M33:1 M34:0} {M41:64 M42:-32 M43:0 M44:1} } rotation:90 block:{X:0 Y:-64 Width:128 Height:64} width:128 height:64 bounds:{X:-64 Y:-32 Width:128 Height:64} { {M11:-1 M12:1.509958E-07 M13:0 M14:0} {M21:-1.509958E-07 M22:-1 M23:0 M24:0} {M31:0 M32:0 M33:1 M34:0} {M41:288 M42:256 M43:0 M44:1} } rotation:180 block:{X:0 Y:0 Width:128 Height:64} width:128 height:64 bounds:{X:160 Y:192 Width:128 Height:64} { {M11:1.192488E-08 M12:-1 M13:0 M14:0} {M21:1 M22:1.192488E-08 M23:0 M24:0} {M31:0 M32:0 M33:1 M34:0} {M41:192 M42:288 M43:0 M44:1} } rotation:270 block:{X:384 Y:0 Width:64 Height:127} width:64 height:127 bounds:{X:192 Y:160 Width:64 Height:127} { {M11:1 M12:-3.019916E-07 M13:0 M14:0} {M21:3.019916E-07 M22:1 M23:0 M24:0} {M31:0 M32:0 M33:1 M34:0} {M41:192 M42:160.5 M43:0 M44:1} } rotation:360 block:{X:384 Y:320 Width:64 Height:127} width:64 height:127 bounds:{X:192 Y:160 Width:64 Height:127} As you can see the rotation works fine between 0 and 90, but then when it goes to 180 the bounding box doesnt change size. anyone got any ideas why my matrix isnt rotating my rectangle correctly? P.S. I've also noticed that my height of my block is being robbed of 1 pixel, as you can see it goes from 128 to 127

Share this post


Link to post
Share on other sites

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