What Im trying to do is pretty simple, offset the rendering position of my image when scaled up/down. I honestly can't remember how to-do this :(

if (newAnim.texture != null)
    // Set Source Rectangle
    sourceRect.X = newAnim.imageRectArray[newAnim.CurrentFrame].X;
    sourceRect.Y = newAnim.imageRectArray[newAnim.CurrentFrame].Y;
    sourceRect.Width = newAnim.imageRectArray[newAnim.CurrentFrame].Width;
    sourceRect.Height = newAnim.imageRectArray[newAnim.CurrentFrame].Height;

    // Get new center when scaled.
    Vector2 center = new Vector2(
        (newAnim.imageVector2Array[newAnim.CurrentFrame].X * scale) / 2,
        (newAnim.imageVector2Array[newAnim.CurrentFrame].Y * scale) / 2

    // Only Render if we have Something to use
    if (newAnim.CurrentFrame >= 0)
            newAnim.texture, new Vector2(100, 100), sourceRect,
            Color.White, 0f, center, 1f, SpriteEffects.None, 0f

I'm unsure what I need to do to the Vector2. Hopefully getting the center/origin position is correct? I think so.



The bigger image is the scaled image (pretty obvious :P), but both are rendered at 100, 100.

  • \$\begingroup\$ Can you upload an image of what you're trying to do? The before and the after. This should be easy to solve, but I'm not sure what you want. For instance, you're not even applying the scale when drawing. Is that on purpose? \$\endgroup\$ Commented Jan 7, 2012 at 16:02
  • \$\begingroup\$ Oh yes, i use a scaling matrix. Sorry about that, will get an image now. \$\endgroup\$
    – dan369
    Commented Jan 7, 2012 at 16:08
  • \$\begingroup\$ What's the correlation between the offset, the image size, and the scale? Or rather, what is it you're actually trying to achieve? This looks like a 2D camera zoom sort of behavior to me. \$\endgroup\$ Commented Jan 7, 2012 at 16:21
  • \$\begingroup\$ This looks like a 2D camera zoom sort of behavior to me - Yep, exactly. Though i did look at some of the tutorials around, most had everything, i.e rotation, movement, zoom. All i want is zoom, hence why i thought i'd just go with using a scalingMatrix. Trying to achieve a zoom effect, without the image moving away from the rendering position *100/100. \$\endgroup\$
    – dan369
    Commented Jan 7, 2012 at 16:31
  • \$\begingroup\$ Oh okay, that's simple. Do you want the zoom to happen centered on the middle of the screen or centered on the top-left corner? Your picture seems to suggest the top left corner. \$\endgroup\$ Commented Jan 7, 2012 at 16:35

1 Answer 1


Since we reached the conclusion that you're simply trying to implement a zooming behavior, here's the most simple camera class possible with Zoom and Origin. By default this will be the middle of the screen:

public class Camera
    public Camera(Viewport viewport)
        Origin = new Vector2(viewport.Width / 2.0f, viewport.Height / 2.0f);
        Zoom = 1.0f;

    public float Zoom { get; set; }
    public Vector2 Origin { get; set; }

    public Matrix ViewMatrix
            return Matrix.CreateTranslation(new Vector3(-Origin, 0.0f)) * 
                   Matrix.CreateScale(Zoom, Zoom, 1) * 
                   Matrix.CreateTranslation(new Vector3(Origin, 0.0f)); 

And here's an example of how to use it. Notice both sprites are being drawn exactly with the same parameters (except for the color):

Camera camera = new Camera(GraphicsDevice.Viewport);
camera.Origin = Vector2.Zero;

Vector2 position = new Vector2(100, 100);
Vector2 origin = new Vector2(texture.Width, texture.Height) / 2f;

camera.Zoom = 1f;
spriteBatch.Begin(0, null, null, null, null, null, camera.ViewMatrix);
spriteBatch.Draw(texture, position, null, Color.LightBlue, 0f, origin, 1f, SpriteEffects.None, 0f);

camera.Zoom = 3f;
spriteBatch.Begin(0, null, null, null, null, null, camera.ViewMatrix);
spriteBatch.Draw(texture, position, null, Color.LightCoral, 0f, origin, 1f, SpriteEffects.None, 0f);

Which results in the following picture:

enter image description here

In this example I set the camera origin to be Vector2.Zero since it matches the behavior on your screenshot. But normally, it would be better to zoom around the center of the screen, in which case just don't set any Origin.

Give both a try, and if you decide that you really don't need an Origin, then simply use the scale matrix Matrix.CreateScale(Zoom, Zoom, 1) directly. You don't need to calculate new positions or origins for the sprites!

PS - About the origin of your sprites

Hopefully getting the center/origin position is correct?

Not really. If you want the origin to be the center of the sprite, and since you're using source rectangles, your origin should be:

Vector2 origin = new Vector2(sourceRect.Width, sourceRect.Height) / 2f;

The origin is defined in local (or model) space so it doesn't matter if you're scaling the entire scene or even the individual sprites, the origin still won't change.

  • \$\begingroup\$ Thanks for the help man :). I do have another query concerning scaling, its a bit more complicated though. Unsure should i make a new question detailing? \$\endgroup\$
    – dan369
    Commented Jan 7, 2012 at 17:34
  • \$\begingroup\$ I had to get in this properly first before i could actually do the next part (*the next question) \$\endgroup\$
    – dan369
    Commented Jan 7, 2012 at 17:35
  • \$\begingroup\$ Basically, i'm creating/draw rectangles via using the mouse, and i need to be able when zooming in create these rectangles. Currently the mouse position is somewhat off? \$\endgroup\$
    – dan369
    Commented Jan 7, 2012 at 17:42
  • \$\begingroup\$ Here's an example of the rectangles: dl.dropbox.com/u/13874083/screenshots/screen120107-173354.png I remember something about inverting the camera view matrix to relate the mouse position back to world view? But im unsure how to-do this/forgot, i remember it's using Matrix.Invert(camera.ViewMatrix)? \$\endgroup\$
    – dan369
    Commented Jan 7, 2012 at 17:44
  • \$\begingroup\$ That's super simple. Just do Vector2 worldPosition = Vector2.Transform(mousePosition, Matrix.Invert(camera.ViewMatrix)). And I wrote this a while back, should cover most questions concerning this topic. I've also explained that once or twice before in other questions here on GD.SE. \$\endgroup\$ Commented Jan 7, 2012 at 17:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .