2

I'm building a framework for XNA where users can use the hierarchy pattern to manage the display objects, meaning the transformation matrices of parents are applied to the children as well.

Users could set translation, scale and rotation, which are internally converted into a matrix when combining the transformations and drawing (users were also able to retrieve the transformation matrix).

The SpriteBatch.Draw() method doesn't have any overloads that has a matrix as a parameter, but I could just use Matrix.Decompose() to get the necessary components out of the concatenated matrix.

Now the problem is, I decided to allow users to directly set the matrix of individual objects (instead of it being read-only), meaning users can apply more complex transformations such as skewing. However a matrix with such transformation can't be decomposed into translation, scale and rotation, meaning I can no longer pass the transformation information to the SpriteBatch.Draw() method.

The only solution I've found so far, is to call SpriteBatch.Begin() for every individual object, passing its concatenated matrix as a parameter. I'm sure this isn't a usable solution at all, since sprite batch wouldn't be a batch anymore if I begin and end the batch for every call to Draw().

tl;dr: How can I apply a transformation matrix rather than components to each 2D object, without having to call SpriteBatch.Begin() for every object?

Edit: I do not want to settle with my solution of calling Begin() and End() for every object drawn on the screen.

A. A. Ron
  • 41
  • 1
  • 6
  • I'm using MonoGame (which is a successor to XNA) and I suppose that breaking sprite batch is your only option. It will slow down the drawing considerably though. You may try to extract the elements with "custom" matrices and group all unaffected elements in a single sprite batch. Also I'll flag your question as a duplicate because after a quick search I've found the same question (if I understand it right): http://stackoverflow.com/questions/30460072/monogame-xna-transform-matrix-for-a-single-object – Sergey.quixoticaxis.Ivanov Feb 20 '17 at 16:39
  • Possible duplicate of [Monogame XNA transform matrix for a single object?](http://stackoverflow.com/questions/30460072/monogame-xna-transform-matrix-for-a-single-object) – Sergey.quixoticaxis.Ivanov Feb 20 '17 at 16:40

0 Answers0