[XNA] Matrix Decomposition into SpriteBatch.Draw()

Started by
-1 comments, last by JoshuaBaker 12 years, 3 months ago
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.

This topic is closed to new replies.

Advertisement