Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)
        spriteBatch.Draw(
            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.

Edit:

http://dl.dropbox.com/u/13874083/screenshots/screen120107-160918.png

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

share|improve this question
    
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? –  David Gouveia Jan 7 '12 at 16:02
    
Oh yes, i use a scaling matrix. Sorry about that, will get an image now. –  dan369 Jan 7 '12 at 16:08
    
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. –  David Gouveia Jan 7 '12 at 16:21
    
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. –  dan369 Jan 7 '12 at 16:31
    
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. –  David Gouveia Jan 7 '12 at 16:35
show 1 more comment

1 Answer 1

up vote 1 down vote accepted

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
    {
        get 
        { 
            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);
spriteBatch.End();

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);
spriteBatch.End();

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.

share|improve this answer
    
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? –  dan369 Jan 7 '12 at 17:34
    
I had to get in this properly first before i could actually do the next part (*the next question) –  dan369 Jan 7 '12 at 17:35
    
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? –  dan369 Jan 7 '12 at 17:42
    
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)? –  dan369 Jan 7 '12 at 17:44
    
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. –  David Gouveia Jan 7 '12 at 17:56
show 2 more comments

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.