XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

The Chase Camera Sample : Zooming in and zooming out

Last post 24/12/2007 00:55 by Lord Ikon. 1 replies.
  • 24/12/2007 00:42

    The Chase Camera Sample : Zooming in and zooming out

    Hi all,

    I am fooling around with the third person camera inside the Chase Camera sample, trying to achieve a zoom-in/zoom-out effort. Here's the code snippet which I have added:

      if (currentKeyboardState.IsKeyDown(Keys.Z))
    {
    chaseCam.DesiredPositionOffset += new Vector3(0f, 0.1f, 0.1f);
    chaseCam.LookAtOffset -= new Vector3(0f, 0.1f, 0.1f);
    }

    if (currentKeyboardState.IsKeyDown(Keys.X))
    {
    chaseCam.DesiredPositionOffset -= new Vector3(0f, 0.1f, 0.1f);
    chaseCam.LookAtOffset += new Vector3(0f, 0.1f, 0.1f);
    }



    I am not sure if I am doing it correctly - I am trying to simulate the third person camera zooming found in most third person games. Also, I also wish to have a max zoom-in constraint (or else the camera will revert to face the entity in the other way round). What is that max zoom value?

    Any advice to implement a better zoom for a third person camera would be welcomed :)

    Thanks in advance!
  • 24/12/2007 00:55 In reply to

    Re: The Chase Camera Sample : Zooming in and zooming out

    Depends on the type of zoom you want. Do you want the camera to actually move its position closer to the target, or do you want the camera to zoom (like an optical zoom lens would give you)?

    If you'd like the camera to actually change position you should linearly interpolate (lerp) the position of the camera between its current position and the target position by a percentage. If the camera is at (0, 0, 0), and the target is at (10, 10, 10), and you want to zoom in 25% then you'd find the forward of the camera to the target, which in this case is (10, 10, 10), and multiply that by 0.25, which is 25%, and you'd get (2.5, 2.5, 2.5), which is the new position of the camera, and is closer to the target now.

    If you'd simply like an optical zoom effect, then you need to restrict the camera's field of view within its projection matrix. Keep track of the default FOV (field of view), so you can return to your normal 'zoom' later on. You can have a zoom level, which I use an integer for, and if your zoom is 4x, then you divide your default FOV by 4, and get 22.5. Use 22.5 degrees as your FOV when creating a new projection matrix for the camera. Don't forget to convert degrees to radians when doing this.

    If you'd like to see a zoom setup, I have zoom implemented in the free-camera in the engine in my signiture. It's open source if you'd like. Code you'll want is in FreeCamera.cs

    XNA QuickStart Engine (3D Game Engine for XNA) | My site
    "I'll be whatever I want to do!", Philip J. Fry
Page 1 of 1 (2 items) Previous Next