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?
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.