Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm developing a simple game for the iphone and am struggling to work out the best way for the camera to follow the main character. The following picture hightlights the three main components:

character and terrain

There are 3 components to this:

  • Circle - the main character
  • Green line - terrain
  • Black background

The terrain is simply made from an array of points (approx 20 points per screen width). The terrain is moved in the x direction relative to the black background in order to keep the circle in its position shown. The distance to move the terrain is simply:

movex = circle.position.x - terrain.position.x

with a constant to fix the circle at some distance from the left of the screen.

I am struggling to determine the best way to position the terrain in the y plane keep the focus in the character. I want to move the terrain in the y direction smoothly and not fix it to the position of the circle, so the circle can move in the y plane.

If I take the same approach as the x positioning, the character is fixed at a point on the screen and the terrain moves. I could sample some terrain points either side of the character and produce an average, but in my implementation this was not smooth. I thought another approach might be to create a camera 'line' that is a smooth version of the terrain line and make the camerea follow this, but I'm not sure if this is the optimum solution. Any advice is much appreciated!

share|improve this question

2 Answers 2

up vote 2 down vote accepted

What you could do is determine where the character is within the viewport and only move the camera up if it is within (say) the top 20% or down if it's within the bottom 20%. Take this image for example: Ball example

The red and blue lines here represent the spot where the player would move the camera. For example, when the ball's position hits the red line, the camera would move up with the player. If the ball's position hits the blue line, the camera would move down with the player. This would allow the ball to bounce and move within the area between the red and blue lines but still keep the camera following the ball if, for example, it bounced really high.

You could move the blue line up if you wanted to make it centered on screen most of the time.

share|improve this answer
    
Many thanks! nice solution - easy to make the scene scale with the thresholds too. –  Alex Jun 4 '12 at 14:23

I would let the ball move in the camera-relative y plane within a certain range. Say the screen is 100px tall (I don't recall the actual specs); if the ball's y is above a certain threshold, maybe 70px above the bottom of the screen, move the camera up with the player. If the ball is below 30px, move the camera down with the player. Otherwise just keep the camera steady and let the ball move.

You may want to slowly recenter the camera on the player if he remains "stuck" at one of the thresholds.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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