title: How to use Transformation Matrix for SpriteBatch correctly?
body: <p>I'm new to XNA and would like to develop a light-weight 2D engine over it, with the entities organized into parent-child hierarchy. I think of matrix when drawing children, because their position, rotation and scale are depend on their parent.</p>
  
  <p>If I use <code>SpriteBatch.Begin()</code>, my rectangles can be drawn on the screen, but when I change them into:</p>
  
  <pre><code>        this.DrawingMatrix = Matrix.Identity;
          this.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullClockwise, null, this.DrawingMatrix);
  </code></pre>
  
  <p>nothing is drawn anymore. I even tried <code>new Matrix()</code> or <code>Matrix.CreateTranslation(0, 0, 0)</code> for <code>DrawingMatrix</code>.</p>
  
  <p>My first question is: why doesn't it work? I'm not working with any camera or viewport.</p>
  
  <p>Secondly, before drawing an entity, I call the <code>PreDraw</code> to transform the matrix (I will then reset to original state at <code>PostDraw</code>):</p>
  
  <pre><code>    protected virtual void PreDraw(Engine pEngine)
      {
          pEngine.DrawingMatrix *=
              Matrix.CreateTranslation(this.X, this.Y, 0) *
              Matrix.CreateScale(this.ScaleX, this.ScaleY, 1) *
              Matrix.CreateRotationZ(this.Rotation);
      }
  </code></pre>
  
  <p>Please clarify the correction of above code. And I need to scale not at the origin, but at <code>ScaleCenterX</code> and <code>ScaleCenterY</code>, how can I achieve this?</p>
  
  <p><strong>ADDED</strong>: Here is an example of my engine's draw process:</p>
  
  <ol>
  <li><p>Call these code:</p>
  
  <pre><code>    this.DrawingMatrix = Matrix.CreateTranslation(0, 0, 0);
      this.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullClockwise, null, this.DrawingMatrix);
  </code></pre></li>
  <li><p>Call <code>PreDraw()</code>, with is:</p>
  
  <pre><code>protected virtual void PreDraw(Engine pEngine)
  {
      pEngine.DrawingMatrix *=
          Matrix.CreateTranslation(this.X, this.Y, 0) *
          Matrix.CreateScale(this.ScaleX, this.ScaleY, 1) *
          Matrix.CreateRotationZ(this.Rotation);
  }
  </code></pre></li>
  <li><p>Call <code>Draw()</code>, for example, in my <code>Rect</code> class:</p>
  
  <pre><code>protected override void Draw(Engine pEngine)
  {
      pEngine.SpriteBatch.Draw(pEngine.RectangleTexture, new Rectangle(0, 0, (int)this.Width, (int)this.Height), new Rectangle(0, 0, 1, 1), this.Color);
  }
  </code></pre></li>
  </ol>
  
  <p>If I replace above <code>Begin</code> code with <code>this.SpriteBatch.Begin()</code>, the rectangle is drawn correctly, so I guess it is because of the matrix.</p>