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.

In a 2d game, I have a basic camera class. The guts of the class creates a transformation matrix that is later passed to the Begin() method of a SpriteBatch. The transformation is done like this:

        Transform = Matrix.Identity *
                    Matrix.CreateTranslation(-Position.X, -Position.Y, 0) *
                    Matrix.CreateScale(new Vector3(Scale, Scale, Scale)) *
                    Matrix.CreateTranslation(ScreenCenter.X, ScreenCenter.Y, 0);

I'm trying to create an effect that zooms in on a specific point on the screen. The issue is that the zoom/translation causes a strange "swooping" effect when applied. For example, when the zoom is set to 1.0 and the current position is (0, 0), and I change these to 2.0 and (100, 200) over a 1 second interval, the camera appears to take a curved path to (100, 200).

Here is my code to update the position / scale on each frame (I removed logic to determine when the transition is finished):

        _positionTimeElapsed += gameTime.ElapsedGameTime.TotalMilliseconds;
        _scaleTimeElapsed += gameTime.ElapsedGameTime.TotalMilliseconds;

        Position = Vector2.Lerp(
            _positionStart,
            PositionDestination.Value,
            (float)(_positionTimeElapsed / TransitionTimeMilliseconds));

        Scale = (ScaleDestination.Value - _scaleStart)
             * (float)(_scaleTimeElapsed / TransitionTimeMilliseconds)
             + _scaleStart;

I'm relatively confident that I'm missing something basic about Matrix math. Does anyone know how to transition both position and scale simultaneously and have the camera appear to take a linear path to the destination?

UPDATE

Added two videos on YouTube that show the issue and the fix in action. These videos zoom to 10x over 5 seconds to make the bug obvious.

Broken: http://www.youtube.com/watch?v=d8fJ4Zf-sqU

Fixed: http://www.youtube.com/watch?v=GpcUc25eZ8o

share|improve this question
    
At first glance I don't see anything obvious. My first check would be that order of operations matter with Matrix multiplication and simply re-ordering the operations could be a starting point to try. I'd try moving the Scale first. Also I think you can just use the scalar Scale * Matrix.Identity rather than needing CreateScale. On the other hand, you could probably just use the CreateScale entirely instead of Matrix.Identity. –  WorldMaker Sep 27 '10 at 2:15
    
At the end of the transition, everything is correctly scaled and positioned, so I'd be very surprised if it's an order of operations issue. That was the first thing that came to mind, but I believe I have ruled it out (always possible that I screwed up the math when trying to rule it out). –  Alex Morris Sep 27 '10 at 9:22
add comment

1 Answer

up vote 8 down vote accepted

Your swooping effect is because there is no stationary pixel during the interpolation. In a dolly and pan, there will be a point (a line actually) that doesn't change in appearance other than mip level.

You're using a zoom/scale, and because of this you're suffering from an accelerated scale problem, if you tried going from 2.0 to 0.1, then it would become more obvious why this was a non-linear operation.

You need to consider the zoom as it's inverse, then this goes away. at half-way, your zoom is 1.5, but if you took the inverse of your zoom (1.0 -> 0.5 instead of 1.0 to 2.0), then half-way becomes 1.3333 (1.0/0.75), which actually gives you the zoom you want at that part of your linear interpolation across space (the 0,0 to 100,200).

For Kylotan: To make a camera look like it's not swooping, you have to be able to pin a point in the screen. What you're after is a linear interpolation of the top,bottom,left,and right values of the render rectangle, well, that's not going to happen if you're interpolating the scale, because that's the "inverse" of the size of the rectangle.

What happened was, interpolating the scale cause the rectangle to shrink non linearly over time. example: assuming the rectangle was -100,100,-100,100 (that's left,right,top,bottom) translation moves it to 0,200,100,300, but shrinks (scaled 0.5) as the scene is zoomed to 50,150,150,250 that's the final destination of the interpolation... but, if you interpolate the zoom... zoom at 1.5 == 0.666 scale in render rectangle, leading to -16.666,116.666,33.333,166.666

At any point during the interpolation, the world point 200,400 should always appear in the same relative position to the render rectangle. At the start it is 200,400 but at scale 1, and at the end it is at 100,200 relative, but scaled up by 2 due to scene scaling, so it's still the same position relatively.

if you take the halfway point, it's at 150,300, but the scene is scaled by 1/.75 at this point so the relative position is once again 200,400 (150/0.75==200 & 300/0.75==400)

share|improve this answer
    
Wow, that was easy. Thanks! Lost about three hours to this problem, and the fix was literally to take the inverse of all of my variables with "scale" in the name, and then take the inverse of that final value before assigning it to Scale. –  Alex Morris Sep 27 '10 at 12:51
    
Richard, doesn't this go away if you treat the centre of the screen as the camera position? That will move linearly and the pixel at the point will not be altered by the scale. I'm finding it hard to picture this problem in the first place and sadly I'm no wiser having read this answer! –  Kylotan Sep 28 '10 at 14:35
    
Kylotan, I have updated the question with two YouTube videos to help with visualization. I should have done this from the start. –  Alex Morris Sep 28 '10 at 21:31
    
Thanks Alex and Richard for elaborating on the problem and the solution. –  Kylotan Sep 29 '10 at 12:26
add comment

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.