Is there any way to update the camera, rather than create a new one?

I was creating a 3-d modeling program and was working on camera controls, when I realized that I couldn’t make a camera system that would just update the camera’s position and other values, rather than just creating a new camera. Is there any better way to do this so camera movements are preserved when another operation is used?

float mouseW;
void mouseWheel(MouseEvent event) {
  mouseW = event.getCount();
void op() {
  if (name!=null) {
    if (name=="Save") {
    } else if (name=="Import") {
      selectInput("Please select an object(.obj) file for import.", "importobj");
    } else if (name=="Shapes") {
    } else if (name=="Pencil") {
    } else if (name=="Clay") {
    } else if (name=="Camera") {
      if (mouseButton==LEFT) {
        camera(mouseX, mouseY, (height/2) / tan(PI/6), width/2, height/2, 0, 0, 1, 0);
      } else if (mouseW!=0.0) {
        if (mouseW>0) {
          camera(0, 10, 0, 0, 0, 0, 0, 1, 0);//add+sub rather than change?
        } else if (mouseW<0) {
          camera(0, -10, 0, 0, 0, 0, 0, 1, 0);
      } else if (mouseButton==RIGHT) {
        camera(mouseX, 0, mouseY, 0, 0, 0, 0, 1, 0);

P.s. If I’m being too vague, ask for the info I missed.

1 Like

Create a camera class like this

public class Camera {

	float eyeX, eyeY, eyeZ;
	float targetX, targetY, targetZ;
	float upX, upY, upZ;

	public Camera(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ) {
		this(eyeX, eyeY, eyeZ, targetX, targetY, targetZ, 0, 1, 0);

	public Camera(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ, float upX, float upY,
			float upZ) {
		this.eyeX = eyeX;
		this.eyeY = eyeY;
		this.eyeZ = eyeZ;
		this.targetX = targetX;
		this.targetY = targetY;
		this.targetZ = targetZ;
		this.upX = upX;
		this.upY = upY;
		this.upZ = upZ;

	public void applyTo(PGraphics pg) {, eyeY, eyeZ, targetX, targetY, targetZ, upX, upY, upZ);

	public void applyTo(PApplet papp) {
        // Leave you to add getters and setters :-)

You can create several cameras and appyTo any PApplet of PGraphics instance.


Thank you so much for the great answer! I was just wondering one last thing, if I wanted to create a UI overlay for the camera class, where in the code you gave me could I put it so it would stay in the same part of the screen the whole time?

void menu() {
  rect(0, 0, width/20, height);
  rect(width/20, 0, width/20, height);
  for (int i=0; i<menunames.length; i++) {
    PImage g=loadImage(menunames[i]+".png");
    g.resize(width/20, width/20);
    image(g, 0, 0+i*(width/20));
    if (mousePressed&&mouseX<width/20&&mouseX>0&&mouseY>i*(width/20)&&mouseY<(i+1)*(width/20)) {
1 Like

Yes. The draw method would look something like this

void draw(){
  backround(0); // or whatever colour you want

  // In this part of draw do all your 3D camera and rendering stuff
  // Normal 2D pixel coordinate space prevails lol
  // Draw any 2D stuff here e.g. menus
  // End of draw method

I’m sorry to waste your time, but I’m curious how to actually define and use the camera class. I have something along the lines of this in my draw loop:

Camera cam=new Camera(0, 0, 0, 0, 0, 0);
void draw() {

But I’m not sure what to put in the cam.applyto field(I tried googling, but for some reason nothing in the docs worked for me). This will be the last question, thanks in advance.

1 Like

In your example the third line would be


Also you can’t do this. This first three values represent the XYZ coordinates for the eye, the second three the XYZ coordinates of where you are looking at.
They can’t be the same position in 3D space. Review the reference for the camera(...) method.