Class GameCamera

java.lang.Object
com.codename1.gaming.GameCamera

public class GameCamera extends Object

The camera a GameView renders through. It has two modes:

  • #MODE_ORTHO_2D (the default) is the classic 2D mode: an orthographic camera mapping one world unit to one screen pixel, origin at the top left with y pointing down. Sprites are flat, positioned in pixels, and the camera scrolls via Scene#setCamera(int, int). You never need to touch GameCamera for a 2D game.

  • #MODE_PERSPECTIVE turns the same scene into a 3D world: sprites become camera-facing billboards positioned in 3D space (Sprite#setPosition(double, double, double)), and 3D meshes can be drawn alongside them. You drive the view with #setPerspective(float, float, float), #setPosition(float, float, float) and #setTarget(float, float, float) -- e.g. an over-the-shoulder or top-down perspective camera that moves with the player.

// switch a GameView into 3D and place the camera
getCamera()
    .setPerspective(60, 0.1f, 500f)
    .setPosition(0, 6, 12)
    .setTarget(0, 0, 0);

In 3D mode world coordinates are right-handed with y up (the convention the com.codename1.gpu package uses), and sprite sizes are world units rather than pixels -- use Sprite#setSize(float, float) or Sprite#setScale(float) to pick a world-space size.

Following a target

In either mode the camera can also follow a moving target, as a side-scroller or a top-down game needs: it keeps the target inside a dead zone, leads in the direction the target faces, eases toward where it wants to be, stays inside bounds (normally the level) and shakes on demand. The follow state is plain arithmetic in world pixels: call #follow once per rendered frame with the target's center, then scroll the scene to #getViewX()/#getViewY() -- Scene#setCamera(int, int) in 2D, or derive the eye and target from them in perspective mode.

GameCamera cam = getCamera();
cam.setViewSize(getWidth(), getHeight());
cam.setBounds(0, 0, levelWidth, levelHeight);
cam.setDeadZone(0.15, 0.25);
// every frame:
cam.follow(heroX, heroY, heroFacing, deltaSeconds);
getScene().setCamera((int) cam.getViewX(), (int) cam.getViewY());
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Orthographic, pixel-space 2D rendering (the default).
    static final int
    Perspective 3D rendering with billboarded sprites.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    follow(double targetX, double targetY, int facing, double seconds)
    Moves the camera for one frame.
    float
     
    float
     
    float
     
    float
     
    int
     
    float
     
    float
     
    float
     
    double
    The view height.
    double
    The view width.
    double
    The left edge of the view, shake included.
    double
    The top edge of the view, shake included.
    void
    setBounds(double left, double top, double right, double bottom)
    Keeps the view inside a rectangle, normally the level.
    void
    setDeadZone(double width, double height)
    Sets the dead zone, as fractions of the view, inside which the target moves without moving the camera.
    void
    setLookAhead(double fraction)
    How far ahead of the target the camera leads, as a fraction of the view width.
    Switches back to the default orthographic 2D mode.
    setPerspective(float fovYDegrees, float near, float far)
    Switches to perspective 3D rendering and sets the lens.
    setPosition(float x, float y, float z)
    The eye position in world space (perspective mode).
    void
    setSmoothing(double rate)
    How quickly the camera catches up; higher is snappier, 0 snaps at once.
    setTarget(float x, float y, float z)
    The point the camera looks at, in world space (perspective mode).
    setUp(float x, float y, float z)
    The camera up vector (defaults to 0,1,0).
    void
    setViewSize(double width, double height)
    Sets the size of the view in world pixels.
    void
    shake(double amplitude, double seconds)
    Starts a shake that fades out.
    void
    snapTo(double targetX, double targetY)
    Jumps straight to a target, as at the start of a level or after a respawn.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MODE_ORTHO_2D

      public static final int MODE_ORTHO_2D
      Orthographic, pixel-space 2D rendering (the default).
      See Also:
    • MODE_PERSPECTIVE

      public static final int MODE_PERSPECTIVE
      Perspective 3D rendering with billboarded sprites.
      See Also:
  • Constructor Details

    • GameCamera

      public GameCamera()
  • Method Details

    • getMode

      public int getMode()
    • setPerspective

      public GameCamera setPerspective(float fovYDegrees, float near, float far)

      Switches to perspective 3D rendering and sets the lens.

      Parameters
      • fovYDegrees: vertical field of view in degrees (e.g. 60)

      • near: near clip distance (> 0)

      • far: far clip distance

    • setOrthographic2D

      public GameCamera setOrthographic2D()
      Switches back to the default orthographic 2D mode.
    • setPosition

      public GameCamera setPosition(float x, float y, float z)
      The eye position in world space (perspective mode).
    • setTarget

      public GameCamera setTarget(float x, float y, float z)
      The point the camera looks at, in world space (perspective mode).
    • setUp

      public GameCamera setUp(float x, float y, float z)
      The camera up vector (defaults to 0,1,0).
    • getEyeX

      public float getEyeX()
    • getEyeY

      public float getEyeY()
    • getEyeZ

      public float getEyeZ()
    • getTargetX

      public float getTargetX()
    • getTargetY

      public float getTargetY()
    • getTargetZ

      public float getTargetZ()
    • getFov

      public float getFov()
    • setViewSize

      public void setViewSize(double width, double height)

      Sets the size of the view in world pixels.

      Parameters
      • width: the width
      • height: the height
    • setBounds

      public void setBounds(double left, double top, double right, double bottom)

      Keeps the view inside a rectangle, normally the level.

      Parameters
      • left: the left edge
      • top: the top edge
      • right: the right edge
      • bottom: the bottom edge
    • setDeadZone

      public void setDeadZone(double width, double height)

      Sets the dead zone, as fractions of the view, inside which the target moves without moving the camera.

      Parameters
      • width: fraction of the view width
      • height: fraction of the view height
    • setLookAhead

      public void setLookAhead(double fraction)

      How far ahead of the target the camera leads, as a fraction of the view width.

      Parameters
      • fraction: 0 for none
    • setSmoothing

      public void setSmoothing(double rate)

      How quickly the camera catches up; higher is snappier, 0 snaps at once.

      Parameters
      • rate: per second
    • shake

      public void shake(double amplitude, double seconds)

      Starts a shake that fades out.

      Parameters
      • amplitude: the strongest offset in pixels
      • seconds: how long it lasts
    • snapTo

      public void snapTo(double targetX, double targetY)

      Jumps straight to a target, as at the start of a level or after a respawn.

      Parameters
      • targetX: the target's center
      • targetY: the target's center
    • follow

      public void follow(double targetX, double targetY, int facing, double seconds)

      Moves the camera for one frame.

      Parameters
      • targetX: the target's center
      • targetY: the target's center
      • facing: 1 right, -1 left
      • seconds: time since the last frame
    • getViewX

      public double getViewX()

      The left edge of the view, shake included.

      Returns

      world pixels

    • getViewY

      public double getViewY()

      The top edge of the view, shake included.

      Returns

      world pixels

    • getViewWidth

      public double getViewWidth()

      The view width.

      Returns

      world pixels

    • getViewHeight

      public double getViewHeight()

      The view height.

      Returns

      world pixels