Hello folks,
I've been messing around with a simple enough sketch that creates a 2d array of tetrahedrons, and I want one to rotate around their own centerpoint if the mouse rolls over it - so as a mouse travels across the screen it will cause any tetrahedrons in the cursors path to begin spinning. All is working fine enough so far, apart from the old translation/rotation conflict. The first tetrahedron in the array (ie the one at x=0, y=0) rotates on it's own axis, but the further down the array you get the rotation is on a wider arc. Is there a simple way to fix this issue?
Here's my code. Feel free to make any other suggestions where necessary!
I've been messing around with a simple enough sketch that creates a 2d array of tetrahedrons, and I want one to rotate around their own centerpoint if the mouse rolls over it - so as a mouse travels across the screen it will cause any tetrahedrons in the cursors path to begin spinning. All is working fine enough so far, apart from the old translation/rotation conflict. The first tetrahedron in the array (ie the one at x=0, y=0) rotates on it's own axis, but the further down the array you get the rotation is on a wider arc. Is there a simple way to fix this issue?
Here's my code. Feel free to make any other suggestions where necessary!
- import processing.opengl.*;
- int num = 18;
- int edgeLength = 15;
- Tetrahedron tetrahedrons[][] = new Tetrahedron[num][num];
- void setup(){
- size(800, 800, OPENGL);
- smooth();
- for(int i=0; i<num; i++){
- for(int j=0; j<num; j++){
- tetrahedrons[i][j] = new Tetrahedron(i*50, j*50, 0, edgeLength);
- }
- }
- }
- void draw(){
- background(0);
- noFill();
- for(int i=0; i<num; i++){
- for(int j=0; j<num; j++){
- pushMatrix();
- tetrahedrons[i][j].render();
- popMatrix();
- }
- }
- }
- class Tetrahedron{
- int x, y, z;
- int edgeLength;
- float theta;
- boolean selected=false;
- Tetrahedron(int ix, int iy, int iz, int iedgeLength){
- x = ix;
- y = iy;
- z = iz;
- edgeLength = iedgeLength;
- }
- void makeShape(){
- stroke(255, 0, 0);
- beginShape(TRIANGLE_STRIP);
- vertex(edgeLength+x, edgeLength+y, edgeLength+z);
- vertex(-edgeLength+x, -edgeLength+y, edgeLength+z);
- vertex(-edgeLength+x, edgeLength+y, -edgeLength+z);
- vertex(edgeLength+x, -edgeLength+y, -edgeLength+z);
- vertex(edgeLength+x, edgeLength+y, edgeLength+z);
- vertex(-edgeLength+x, -edgeLength+y, edgeLength+z);
- endShape(CLOSE);
- }
- void render(){
- switchOn();
- makeShape();
- }
- void switchOn(){
- if(selected(x, y, edgeLength)){
- stroke(255, 0, 0);
- beginSpin();
- }else{
- stroke(0, 0, 255);
- }
- }
- void beginSpin(){
- theta = frameCount/TWO_PI*0.2;
- rotateX(theta);
- rotateY(theta*0.5);
- }
- boolean selected(int x, int y, int edgeLength){
- if(mouseX >= x && mouseX <= x+edgeLength &&
- mouseY >= y && mouseY <= y+edgeLength){
- return true;
- }
- else{
- return false;
- }
- }
- }
1