I want to be able to scale my sprite and keep them in the same spot as they were before the scale. I use the center of the sprite for the origin parameter because I want to be able to rotate my sprite too.
I'm sure the solution is going to be trivial but I can't find a proper/general solution to solve this problem.
If it's not really clear I made some pictures to show my code, the result of this code and what I want to achieve.
1 - This my code and the result this is what I get when I scale the sprite, as you can see the sprite is scaled but it "moved":
As suggested in the comment here is the code:
Vector2 scale = Vector2.One;
float rotation = 0;
public void Update(GameTime gameTime)
{
if (Input.IsKeyPressed(Keys.P))
scale += new Vector2(0.05f, 0.0f);
if (Input.IsKeyPressed(Keys.M))
scale -= new Vector2(0.05f, 0.0f);
if (Input.IsKeyPressed(Keys.O))
scale += new Vector2(0.0f, 0.05f);
if (Input.IsKeyPressed(Keys.L))
scale -= new Vector2(0.0f, 0.05f);
if (Input.IsKeyPressed(Keys.D))
rotation += MathHelper.ToRadians(5);
if (Input.IsKeyPressed(Keys.Q))
rotation -= MathHelper.ToRadians(5);
Input.Update(gameTime);
}
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, Matrix.CreateScale(4));
spriteBatch.Draw(Sprites.BACKGROUND, Vector2.Zero, new Rectangle(0, 0, 128, 128), Color.White);
Vector2 marioPosition = new Vector2(64, 112 - 32);
Rectangle source = new Rectangle(0,0,16,32);
Vector2 origin = source.Size.ToVector2() / 2;
spriteBatch.Draw(Sprites.MARIO, marioPosition + origin, source, Color.White, rotation, origin, scale, SpriteEffects.None, 0f);
spriteBatch.End();
}
2 - This is what I want to achieve, I know I can do this by moving my origin point but I want to keep it in the center of the sprite so i can apply rotation around this point: http://pasteboard.co/rzMfc0p.png