import peasy.*;

PeasyCam cam;

void setup() {
  size(200,200,P3D);
  cam = new PeasyCam(this, 100);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(500);
}
void draw() {
  rotateX(-.5);
  rotateY(-.5);
  background(0);
  fill(255,0,0);
  box(30);
  pushMatrix();
  translate(0,0,20);
  fill(0,0,255);
  box(5);
  popMatrix();
}

peasycam v302

A library by Jonathan Feinberg for the programming environment processing. Distributed under the Apache Public License, version 2.0. Last update, 05/13/2018.

PeasyCam provides a dead-simple mouse-driven camera for Processing.

Example

PeasyCam camera;

void setup() {
  camera = new PeasyCam(this, 0, 0, 0, 50);
}

That's it. Now a mouse left-drag will rotate the camera around the subject, a right drag will zoom in and out, and a middle-drag (command-left-drag on mac) will pan. A double-click restores the camera to its original position. The shift key constrains rotation and panning to one axis or the other.

The PeasyCam is positioned on a sphere whose radius is the given distance from the look-at point. Rotations are around axes that pass through the looked-at point.

PeasyCam is impervious to gimbal lock, and has no known "singularities" or discontinuities in its behavior. It relies on the excellent Apache Commons Math geometry package for its rotations.

Thanks

Useful bug reports and feature suggestions were provided by:

Please let me know if I've forgotten to acknowledge you.

Installing Peasycam

Processing 1.5.1

You can download a version of Peasycam compatible with Processing 1.5.1 here: http://static.mrfeinberg.com/peasycam/peasycam_15100.zip

Processing 2 and Later

You can install the latest Peasycam from within Processing via the menus Sketch > Import Library... > Add Library...

Constructors

PeasyCam(PApplet parent, double lookAtX, double lookAtY, double lookAtZ, double distance);
PeasyCam(PApplet parent, double distance); // look at 0,0,0

Methods

camera.setActive(boolean active);  // false to make this camera stop responding to mouse

// By default, the camera is in "free rotation" mode, but you can
// constrain it to any axis, around the look-at point:
camera.setYawRotationMode();   // like spinning a globe
camera.setPitchRotationMode(); // like a somersault
camera.setRollRotationMode();  // like a radio knob
camera.setSuppressRollRotationMode();  // Permit pitch/yaw only.

// Then you can set it back to its default mode:
camera.setFreeRotationMode();

// reassign particular drag gestures, or set them to null
camera.setLeftDragHandler(PeasyDragHandler handler);
camera.setCenterDragHandler(PeasyDragHandler handler);
camera.setRightDragHandler(PeasyDragHandler handler);
PeasyDragHandler getPanDragHandler();
PeasyDragHandler getRotateDragHandler();
PeasyDragHandler getZoomDragHandler();

// mouse wheel zooms by default; set null, or make your own
camera.setWheelHandler(PeasyWheelHandler handler);
PeasyWheelHandler getZoomWheelHandler();

// change sensitivity of built-in mouse wheel zoom
camera.setWheelScale(double scale); // 1.0 by default
double getWheelScale();

// make your own!
public interface PeasyDragHandler {
    public void handleDrag(final double dx, final double dy);
}
public interface PeasyWheelHandler {
    public void handleWheel(final int delta);
}
camera.setResetOnDoubleClick(boolean resetOnDoubleClick); // default true

camera.lookAt(double x, double y, double z);
camera.lookAt(double x, double y, double z, long animationTimeInMillis);
camera.lookAt(double x, double y, double z, double distance);
camera.lookAt(double x, double y, double z, double distance, long animationTimeInMillis);

camera.rotateX(double angle);  // rotate around the x-axis passing through the subject
camera.rotateY(double angle);  // rotate around the y-axis passing through the subject
camera.rotateZ(double angle);  // rotate around the z-axis passing through the subject
camera.setDistance(double d);  // distance from looked-at point
camera.pan(double dx, double dy);   // move the looked-at point relative to current orientation

double camera.getDistance();  // current distance
float[] camera.getLookAt();  // float[] { x, y, z }, looked-at point

camera.setMinimumDistance(double minimumDistance);
camera.setMaximumDistance(double maximumDistance);  // clamp zooming

camera.reset();
camera.reset(long animationTimeInMillis);  // reset camera to its starting settings

CameraState state = camera.getState(); // get a serializable settings object for current state
camera.setState(CameraState state);
camera.setState(CameraState state, long animationTimeInMillis); // set the camera to the given saved state

float[] rotations = camera.getRotations(); // x, y, and z rotations required to face camera in model space
camera.setRotations(double pitch, double yaw, double roll); // rotations are applied in that order
float[] position = camera.getPosition(); // x, y, and z coordinates of camera in model space

// Utility methods to permit the use of a Heads-Up Display
// Thanks, A.W. Martin
camera.beginHUD();
// now draw things that you want relative to the camera's position and orientation
camera.endHUD(); // always!

Keywords 3D,camera,rotation

Reference. Have a look at the javadoc reference here. a copy of the reference is included in the .zip as well.

Source. The source code of peasycam is available at GitHub, and its repository can be browsed here. Fork it and fix it!

Tested

Platform Mac
Processing 2.0
Dependencies