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

Help!! Camera following character

Last post 5/15/2009 7:55 PM by Verminaard. 9 replies.
  • 5/6/2009 10:52 PM

    Help!! Camera following character

    We have a 2d game world where we have our character in and we want the camera to follow him.  We have our background and we want the character to move towards the edge of the screen and have the camera shift to reveal the other parts of the background and follow the character.  We need to find out how to get the camera to follow the character.  Any help would be appreciated.

    Thanks in advance.
  • 5/7/2009 12:04 AM In reply to

    Re: Help!! Camera following character

    Paul Lindeke:
    We have a 2d game world where we have our character in and we want the camera to follow him.  We have our background and we want the character to move towards the edge of the screen and have the camera shift to reveal the other parts of the background and follow the character.  We need to find out how to get the camera to follow the character.  Any help would be appreciated.

    Thanks in advance.

    Your character in most platform games doesn't really move. He stays in the center of the screen and walks (animated) in place while the background scrolls behind him. What you really need to do is make your background scroll smoothly behind him and keep him stationary.
  • 5/7/2009 12:51 AM In reply to

    Re: Help!! Camera following character

    The character camera-relative position is simply "character position - camera position."

    You can then express a rectangle of allowed character movement. For example, you may want to allow the character to be in the area -200 .. 200 in X direction, and -100 .. 100 in Y direction, and scroll the camera if the character moves more than that.

    In your update, simply do this:

      Rectangle AllowedMovement = new Rectangle(-200, -100, 200, 100);

      void Update(GameTime time) {
        Vector2 delta = CharacterPos - CameraPos;
        if (delta.X < AllowedMovement.Left)
          CameraPos += (delta.X - AllowedMovement.Left);
        if (delta.X > AllowedMovement.Right)
          CameraPos += (delta.X - AllowedMovement.Right);
        if (delta.Y < AllowedMovement.Top)
          CameraPos += (delta.Y - AllowedMovement.Top);
        if (delta.Y > AllowedMovement.Bottom
          CameraPos += (delta.Y - AllowedMovement.Bottom);
       ...
      }
    Note that this assumes Y grows downwards; swap the "top" and "bottom" values if your Y grows upwards.

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 5/7/2009 1:28 AM In reply to

    Re: Help!! Camera following character

    The documentation includes a tutorial for adding scrolling to the platformer starter kit. Check it out!
    Brandon Bloom
    Software Design Engineer
    XNA Platform and Tools
  • 5/7/2009 3:32 AM In reply to

    Re: Help!! Camera following character

    Sorry I didn't mention this in my previous post but the 2d game is a top-down view not a platformer.
  • 5/7/2009 9:27 AM In reply to

    Re: Help!! Camera following character

    The perspective is different, but the principals and math are identical. I recommend checking it out anyway.
    Brandon Bloom
    Software Design Engineer
    XNA Platform and Tools
  • 5/11/2009 9:37 PM In reply to

    Re: Help!! Camera following character

    Can't find the tutorial for adding scrolling to the game.  Still can't get the camera to move.
    Could it be the block of code
    Rectangle viewportRect;

    viewportRect = new Rectangle(0, 0,
                    GraphicsDevice.Viewport.Width,
                    GraphicsDevice.Viewport.Height);


  • 5/11/2009 9:44 PM In reply to

    Re: Help!! Camera following character

    You are making this a lot more complicated than it has to. To move the background, you simply move its draw position like you did initiallially with your main sprite. Say your background is 800x600 and you draw it a 0,0. When your charactor moves, instead of moving him to the right by 5 pixels, move the background to the left by 5. The illiusion is exactly the same as your char has 'moved' in relation to your background.
  • 5/12/2009 12:49 AM In reply to

    Re: Help!! Camera following character

    OK, we have the camera going with the character but now we have the problem of him going outside the level map.  We can't find any code that prevents the character from leaving the level map.  Is there a way we can also prevent the camera from going outside the level map.  If it is possible then we might be able to use the Clamptoviewport function and that will prevent him from leaving, but we don't know the code to prevent the camera from leaving the level map.  Any help would be great.  Thank you all for your assistance.
  • 5/15/2009 7:55 PM In reply to

    Re: Help!! Camera following character

    This is actually fairly simple to implement on a basic level as well. Say you know your level is 4000 pixels wide, your camera is 800px wide and your char is 50 px wide.

    To stop your camera going off the off-level, you can't have the left hand position of your screen below 0 (ie: off to the left) or above 3200 (ie: your level with minus the size of your screen).
    Similiarly, to stop your char going off-level you can't have the left hand position of your cahr sprite below 0 (ie: off to the left) or above 3950 (ie: your level with minus the size of your char sprite).

    Now you know the logic behind it, the code is fairly easy. Say you have a 'MoveChar' and a 'MoveCamera' function. They would look something like this:
    public void MoveChar()  
    {  
       if (CharShouldMoveLeft())  
           charPosition.X -= 5;  
     
       if (CharShouldMoveRight())  
           charPosition.X += 5;  
     
       MathHelper.Clamp(charPosition.X, 0, 3950);  
    }  
     
    public void MoveCamera()  
    {  
       if (CameraShouldMoveLeft())  
           cameraPosition.X -= 5;  
     
       if (CameraShouldMoveRight())  
           cameraPosition.X += 5;  
     
       MathHelper.Clamp(cameraPosition.X, 0, 3200);  


     the 'MathHelper.Clamp' function basically takes the first value and clamps it between the first and second value.

    This is obviously a pretty basic method., if you are looking to expand it, look at the platformer sample in the education catalog 
Page 1 of 1 (10 items) Previous Next