I have a nice camera set up for my 2D xna game. Im wondering how I should make the camera have 'acceleration' or 'lag' so it smoothly follows the player, instead of 'exactly' like mine does now. Im thinking somehow I need to Lerp the values when I set CameraPosition. Heres my code

private void ScrollCamera(Viewport viewport)

        float ViewMargin = .35f;

        float marginWidth = viewport.Width * ViewMargin;
        float marginLeft = cameraPosition.X + marginWidth;
        float marginRight = cameraPosition.X + viewport.Width - marginWidth;

        float TopMargin = .3f;
        float BottomMargin = .1f;
        float marginTop = cameraPosition.Y + viewport.Height * TopMargin;
        float marginBottom = cameraPosition.Y + viewport.Height - viewport.Height * BottomMargin;

        Vector2 CameraMovement;
        Vector2 maxCameraPosition;

        CameraMovement.X = 0.0f;
        if (Player.Position.X < marginLeft)
            CameraMovement.X = Player.Position.X - marginLeft;
        else if (Player.Position.X > marginRight)
            CameraMovement.X = Player.Position.X - marginRight;

        maxCameraPosition.X = 16 * Width - viewport.Width;
        cameraPosition.X = MathHelper.Clamp(cameraPosition.X + CameraMovement.X, 0.0f, maxCameraPosition.X);

        CameraMovement.Y = 0.0f;
        if (Player.Position.Y < marginTop) //above the top margin
            CameraMovement.Y = Player.Position.Y - marginTop;
        else if (Player.Position.Y > marginBottom) //below the bottom margin
            CameraMovement.Y = Player.Position.Y - marginBottom;

        maxCameraPosition.Y = 16 * Height - viewport.Height;
        cameraPosition.Y = MathHelper.Clamp(cameraPosition.Y + CameraMovement.Y, 0.0f, maxCameraPosition.Y);


Yes you can do it with a simple lerp. You have your current camera position called cameraPosition, let's say you add a target position you're trying to reach called targetPosition. You set your targetPosition to follow 'exactly' your player, and you smooth your cameraPosition this way:

cameraPosition.X = MathHelper.Lerp(cameraPosition.X, targetPosition.X, 0.1f);
cameraPosition.Y = MathHelper.Lerp(cameraPosition.Y, targetPosition.Y, 0.1f);

That's quick and easy, just tweak the last factor to adjust the effect. To get an even more natural-looking movement, you can do this twice: make your camera position follow a target that follows the 'fixed' target.

Note that this works well with fixed delta times, if your framerate varies a lot you'll be in trouble.

| improve this answer | |
  • \$\begingroup\$ Wait a sec.. For some reason when I jump UP or walk LEFT it dosent work, but if I fall DOWN or walk RIGHT it works, Im using my players position for TargetPosition. Prolly a problem with my camera, something with the viewmargin stuff. \$\endgroup\$ – Cyral Mar 29 '12 at 15:55
  • \$\begingroup\$ Maybe, try to get rid of what you can to leave this "naked", and do a bit of debugging if the bug persists. It should work. Fun fact: I have to give credit to Michel Ancel for that. He taught this technique to us programmers working on Rayman Origins at Ubisoft. Thus we called it the "Michel Smooth", for lack of a better name. \$\endgroup\$ – Laurent Couvidou Mar 29 '12 at 16:17
  • \$\begingroup\$ Ah Cool, Im going to step though it now and see what I can find thats wrong. \$\endgroup\$ – Cyral Mar 29 '12 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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