Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualHappyCoder

Posted 19 February 2013 - 09:10 PM

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.

#1HappyCoder

Posted 19 February 2013 - 09:09 PM

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.

A quick and dirty alternative 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.

PARTNERS