Andy_Pipkin
YaBB Newbies
Offline
Posts: 3
rotation-axis in center of cube
Nov 29th , 2007, 10:55pm
Hello everyone, I'm new to processing, but already got some Java exeriences. It's really impressive to see how processing makes many things remarkably easier in comparison to pure java. Finally, you can focus on arts. That's how it should be! I'm working on a project for University. Actually, it's a Java project. But I'm going to pimp the GUI with processing ;-) The backend stuff is pure Java. Due to this my code isn't straight processing. I hope it's o.k. to post it like that. In my application the user should be able to rotate the RGB-Cube around it's y-axis by pressing the left or right arrow keys. My problem is that the rotation axis isn't placed in the center, but in the backmost left corner of the cube (the green area). I tried many things regarding to the pushMatrix() and popMatrix() methods combined with translate() to avoid the described effect. But without success, after all. I'd be very grateful if you help me solving the issue. import processing.core.*; public class RGBCubeSegments extends PApplet { private int segmentation, space; private ColorCube[][][] cubeSegments; private int yRotation; static public void main(String args[]) { PApplet.main(new String[] {"RGBCubeSegments"}); } public void setup() { size(768, 576, OPENGL); frameRate(24); colorMode(RGB); smooth(); yRotation = 0; segmentation = 8; space = 10; cubeSegments = new ColorCube[segmentation][segmentation][segmentation]; int size = 256/segmentation; int cubeX = 0, cubeY = 0, cubeZ = 0; int r = 0, g = 0, b = 0; for(int x = 0; x < cubeSegments.length; x++) { for(int y = 0; y < cubeSegments[x].length; y++) { for(int z = 0; z < cubeSegments[x][y].length; z++) { cubeSegments[x][y][z] = new ColorCube(cubeX, cubeY, cubeZ, size); cubeSegments[x][y][z].setRGBRange(r, g, b); cubeZ += size + space; b += size; } cubeY += size + space; g += size; cubeZ = 0; b = 0; } cubeX += size + space; r += size; cubeY = 0; g = 0; } } public void draw() { background(0); translate(225, 125, -300); rotateY(radians(yRotation)); rotateCube(); pushMatrix(); for(int x = 0; x < cubeSegments.length; x++) { for(int y = 0; y < cubeSegments[x].length; y++) { for(int z = 0; z < cubeSegments[x][y].length; z++) { cubeSegments[x][y][z].drawCube(); } } } popMatrix(); } public void rotateCube() { if(keyPressed) { if (key == CODED && keyCode == LEFT) { yRotation -= 1; rotateY(radians(- 1)); } else if(key == CODED && keyCode == RIGHT) { yRotation += 1; rotateY(radians(1)); } } } private class ColorCube { private int x, y, z, size; private int r, g, b; ColorCube(int x, int y, int z, int size) { this.x = x; this.y = y; this.z = z; this.size = size; r = 0; g = 0; b = 0; } private void setRGBRange(int r, int g, int b) { this.r = r; this.g = g; this.b = b; } private void drawCube() { noStroke(); beginShape(QUADS); //front face fill(r, g + size - 1, b + size - 1); vertex(-size/2 + x, size/2 + y, size/2 + z); fill(r + size - 1, g + size - 1, b + size - 1); vertex(size/2 + x, size/2 + y, size/2 + z); fill(r + size - 1, g, b + size - 1); vertex(size/2 + x, -size/2 + y, size/2 + z); fill(r, g, b + size - 1); vertex(-size/2 + x, -size/2 + y, size/2 + z); //back face fill(r + size - 1, g + size - 1, b + size - 1); vertex(size/2 + x, size/2 + y, size/2 + z); fill(r + size - 1, g + size - 1, b); vertex(size/2 + x, size/2 + y, -size/2 + z); fill(r + size - 1, g, b); vertex(size/2 + x, -size/2 + y, -size/2 + z); fill(r + size - 1, g, b + size - 1); vertex(size/2 + x, -size/2 + y, size/2 + z); //left face fill(r + size - 1, g + size - 1, b); vertex(size/2 + x, size/2 + y, -size/2 + z); fill(r, g + size - 1, b); vertex(-size/2 + x, size/2 + y, -size/2 + z); fill(r, g, b); vertex(-size/2 + x, -size/2 + y, -size/2 + z); fill(r + size - 1, g, b); vertex(size/2 + x, -size/2 + y, -size/2 + z); //right face fill(r, g + size - 1, b); vertex(-size/2 + x, size/2 + y, -size/2 + z); fill(r, g + size - 1, b + size - 1); vertex(-size/2 + x, size/2 + y, size/2 + z); fill(r, g, b + size - 1); vertex(-size/2 + x, -size/2 + y, size/2 + z); fill(r, g, b); vertex(-size/2 + x, -size/2 + y, -size/2 + z); //top face fill(r, g + size - 1, b); vertex(-size/2 + x, size/2 + y, -size/2 + z); fill(r + size - 1, g + size - 1, b); vertex(size/2 + x, size/2 + y, -size/2 + z); fill(r + size - 1, g + size - 1, b + size - 1); vertex(size/2 + x, size/2 + y, size/2 + z); fill(r, g + size - 1, b + size - 1); vertex(-size/2 + x, size/2 + y, size/2 + z); //bottom face fill(r, g, b); vertex(-size/2 + x, -size/2 + y, -size/2 + z); fill(r + size - 1, g, b); vertex(size/2 + x, -size/2 + y, -size/2 + z); fill(r + size - 1, g, b + size - 1); vertex(size/2 + x, -size/2 + y, size/2 + z); fill(r, g, b + size - 1); vertex(-size/2 + x, -size/2 + y, size/2 + z); endShape(); } } }