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 am creating a simple ambient/explorer platformer. Because I want the player to only be able to see a new area once they have reached it, I decided to split up the world in rooms.

To not make things like pushing boxes from room to room overly complicated, this camera behavior is implemented by simply adding or subtracting the screen width/height to/from the camera when the player is out of the screen bounds.

Or, in pseudocode:

if(player.x < camera.x) 
    camera.x -= screen.width;
if(player.x > camera.y + screen.width)
    camera.x += screen.width;
if(player.y < camera.y)
    camera.y -= screen.height;
if(player.y > camera.y + screen.height)
    camera.y += screen.height;

However, I also want to implement a slight parallax effect. For this I obviously need to move the camera around a little. So I need rooms that are slightly bigger than the screen size, and to make the camera follow the player until the player is at one of the sides of the room.

How do I create a smooth camera movement for this?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I am creating a simple ambient/explorer platformer. Because I want the player to only be able to see a new area once they have reached it, I decided to split up the world in rooms. However, I also want to implement a slight parallax-effect.

These effects always manage to look awesome.

How do I create a smooth camera movement for this?

The code you posted up there probably produces a kind of "snappy" camera movement as the player is walking around.

Keep track of where your camera is and where it needs to be next, and update it every frame by a tiny amount. In other words, perform interpolation from camera's currentPosition to nextPosition over time. This will create the smooth effect.

From here on, it's easy to clamp the camera to the sides of the room - make sure your nextPosition is within the boundaries of the room.

For these types of effects, I usually write a Camera Animator class with different interpolations.

share|improve this answer
    
I believe I might have been unclear in my question: I do want the 'snappy' movement to happen when the player switches rooms(which are areas directly adjacent in the scene to allow cross-room physics manipulations). But when the player is inside a room, I want the camera to follow the player until the camerabounds are against the room bounds. –  Qqwy Jan 11 '13 at 16:19
1  
@Qqwy - Assuming you have some way of representing room boundaries, you can always set the interpolation to occur between the two room walls (minimumX and maximumX?), and snap the camera whenever the room is changed. You're going to fiddle around with a few variables to get it to work. –  Jovan Jan 11 '13 at 16:40

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.