Class Camera

java.lang.Object
org.openpatch.scratch.Camera

public final class Camera extends Object
The camera of a stage: which part of the world the stage shows. Every layer but Layer.UI follows it, the backdrop included; pens draw where the camera shows their position when they draw. Unused, it shows the world as it is: position (0, 0), zoom 100.
Camera camera = this.getStage().getCamera();
camera.setPosition(player.getX(), player.getY());

World coordinates are where sprites are; screen coordinates are where something shows on the stage, with (0, 0) in its centre. The mouse is reported in world coordinates by getMouseX(), so it can be compared with sprite positions directly.

  • Constructor Details

    • Camera

      public Camera()
      A camera at (0, 0) with zoom 100. Every stage has its own: getStage().getCamera().
  • Method Details

    • setPosition

      public void setPosition(double x, double y)
      Centres the camera on a world position.
      Parameters:
      x - world x
      y - world y
    • setPosition

      public void setPosition(Vector2 position)
      Centres the camera on a world position.
      Parameters:
      position - world position
    • getPosition

      public Vector2 getPosition()
      Returns:
      the world position at the stage's centre
    • getX

      public double getX()
      Returns:
      the world x at the stage's centre
    • setX

      public void setX(double x)
      Parameters:
      x - the world x to show at the stage's centre
    • changeX

      public void changeX(double dx)
      Parameters:
      dx - how far to move the camera along x
    • getY

      public double getY()
      Returns:
      the world y at the stage's centre
    • setY

      public void setY(double y)
      Parameters:
      y - the world y to show at the stage's centre
    • changeY

      public void changeY(double dy)
      Parameters:
      dy - how far to move the camera along y
    • setZoom

      public void setZoom(double zoom)
      Sets the zoom in percent: 200 shows everything twice as large, 50 half as large. It stays within the zoom limits (50 to 200 unless changed).
      Parameters:
      zoom - the zoom in percent
    • changeZoom

      public void changeZoom(double dz)
      Parameters:
      dz - percentage points to add to the zoom
    • getZoom

      public double getZoom()
      Returns:
      the zoom in percent
    • resetZoom

      public void resetZoom()
      Back to zoom 100.
    • setZoomLimit

      public void setZoomLimit(double min, double max)
      Limits the zoom.
      Parameters:
      min - the smallest zoom in percent
      max - the largest zoom in percent
    • toScreenPosition

      public Vector2 toScreenPosition(Vector2 world)
      Where a world position shows on the stage.
      Parameters:
      world - a world position
      Returns:
      the screen position (origin at the stage's centre)
    • toScreenX

      public double toScreenX(double worldX)
      Parameters:
      worldX - a world x
      Returns:
      where it shows on the stage
    • toScreenY

      public double toScreenY(double worldY)
      Parameters:
      worldY - a world y
      Returns:
      where it shows on the stage
    • toWorldPosition

      public Vector2 toWorldPosition(Vector2 screen)
      Which world position shows at a place on the stage.
      Parameters:
      screen - a screen position (origin at the stage's centre)
      Returns:
      the world position
    • toWorldX

      public double toWorldX(double screenX)
      Parameters:
      screenX - an x on the stage
      Returns:
      the world x shown there
    • toWorldY

      public double toWorldY(double screenY)
      Parameters:
      screenY - a y on the stage
      Returns:
      the world y shown there