We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › rotation-axis in center of cube
Page Index Toggle Pages: 1
rotation-axis in center of cube (Read 429 times)
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();
}
}
}
Re: rotation-axis in center of cube
Reply #1 - Nov 30th, 2007, 1:25am
 
Since your major cube (the big cube of minor cubes) is built outward from 0,0,0 it'll rotate on that back edge unless you translate it elsewhere (or change where it's built).  Something like this in draw():

// go to center of screen and eye distance
translate(width/2f, height/2f);
// apply rotation from HERE
rotateY(whatever);
// now offset the major box by half its overall size
// (check this math: half of 8 boxes + 7 spaces, right?)
float offset = ((segmentation * size) + ((segmentation-1) * space)) / 2f;
translate(-offset,-offset,-offset);
// then draw the minor cubes from here and
// they'll appear rotated around major cube center
Re: rotation-axis in center of cube
Reply #2 - Nov 30th, 2007, 2:17am
 
Hey davbol, it works! Thanks for your help! I think it's too late and I have to sleep now. Don't know why I didn't get it for hours. Such an easy thing. Anyway, good night!
Page Index Toggle Pages: 1