Using camera in processing for moving in specific area

Hi I’m a new coder on processing. I have long and complex code but I prepared a simple code to you for my question.

float camX = 0;
float camY = 0;
float camZ = (height/2)/tan(PI/6) - 260;

float x, y, z = 0;

float rotX = 0, rotY = 0;
float lastX, lastY;
float distX, distY;

void setup() {
  size(1600, 1000, P3D);
}


void draw() {
  background(0);

  camera(camX, camY, camZ, //default camera position without 360 degree
    x, y, z, //where it is looking to
    0, 1, 0); //eye openness

  rotateX(rotX + distY);
  rotateY(rotY + distX);

  beginShape(QUADS);
  vertex(-350, 100.1, 350);         // Upper left to Corner    
  vertex(350, 100.1, 350);          // Upper right to Corner
  vertex(350, 100.1, -350);         // Bottom right to Corner
  vertex(-350, 100.1, -350);        // Bottom left to Corner
  endShape();

  if (keyPressed)
  {
    if (key == 'w') {           // FRONT
      camZ += 5;
    } else if (key == 's') {   // BACK
      camZ -= 5;
    } else if (key == 'a') {   // LEFT
      camX += 5;
      x += 5;
    } else if (key == 'd') {    // RIGHT
      camX -=5;
      x -= 5;
    }
  }
}

public void mousePressed() {
  lastX = mouseX;
  lastY = mouseY;
}

public void mouseDragged() {
  distX = radians(mouseX - lastX);
  distY = radians(lastY - mouseY);
}

public void mouseReleased() {
  rotX += distY; 
  rotY += distX;
  distX = distY = 0;
}

So, I want to make a camera that can only go around the vertices of which was given.The camera won’t go outside from the white area. How to make that happened? Also there is a bug when I go further on the area. The camera changes the way when it reachs the half of the area. Can you help me about fixing that bug? Thank you.

1 Like

Concerning the bug:

1 Like

I tried the perspective but it didn’t help. It stills give a mirror effect when it reaches the half of area. I can’t go till the end of area.

Do you mean that the camera should not go into the Center of the scene where the vertices are?

Two approaches:

  • Instead of the camera going left/right/forward/backwards without conditions you could check with if() and dist() whether we are close to the vertices and then not allow the change of position required by the keys.

  • Another approach would be to think of the camera as being on a circle around the center and moving on the circle (different angle, together with cos and sin; see website | tutorials | trigonometry). Additionally to the angle you can change the radius but don’t let the radius become smaller than 99 or whatever.

Warmest regards,

Chrisir

1 Like

Hey, and welcome to the forum!

Great to have you here and great that you start to program!

Chrisir

Thank you so much :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray:

1 Like