Hello people,
I’m trying to set up a fully functional camera in my code that responds to movements assigned by the computer keys, that I’ve accomplished. Now I’m trying to assign a limit to those movements, for example: in the code there are two torus objects, I want the camera movement (pan) to stop moving along the X axis when the torus touches the edge of the canvas.
I have tried setting up if statements calling out the p5.camera orientation properties (eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) but have not succeeded.
What is your opinion on how to get this working ?
Thannks,
// Left Arrow ====> Rotate camera left
// Right Arrow ====> Rotate camera right
// Up Arrow ====> Move the camera upward
// Down Arrow ====> Move the camera downward
// "+ - key ====> Move forward
// "-" - Key ====> Move backward
//50x50 red ellipse moves left, right, up and down with arrow presses and is limited by the canvas size
//keyIsDown example 0
// set up rotations speed values
let x = 0.01;
let y = 0.01;
let zMove = 0.8;
let eyeX;
let eyeY;
let eyeZ;
let centerX;
let centerY;
let centerZ;
let upX;
let upY;
let upZ;
let cam;
function setup() {
createCanvas(300, 300, WEBGL);
perspective(PI/3.0, width/height, ((height/2.0) / tan(PI * (60.0/360.0))) / 100, ((height/2.0) / tan(PI * (60.0/360.0))) * 100);
perspective(PI/3.0, width/height, ((height/2.0) / tan(PI * (60.0/360.0))) / 10, ((height/2.0) / tan(PI * (60.0/360.0))) * 10);
cam = createCamera();
cam.setPosition(0, 0, (height/2.0) / tan(PI*30.0 / 180.0)); // (eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
cam.pan(0);
cam.tilt(0);
cam.move(0, 0, 0);
fill(255, 0, 0);
}
function draw() {
if (keyIsDown(LEFT_ARROW)) {
x = (0.01) * 1.0;// this regulates the speed of the movement
}
else if (keyIsDown(RIGHT_ARROW)) {
x = (0.01) * -1.0;
}
else if (keyIsDown(UP_ARROW)) {
y = (0.01) * 1.0;
}
else if (keyIsDown(DOWN_ARROW)) {
y = (0.01) * -1.0;
}
else if (keyIsDown(107) || keyIsDown(187)) { // +
zMove = (0.8) * -1.0;
}
else if (keyIsDown(109) || keyIsDown(189)) { // -
zMove = (0.8) * 1.0;
}
else {
x = 0.0;
y = 0.0;
zMove = 0.0;
}
//This is what I tried to do but didn't work
//if (eyeX, centerX, upX == 0){
// x = 0.0;
//}
// if (eyeY, centerY, upY == 300){
// y = 0.0;
//}
//// if (eyeZ, centerZ, upZ == 100){
//// zMove = 0.0;
////}
cam.pan(x);
cam.tilt(y);
cam.move(0, 0, zMove);
background(200);
clear();
push();
translate (-30,0,0);
torus(30,15);
pop();
push();
translate (95, 0,0);
torus(30,15);
pop();
}