public class GameCamera

  1. Object
  2. GameCamera

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.

Fields

public static final int MODE_ORTHO_2D = 0Orthographic, pixel-space 2D rendering (the default).
public static final int MODE_PERSPECTIVE = 1Perspective 3D rendering with billboarded sprites.

Constructors

public GameCamera()

Methods

public int getMode()
public GameCamera setPerspective(float fovYDegrees, float near, float far)Switches to perspective 3D rendering and sets the lens.
public GameCamera setOrthographic2D()Switches back to the default orthographic 2D mode.
public GameCamera setPosition(float x, float y, float z)The eye position in world space (perspective mode).
public GameCamera setTarget(float x, float y, float z)The point the camera looks at, in world space (perspective mode).
public GameCamera setUp(float x, float y, float z)The camera up vector (defaults to 0,1,0).
public float getEyeX()
public float getEyeY()
public float getEyeZ()
public float getTargetX()
public float getTargetY()
public float getTargetZ()
public float getFov()

Inherited methods

Field details

MODE_ORTHO_2D

public static final int MODE_ORTHO_2D = 0
Orthographic, pixel-space 2D rendering (the default).

MODE_PERSPECTIVE

public static final int MODE_PERSPECTIVE = 1
Perspective 3D rendering with billboarded sprites.

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 float
vertical field of view in degrees (e.g. 60)
near float
near clip distance (> 0)
far float
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()