TobiTobsen
YaBB Newbies
Offline
Posts: 6
Rotating a cube with mouse, works but...
May 11th , 2010, 12:10pm
Hey, I'm a new member in this forum, I want to try a few things with Processing. I'm a Java developer mostly dealing with Interactive-devices. Lets see if there is an easy solution for my question. I want to rotate a cube with the mouse. This works quite well, but if I rotate for example around the y axis, then around x axis, I want this to happen step by step. Right now I allways rotate around the origin of the cube, not around its new position. Hmm I hope you understand this...This is a well known issue for me, but maybe there is an easy solution... Here is my script: package impl; import java.util.Vector; import processing.core.PApplet; public class RotateCube extends PApplet { int cubeXpos; int cubeYpos; int cubeSize; float cubeXrot = 1.0f; float cubeYrot = 1.0f; int lastPointerX = 0; int lastPointerY = 0; static int defValLastX = 9999; static int threshold = 0; public void setup(){ size(640, 360, P3D); background(100); lights(); stroke(255,0,0); cubeXpos = width/2; cubeYpos = height/2; cubeSize = 100; pushMatrix(); translate(cubeXpos, cubeYpos, 0); rotateY(cubeXrot); rotateX(cubeYrot); box(100); popMatrix(); line(0, height/2, width, height/2 ); line(width/2, 0, width/2, height ); } public void draw(){ } public void mouseDragged() { if( mouseX > (cubeXpos-(cubeSize/2)) && mouseX <(cubeXpos-(cubeSize/2)) + cubeSize && mouseY > (cubeYpos-(cubeSize/2)) && mouseY <(cubeYpos-(cubeSize/2)) + cubeSize){ if(lastPointerX != this.defValLastX){ /* Checking X Movement */ if(lastPointerX > mouseX + threshold){ this.cubeXrot = cubeXrot - 0.05f; } else if (lastPointerX < mouseX - threshold){ this.cubeXrot = cubeXrot + 0.05f; } /* Checking Y Movement */ if(lastPointerY > mouseY + threshold){ this.cubeYrot = cubeYrot + 0.05f; } else if (lastPointerY < mouseY - threshold){ this.cubeYrot = cubeYrot - 0.05f; } background(100); pushMatrix(); translate(cubeXpos, cubeYpos, 0); rotateY(cubeXrot); rotateX(cubeYrot); box(100); popMatrix(); } lastPointerX = mouseX; lastPointerY = mouseY; } else{ lastPointerX = defValLastX; lastPointerY = defValLastX; } } } edit: There are two other questions that came into my mind: Is it possible to map a different image on each side of a cube? Is it possible to develop really large resolutions like four times 1920*1080 as one big desktop (using the span mode) with processing? Thanks a lot! Cheers, Tobi