
[ad_1]
I’ve a Rectangle and a Vector2 place, my sprite is being drawn at this place. The rectangle is supposed to be a collision system with the cursor, once I rotate my sprite across the origin and stuff in a draw name
public digital void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, place, null, Color.White, rot, Origin, SCALEFACTOR, SpriteEffects.None, 0f);
}
It works superb, my object is rotated, at it is centre, by Pi/2, or no matter I put in.
However, rotating the rectangle is not as straightforward, I learn nearly each stack overflow query on this drawback and nonetheless have not discovered a repair. I then adopted a youtube information, that is the ensuing code for rotating a rectangle.
non-public void RotateItem(float rotation)
{
rot += rotation;
ReplaceRectangle();
}
non-public void ReplaceRectangle()
{
Vector2 tl = Vector2.Transform(Vector2.Zero, remodel);
Vector2 tr = Vector2.Transform(new Vector2(scaledWidth, 0), remodel);
Vector2 bl = Vector2.Transform(new Vector2(0, scaledHeight), remodel);
Vector2 br = Vector2.Transform(new Vector2(scaledWidth, scaledHeight), remodel);
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));
}
non-public void ReplaceTransformMatrix()
{
remodel = Matrix.CreateTranslation(new Vector3(-Origin, 0))
* Matrix.CreateRotationZ(rot)
* Matrix.CreateTranslation(new Vector3(place, 0));
}
The width + top do not appear to vary till I click on on the field (which is not even drawn correctly over the sprite as a result of Origin + and does not appear to rotate in any respect tbh)… I’ll present you an instance https://gyazo.com/c045b844f306a0ea25f48a0d45127f21
And that is my full draw code + together with the rectangle’s rectTex
public digital void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, place, null, Color.White, rot, Origin, SCALEFACTOR, SpriteEffects.None, 0f);
if (PresentRectangle)
spriteBatch.Draw(rectTex, rectangle, null, Color.White, rot, Vector2.Zero, SpriteEffects.None, 0f);
}
[ad_2]