I have a Rectangle and a Vector2 position, my sprite is being drawn at this position. The rectangle is meant to be a collision system with the cursor, when I rotate my sprite around the origin and stuff in a draw call
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, null, Color.White, rot, Origin, SCALEFACTOR, SpriteEffects.None, 0f);
}
It works fine, my object is rotated, at it's centre, by Pi/2, or whatever I put in. However, rotating the rectangle isn't as easy, I read almost every stack overflow question on this problem and still haven't found a fix. I then followed a youtube guide, this is the resulting code for rotating a rectangle.
private void RotateItem(float rotation)
{
rot += rotation;
UpdateRectangle();
}
private void UpdateRectangle()
{
Vector2 tl = Vector2.Transform(Vector2.Zero, transform);
Vector2 tr = Vector2.Transform(new Vector2(scaledWidth, 0), transform);
Vector2 bl = Vector2.Transform(new Vector2(0, scaledHeight), transform);
Vector2 br = Vector2.Transform(new Vector2(scaledWidth, scaledHeight), transform);
Vector2 min = new Vector2(FindSmallest(tl.X, tr.X, bl.X, br.X), FindSmallest(tl.Y, tr.Y, bl.Y, br.Y));
Vector2 max = new Vector2(FindBiggest(tl.X, tr.X, bl.X, br.X), FindBiggest(tl.Y, tr.Y, bl.Y, br.Y));
rectangle = new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y));
}
private void UpdateTransformMatrix()
{
transform = Matrix.CreateTranslation(new Vector3(-Origin, 0))
* Matrix.CreateRotationZ(rot)
* Matrix.CreateTranslation(new Vector3(position, 0));
}
The width + height don't seem to change until I click on the box (which isn't even drawn properly over the sprite because Origin + and doesn't seem to rotate at all tbh)... I'll show you an example https://gyazo.com/c045b844f306a0ea25f48a0d45127f21
And this is my full draw code + including the rectangle's rectTex
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, null, Color.White, rot, Origin, SCALEFACTOR, SpriteEffects.None, 0f);
if (ShowRectangle)
spriteBatch.Draw(rectTex, rectangle, null, Color.White, rot, Vector2.Zero, SpriteEffects.None, 0f);
}