Search code examples
c#xnamonogamespritebatch

Spritebatch drawing in wrong position


Why is my sprite drawing to the top left corner when my wizardPos variable clearly puts it at the middle of the screen?

//in load content
wizardPos = new Vector2(graphics.PreferredBackBufferWidth /2, 700);
wizardChar = new Characters.Wizard(this, spriteBatch, wizardPos, wizardWalk1);


//in Draw method

 this.spriteBatch.Draw(tempWizard, wizardPos, null, Color.White, 0f, wizardPos, 1f, SpriteEffects.None, 0f);

Wizard should be drawing in red rectangle


Solution

  • Try this:

    this.spriteBatch.Draw(tempWizard, wizardPos, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
    

    The position of your sprite is actually the position of the origin of your sprite. So when you set the origin to wizardPos, the origin is set relative to the sprite - not the screen. So setting the origin and position to the same value effectively cancel each other out.

    The origin will usually be somewhere within the bounds of your sprite since it is used to calculate things like rotation as well