Can somebody help me with the camera function?
in
Programming Questions
•
2 years ago
I am trying to get the following code to make the shapes rotate around the center using the camera() function. All it does is begin at the starting point, then move to a second point exactly half-way between the x and y axis. You can try the code for yourself to see what it does.
int domeRadius = 100;
int cwidth = 25;
int cheight = 100;
int csides = 50;
int sectorRadius = 75;
int R = 93;
int G = 93;
int BDome = 139;
int ADome = 128;
int A = 255;
int B = 255;
float cangle = 0;
float initialRotationx = 0;
float initialRotationy = width/2;
float rotationx = initialRotationx;
float rotationy = initialRotationy;
int cwidth = 25;
int cheight = 100;
int csides = 50;
int sectorRadius = 75;
int R = 93;
int G = 93;
int BDome = 139;
int ADome = 128;
int A = 255;
int B = 255;
float cangle = 0;
float initialRotationx = 0;
float initialRotationy = width/2;
float rotationx = initialRotationx;
float rotationy = initialRotationy;
void setup() {
size(400, 400, P3D);
background(0);
smooth();
noStroke();
rotateCamera();
celestialDome(width/2, height/2, 0);
sectorFive();
tunnels(cwidth, cheight, csides);
}
size(400, 400, P3D);
background(0);
smooth();
noStroke();
rotateCamera();
celestialDome(width/2, height/2, 0);
sectorFive();
tunnels(cwidth, cheight, csides);
}
void draw() {
background(0);
rotateCamera();
celestialDome(width/2, height/2, 0);
sectorFive();
tunnels(cwidth, cheight, csides);
}
background(0);
rotateCamera();
celestialDome(width/2, height/2, 0);
sectorFive();
tunnels(cwidth, cheight, csides);
}
void celestialDome(int translationx, int translationy, int translationz) {
translate(width / 2, height / 2, 0);
fill(R, G, BDome, ADome);
sphere(domeRadius);
}
translate(width / 2, height / 2, 0);
fill(R, G, BDome, ADome);
sphere(domeRadius);
}
void sectorFive() {
fill(R, G, B, A);
sphere(sectorRadius);
}
fill(R, G, B, A);
sphere(sectorRadius);
}
void tunnels(int cWidth, int cHeight, int cSides) {
fill(93, 93, 125);
rotateZ(-PI - HALF_PI);
translate(0, 125, 0);
cylinder(cWidth, cHeight, cSides);
rotateZ(PI);
translate(0, 250, 0);
cylinder(cWidth, cHeight, cSides);
rotateZ(HALF_PI);
translate(-125, 125, 0);
cylinder(cWidth, cHeight, cSides);
rotateZ(PI);
translate(0, 250, 0);
cylinder(cWidth, cHeight, cSides);
println("Tunnels Created");
}
fill(93, 93, 125);
rotateZ(-PI - HALF_PI);
translate(0, 125, 0);
cylinder(cWidth, cHeight, cSides);
rotateZ(PI);
translate(0, 250, 0);
cylinder(cWidth, cHeight, cSides);
rotateZ(HALF_PI);
translate(-125, 125, 0);
cylinder(cWidth, cHeight, cSides);
rotateZ(PI);
translate(0, 250, 0);
cylinder(cWidth, cHeight, cSides);
println("Tunnels Created");
}
void cylinder(float w, float h, int sides)
{
float angle;
float[] x = new float[sides+1];
float[] z = new float[sides+1];
//get the x and z position on a circle for all the sides
for(int i=0; i < x.length; i++){
angle = TWO_PI / (sides) * i;
x[i] = sin(angle) * w;
z[i] = cos(angle) * w;
}
//draw the top of the cylinder
beginShape(TRIANGLE_FAN);
vertex(0, -h/2, 0);
for(int i=0; i < x.length; i++){
vertex(x[i], -h/2, z[i]);
}
endShape();
//draw the center of the cylinder
beginShape(QUAD_STRIP);
for(int i=0; i < x.length; i++){
vertex(x[i], -h/2, z[i]);
vertex(x[i], h/2, z[i]);
}
endShape();
//draw the bottom of the cylinder
beginShape(TRIANGLE_FAN);
vertex(0, h/2, 0);
for(int i=0; i < x.length; i++){
vertex(x[i], h/2, z[i]);
}
endShape();
}
{
float angle;
float[] x = new float[sides+1];
float[] z = new float[sides+1];
//get the x and z position on a circle for all the sides
for(int i=0; i < x.length; i++){
angle = TWO_PI / (sides) * i;
x[i] = sin(angle) * w;
z[i] = cos(angle) * w;
}
//draw the top of the cylinder
beginShape(TRIANGLE_FAN);
vertex(0, -h/2, 0);
for(int i=0; i < x.length; i++){
vertex(x[i], -h/2, z[i]);
}
endShape();
//draw the center of the cylinder
beginShape(QUAD_STRIP);
for(int i=0; i < x.length; i++){
vertex(x[i], -h/2, z[i]);
vertex(x[i], h/2, z[i]);
}
endShape();
//draw the bottom of the cylinder
beginShape(TRIANGLE_FAN);
vertex(0, h/2, 0);
for(int i=0; i < x.length; i++){
vertex(x[i], h/2, z[i]);
}
endShape();
}
void rotateCamera(){
rotationx = initialRotationx - (initialRotationx * sin(radians(cangle)));
rotationy = initialRotationy - (initialRotationy * cos(radians(cangle)));
camera(rotationx, rotationy, 0,
width/2, height/2, 0,
0, 0, -1.0);
cangle++;
println("Angle Incremented");
println("Angle = " + cangle);
if(rotationx <= -width/2) {
rotationx = initialRotationx;
cangle = 0;
println("Rotationx reset");
}
if(rotationy <= 0) {
rotationy = initialRotationy;
cangle = 0;
println("Rotationy reset");
}
println("Rotating Camera");
}
rotationx = initialRotationx - (initialRotationx * sin(radians(cangle)));
rotationy = initialRotationy - (initialRotationy * cos(radians(cangle)));
camera(rotationx, rotationy, 0,
width/2, height/2, 0,
0, 0, -1.0);
cangle++;
println("Angle Incremented");
println("Angle = " + cangle);
if(rotationx <= -width/2) {
rotationx = initialRotationx;
cangle = 0;
println("Rotationx reset");
}
if(rotationy <= 0) {
rotationy = initialRotationy;
cangle = 0;
println("Rotationy reset");
}
println("Rotating Camera");
}
1