# [XNA] Matrix Decomposition into SpriteBatch.Draw()

This topic is 2844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am working on building a 2D hierarchical Scene Manager, something simple that lets the position rotation and scale of a node effect its children.

I trying to do this using SpriteBatch, but SpriteBatch does not allow me to get it a matrix unless i call a new SpriteBatch.Begin() for each node, which i believe is not ideal in any way. I decided to try and use the Draw command to do this.

Each node has this property. [color=blue]
 public Matrix Transform { get { if (Parent !=null) {returnParent.Transform * Matrix.CreateScale(new Vector3(Scale, 1.0f)) * Matrix.CreateRotationZ(Rotation) *Matrix.CreateTranslation(new Vector3(Position, 0)); } else { return Matrix.CreateScale(new Vector3(Scale, 1.0f)) * Matrix.CreateRotationZ(Rotation) * Matrix.CreateTranslation(Vector3(Position,0)); } } } 

When a node gets its transform, it gets its parent transform, and then changes it by its own transforms.

Then when i do a draw command
 Vector3 pos; Vector3 scale; Quaternion rot; Transform.Decompose(out scale, out rot, out pos); Vector2 direction = Vector2.Transform(Vector2.UnitX, rot); float rotation = (float)Math.Atan2((double)(direction.Y), (double)(direction.X)); spriteBatch.Draw(Texture, new Vector2(pos.X, pos.Y), Source, Color, rotation, Origin, new Vector2(scale.X, scale.Y), SpriteEffects.None, 0.0f); 

It decomposes the matrix into its parts and draws.
This works ok for position and when i rotate a parent it rotates the child alright. The problem is when i rotate a child it does not rotate at its origin (which should be the center of the image), it rotates around 0,0.

Any insight is appreciated on how do go about doing this.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 15
• 9
• 11
• 15