Jump to content
  • Advertisement
Sign in to follow this  

[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.

If you intended to correct an error in the post then please contact us.

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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!