# Camera movement

## Recommended Posts

Hi!

I'm trying to learn how to automatically animate the camera movement. I want to add an effect where the camera animates into the scene before the actual game starts. How is this done smoothly ?

Any examples?

Thanks

##### Share on other sites

I'm guessing that by animation you mean translation/rotation (i.e. moving the camera about) over time.

Really it sort of depends on how complicated you want the "animation" to be, but ultimately should be no different than moving any other object around. Each frame move/rotate the camera toward it's target.

Do you have some more details as to what your trying to accomplish?

• A Simple pan/rotate from A/B/C to D/E/F
• A series of translations/rotations
• Orbiting around a point
• Some combination of movement

##### Share on other sites

I'm really new at this.

I would like to move and rotate the camera from a starting point to an ending point.

Something of a zoom effect. First the object is zoomed out and then the camera zooms in and rotates a little around the object to finally stop.

##### Share on other sites
You could define different keyframes and interpolate between them. Each keyframe representing a position and rotation at a given point in time.

To interpolate between two points you simply use

x = a * (1 - t) + b * t

where a is your stating point, b is the ending point, and t is a value between 0 and 1. You could then keep track of how much time has passed and calculate t with timePassed / animationDuration. Once time passed is bigger than animationDuration, you end the animation and continue the camera normally.

How do you represent camera rotation? If you using euler angles you could interpolate the angles as seen above and should get a good result.

Also, a trick I have found that works really well for player or camera control state is using the state pattern. It works by implementing the differnt control logic states in separate classes, then you simply have a variable in the camera that you assign to change the camera control state. So for example
class CameraControlState
{
abstract void update(Camera camera, double dt);
}

class FollowCameraControlState extends CameraControlState
{
void update(Camera camera, double dt)
{
// logic for follow camera
}
}

class AnimationCameraControlState extends CameraControlState
{
void update(Camera camera, double dt)
{
// logic for animating camera
}
}

class Camera
{
CameraControlState controlState;

void update(double dt)
{
controlState.update(this, dt);
}
}

// then to use it somewhere in the code
Camera camera = new Camera();
camera.setControlState(new FollowCameraControlState(cameraTarget));

// then to start a cutscene
camera.setControlState(new AnimationCameraControlState(animationKeyframes));


Implementing that may be an overkill for you current project but I am throwing it out there for consideration. Doing the camera (and player) control like this makes it extremely flexible. You can add a first person view, a fixed camera, a top down camera, a follow camera, and any other idea you may have easily. Just add another class, you don't need to touch any of the camera code and you don't end up with monstrous switch statements everywhere in your code that are difficult to maintain.

Or a quick and dirty alternative to animating your camera could be to only allow your camera to move and rotate at a limited velocity and then start the camera far away and facing another direction. Edited by HappyCoder

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account