I am trying to get 4 Vector2 objects representing the 4 corners of a sprite to rotate around the sprite itself rotates around its center. With my following code, however, the Vector2 objects rotate around 0,0 in Client Space instead of rotating around the center of the object. Using matrix transformations, is there any way to rotate the Vector2 objects around the center of the object instead of the global coordinate (0,0)?
Here is the function for the rotation so far:
public Vector2[] CheckCollision()
{
//Get the 4 corners of the sprite locally
//We can get all 4 corners from only 2 vectors
Vector2 topLeft = new Vector2(position.X - spriteSize.X, position.Y - spriteSize.Y);
//Not sure why position is representing the
//bottom right instead of the center here....
Vector2 bottomRight = position;
Vector2 bottomLeft = new Vector2(topLeft.X, bottomRight.Y);
Vector2 topRight = new Vector2(bottomRight.X, topLeft.Y);
//Create transformation matrix
Matrix transform = Matrix.CreateRotationZ(MathHelper.ToRadians(this.direction)) *
Matrix.CreateScale(this.scale);
//Transform the vectors
topLeft = Vector2.Transform(topLeft, transform);
bottomRight = Vector2.Transform(bottomRight, transform);
bottomLeft = Vector2.Transform(bottomLeft, transform);
topRight = Vector2.Transform(topRight, transform);
Vector2[] vectorArray = new Vector2[4];
vectorArray[0] = topLeft;
vectorArray[1] = bottomRight;
vectorArray[2] = bottomLeft;
vectorArray[3] = topRight;
return vectorArray;
}
It would probably be a lot easier to just rotate the four corners first before adding the
spritePositionand add the spriteposition after the rotation and scaling has been performed.Just make your four corners into the corresponding combinations of
spriteSizeand do theVector2.Transformonce that is done add thespritePositionto the four vectors invectorArray