Rotating a composite shape, made of many cubes, around a single center point
in
Programming Questions
•
1 year ago
Obviously new to this, and the solution is probably obvious, too, but I've tried every placement of the rotate and translate functions I can think of.
I'm just trying to make the entire level-2 fractal shape rotate around its own center point in the middle of the screen. As it is, the whole shape rotates around 0, 0, 0, or at least appears to. Code follows below. Thanks for any help.
// level-2 mosely fractal assembler
color c = color (0, 200, 20, 100);
int cubeSize = 40;
float rX;
float rY;
MoselyTwo moselySecond;
void setup () {
size (1200, 800, OPENGL);
smooth ();
moselySecond = new MoselyTwo (width / 2, height / 2, 0);
}
void draw () {
background (10);
pushMatrix ();
rotateX (rX);
rotateY (rY);
moselySecond.displayTwo ();
popMatrix ();
rX = rX + .005;
rY = rY + .005;
}
class Cube {
int cubeX; // final cube x position relative to sponge center
int cubeY; // final cute y position relative to sponge center
int cubeZ; // final cube z position relative to sponge center
int holdTime; // wait time in millis before displaying next cube
Cube (int tempCubeX, int tempCubeY, int tempCubeZ, int tempHoldTime) {
cubeX = tempCubeX;
cubeY = tempCubeY;
cubeZ = tempCubeZ;
holdTime = tempHoldTime;
}
void display () {
pushMatrix ();
translate (cubeX, cubeY, cubeZ);
fill (c);
box (cubeSize);
popMatrix ();
}
}
class MoselyOne {
int spongeOneCenterX;
int spongeOneCenterY;
int spongeOneCenterZ;
int cubeID;
int [] cubeCentersInOne = {-1, 0, 0, 1, 0, 0, -1, 0, -1, 0, 0, -1, 1, 0, -1,
-1, 0, 1, 0, 0, 1, 1, 0, 1, -1, 1, 0, 0, 1, 0,
1, 1, 0, -1, -1, 0, 0, -1, 0, 1, -1, 0, 0, 1, -1,
0, 1, 1, 0, -1, -1, 0, -1, 1};
Cube [] cubesInOne = new Cube [18];
MoselyOne (int tempSpongeOneCenterX, int tempSpongeOneCenterY, int tempSpongeOneCenterZ) {
spongeOneCenterX = tempSpongeOneCenterX;
spongeOneCenterY = tempSpongeOneCenterY;
spongeOneCenterZ = tempSpongeOneCenterZ;
for (int i = 0; i < cubeCentersInOne.length - 1; i = i + 3) {
cubesInOne [cubeID] = new Cube (spongeOneCenterX + cubeCentersInOne [i] * cubeSize, spongeOneCenterY + cubeCentersInOne [i + 1] * cubeSize,
spongeOneCenterZ + cubeCentersInOne [i + 2] * cubeSize, 0);
cubeID = cubeID + 1;
}
}
void displayOne () {
for (int i = 0; i <= cubesInOne.length - 1; i ++) {
cubesInOne [i].display ();
}
}
}
class MoselyTwo {
int spongeTwoCenterX;
int spongeTwoCenterY;
int spongeTwoCenterZ;
int oneID;
int [] oneCentersInTwo = {-1, 0, 0, 1, 0, 0, -1, 0, -1, 0, 0, -1, 1, 0, -1,
-1, 0, 1, 0, 0, 1, 1, 0, 1, -1, 1, 0, 0, 1, 0,
1, 1, 0, -1, -1, 0, 0, -1, 0, 1, -1, 0, 0, 1, -1,
0, 1, 1, 0, -1, -1, 0, -1, 1};
MoselyOne [] spongesInTwo = new MoselyOne [18];
MoselyTwo (int tempSpongeTwoCenterX, int tempSpongeTwoCenterY, int tempSpongeTwoCenterZ) {
spongeTwoCenterX = tempSpongeTwoCenterX;
spongeTwoCenterY = tempSpongeTwoCenterY;
spongeTwoCenterZ = tempSpongeTwoCenterZ;
for (int i = 0; i < oneCentersInTwo.length - 1; i = i + 3) {
spongesInTwo [oneID] = new MoselyOne (spongeTwoCenterX + oneCentersInTwo [i] * cubeSize * 3, spongeTwoCenterY + oneCentersInTwo [i + 1] * cubeSize * 3,
spongeTwoCenterZ + oneCentersInTwo [i + 2] * cubeSize * 3);
oneID = oneID + 1;
}
}
void displayTwo () {
for (int i = 0; i <= spongesInTwo.length - 1; i ++) {
spongesInTwo [i].displayOne ();
}
}
}
1