How do I focus a camera on more than one entity and make physical camera boundaries?

  • I've educated myself and tested scirra.com/tutorials/626/making-a-smooth-following-camera which brought about the desired results with one player, however I tried using the exact same method to command the camera to lock on to Player 1 and Player 2 at the same time, which seemed to "confuse" the camera, so I need to know how to have a camera focus on four entities at one without ever zooming in or out.

    It was difficult to find something similar to what I'm talking about, but this video a little after six minutes somewhat showcases what I'm talking about.

    Furthermore, I'm making a four-player two-dimensional platformer and I want the camera made so that if one player would go as far as they could, the camera would stop them from moving more than a screen's length away from a player all the way in the back, so that no one can move out of the view of the camera. However, I do not want this camera boundary to affect other entities besides players in any way.

  • You would average the xy's

    (x1 + x2)/2

    (y1 + y2)/2

    In this scenario.

    Same formula for the restriction, but setting object xy's including the "clamp(value,min, max)" expression.

  • 1. Calculate the average x,y of all the player positions.

    2. Set your camera position using the average position as the second parameter of the lerp.

    Then to limit the player positions to the screen you could clamp the player positions.

    So maybe something like this.

    Global number camx=320

    Global number camy=240

    Every tick

    — set camx to lerp(camx, (player1.x + player2.x)/2, 0.03)

    — set camy to lerp(camy, (player1.y + player2.y)/2, 0.03)

    —scroll to (camx, camy)

    — player1: set x to clamp(self.x, camx-320, camx+320)

    — player1: set y to clamp(self.y, camy-240, camy+240)

    — ...do the same for player2

    Notes:

    320, 240 are half the viewport size 640,480. Change as needed.

    The above does the average of two players. For three it would look like this:

    (Player1.x+player2.x+player3.x)/3

  • You would average the xy's

    (x1 + x2)/2

    (y1 + y2)/2

    In this scenario.

    Same formula for the restriction, but setting object xy's including the "clamp(value,min, max)" expression.

    Thank you! What does clamp do? I looked that up, but couldn’t find really anything on it.

  • 1. Calculate the average x,y of all the player positions.

    2. Set your camera position using the average position as the second parameter of the lerp.

    Then to limit the player positions to the screen you could clamp the player positions.

    So maybe something like this.

    Global number camx=320

    Global number camy=240

    Every tick

    — set camx to lerp(camx, (player1.x + player2.x)/2, 0.03)

    — set camy to lerp(camy, (player1.y + player2.y)/2, 0.03)

    —scroll to (camx, camy)

    — player1: set x to clamp(self.x, camx-320, camx+320)

    — player1: set y to clamp(self.y, camy-240, camy+240)

    — ...do the same for player2

    Notes:

    320, 240 are half the viewport size 640,480. Change as needed.

    The above does the average of two players. For three it would look like this:

    (Player1.x+player2.x+player3.x)/3

    Thanks! I asked the same to the previous person who applied, but what exactly does clamp do. I looked it up and couldn’t find much on it.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Clamp limits the range of a number.

    It’s clamp(number, low, high)

    So if the number is lower than low, it will use low.

    And if the number is higher than high, it will use high.

    Otherwise the number will be used as is.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)