We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I am using the camera() function to rotate around a box in 3D, but I have found an unexpected camera behavior when the z axis reaches 0 and minus values.
The rotation starts at y=height/2 and z= 200 (always looking at x=width/2, y=height/2, z=0), and it rotates properly upwards, until the camera is supposed to be located just vertically over the box (which is located at z=0). Then, it seems that the camera rotates over the z-axis or something that I don't understand.
I see the values of y and z with println, and I think they are correct. Anybody can see where the error is...?
Thank you,
Answers
// Here is the code:
// -----------------------------------------------------------------
float eyeX, eyeY, eyeZ;
float ang = 0;
int d = 200;
// -----------------------------------------------------------------
void setup(){
size(1000, 600, P3D);
eyeX = width/2;
eyeY = height/2;
eyeZ = d;
}
// -----------------------------------------------------------------
void draw(){
// background in the draw loop to make it animate rather than draw over itself
background(0);
lights();
stroke(255);
// CAMERA:
camera(eyeX, eyeY, eyeZ, width/2, height/2, 0, 0, 1, 0);
pushMatrix();
translate(width/2, height/2, 0);
box(100);
popMatrix();
}
// -----------------------------------------------------------------
void keyPressed(){
switch(key){
// Move camera
case CODED:
if (keyCode == UP) {
ang += 5;
eyeY = (height/2)-d*(sin(radians(ang)));
eyeZ = d*cos(radians(ang));
}
if (keyCode == DOWN) {
ang -= 5;
eyeY = (height/2)-d*(sin(radians(ang)));
eyeZ = d*cos(radians(ang));
}
println(eyeX+" / "+eyeY+" / "+eyeZ);
break;
}
}
// -----------------------------------------------------------------
reason is that the cam is upside down when going over the top
you can solve this by changing upY to -1
Great...!
I have implemented your suggestion and it works. Now I understand better the concept of the up-axis. Thank you.
Besides that, I have also completed the code with your suggested improvements.
Thank you very much, Chrisir!
Hi again,
Imagine I implement the same piece of code to rotate horizontally (x and z-axis) along with the previous code. This horizontal rotation goes well without having to change the up-axis. So, the "if (z<0)" condition wouldn't work right.</p>
So without using the "if (z<0)" condition, which would be the condition to change the direction of the up-axis? Maybe something related to the value of y?</p>
when only rotate horizontally you don't need to change the up-axis
just say
Or do you want to combine both rotations (vertically and horizontally)?
here
Thank you again, Chrisir.
Another odd behavior I see in the code is that, for example in the first code you have posted, the box disappears when z=0; I have tried to see if the problem is in the up-axis, so that I have tried with 'if (eyeZ <= 0)' instead of 'if (eyeZ < 0)' in line 20. But it keeps on disappearing... Is it due to the fact of the up-axis as well?</p>
Thank you,
...
...
sorry I don't know...
maybe start a new thread?
Did you try eyeZ <= 1 For that above?
Also an angle of 360 is = 0
Yes, I have tried this, and the same happens. My conclusion is that something odd happens when the camera is located just in the up-axis, but I'm not sure...