Xna SpriteBatch Matrix.Decompose ()

What I want to do is make it transform the matrices into SpriteBatch. I have 2 sprites, parent and child, and the child needs to be scaled, rotated, translated relative to the parent.

I currently have an implementation using textured ATVs, but I think it should be possible using the built-in SpriteBatch class and using Matrix.Decompose (). I'm not sure how to pass decomposed values โ€‹โ€‹to SpriteBatch as soon as I decompose them.

I understand how to save the matrix stack, I'm just looking for an example of using the values โ€‹โ€‹obtained from Matrix.Decompose () in combination with SpriteBatch.

+4
source share
1 answer

I thought about it myself, finally. Most loans refer to this blog post .

You can use this method to decompose your matrix:

private void DecomposeMatrix(ref Matrix matrix, out Vector2 position, out float rotation, out Vector2 scale) { Vector3 position3, scale3; Quaternion rotationQ; matrix.Decompose(out scale3, out rotationQ, out position3); Vector2 direction = Vector2.Transform(Vector2.UnitX, rotationQ); rotation = (float)Math.Atan2((double)(direction.Y), (double)(direction.X)); position = new Vector2(position3.X, position3.Y); scale = new Vector2(scale3.X, scale3.Y); } 

Then you can build the transformation matrix so that your sheets can do this:

 Matrix transform = Matrix.CreateScale(new Vector3(this.Scale, 1.0f)) * Matrix.CreateRotationZ(this.Rotation) * Matrix.CreateTranslation(new Vector3(this.Position, 0)); 

For parent sprites, specify the source:

 Matrix transform = Matrix.CreateTranslation(new Vector3(-this.Origin, 0)) * Matrix.CreateScale(new Vector3(this.Scale, 1.0f)) * Matrix.CreateRotationZ(this.Rotation) * Matrix.CreateTranslation(new Vector3(this.position, 0)); 

Multiply all matrices on the stack in reverse order.

+4
source

Source: https://habr.com/ru/post/1302458/


All Articles