Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to make a 2D engine for XNA with SpriteBatch. My object unit is Entity, which can include other Entities as its children. However, when drawing them, the rotation and scaling stacking do not work well.

Here is my Entity code:

    protected virtual void PreDraw(Engine pEngine)

        { // Transform Matrix
            // Translate
            pEngine.DrawingMatrix *= Matrix.CreateTranslation(this.X, this.Y, 0);

            { // Scale
                float translateX = this.ScaleCenterY + this.X;
                float translateY = this.ScaleCenterX + this.Y;

                pEngine.DrawingMatrix *= Matrix.CreateTranslation(-translateX, -translateY, 0);
                pEngine.DrawingMatrix *= Matrix.CreateScale(this.ScaleX, this.ScaleY, 0);
                pEngine.DrawingMatrix *= Matrix.CreateTranslation(translateX, translateY, 0);

            { // Rotation
                float translateX = this.RotationCenterX + this.X;
                float translateY = this.RotationCenterY + this.Y;

                pEngine.DrawingMatrix *= Matrix.CreateTranslation(-translateX, -translateY, 0);
                pEngine.DrawingMatrix *= Matrix.CreateRotationZ(MathHelper.ToRadians(this.Rotation));
                pEngine.DrawingMatrix *= Matrix.CreateTranslation(translateX, translateY, 0);


    protected virtual void Draw(Engine pEngine) { }

    protected virtual void PostDraw(Engine pEngine)

I will implement some primitive entities by overridding Draw method. Here is my Drawing call, and the Draw implementation for Rectangle, and the StartDrawing in the Engine:

Managed Draw call every drawing:

    public void ManagedDraw(Engine pEngine)
        if (!this.Visible) return;

        { // Perform Drawing

            int childIndex = 0;

            // Underlay Children
            while (childIndex < this.Children.Count && this.Children[childIndex].Z < 0)

            // Draw Self

            // Overlay Children
            for (; childIndex < this.Children.Count; childIndex++)



** Draw implementation of Rectable**:

    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);

Start Drawing of Engine:

    public void StartDrawing()
        this.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, null, null, this.DrawingMatrix);

Here is one example of my failure, where the parent has Rotation of 30, while the children has none, but it should be in the center of the parent (red) rectangle: enter image description here

Please give me the problem with above Drawing Matrix. What should I do to achieve the stacking?

share|improve this question
AFAIK, this matrix you puts in the spriteBatch.Begin call represents the final Camera/View of the renderization. It doesn't make much sense to stack matrices in there, because the renderization generates only one final image. Individual rotations of sprites should be applied in the spriteBatch.Draw call. – Emir Lima Jan 22 '13 at 11:46
So can you suggest me any alternative? I would like the transformation of the children inherited by the parent entity. For example, the child is right at the center of the parent, when the parent entity rotation is 30, the child also rotate, but still at parent's center? An example is Unity's game objects, and AndEngine entities. – DatVM Jan 23 '13 at 2:58
Hum... Now I understood what you want! I'd simply put a reference of the parent in the child, and the child would apply the Rotation of its parent over its own Rotation. – Emir Lima Jan 23 '13 at 12:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.