import processing.opengl.*; Grid grid; Interface programInterface; float currentAngleX, currentAngleY, currentAngleZ; float currentPositionX, currentPositionY, currentPositionZ; int tempAngleX = 0, tempAngleY = 0, tempX = 0, tempY = 0; float tempPositionX = 0, tempPositionY = 0, tempPositionZ = 0; boolean ALT_PRESSED = false, CTRL_PRESSED = false; void setup() { size(500,500, OPENGL); grid = new Grid(20.0, 10,10); programInterface = new Interface(); currentAngleX = -45; currentAngleY = 45; currentAngleZ = 0; currentPositionX = width/2; currentPositionY = height/2; currentPositionZ = 0; } void draw() { background(150); // GET ALL VALUES FROM PROGRAM INTERFACE ###################################################### float rollForward = (float)programInterface.sliders[0].value; float moveUp = (float)programInterface.sliders[1].value; float rollBack = (float)programInterface.sliders[2].value; // ############################################################################################ pushMatrix(); translate(currentPositionX, currentPositionY, currentPositionZ); rotateX(radians(currentAngleX)); rotateY(radians(currentAngleY)); rotateZ(radians(currentAngleZ)); grid.showGrid(); pushMatrix(); translate(0,-moveUp,0); rotateZ(radians(rollBack)); translate(-50,0,0); pushMatrix(); // BEGIN TOE ###################################################################### // check toe when roll forward --------------------------------------- if (rollForward != 0) { float angle = rollForward * (moveUp / rollForward)*3.0; if (angle > rollForward) rotateZ(-radians(rollForward)); else rotateZ(-radians(angle)); } // ----------------------------------------------------------------- translate(-10,-10,0); box(20,20,20); popMatrix(); // END TOE ######################################################################### pushMatrix(); // BEGIN FOOT ##################################################################### rotateZ(-radians(rollForward)); translate(25,-10,0); box(50,20,20); popMatrix(); // END FOOT ######################################################################## popMatrix(); popMatrix(); programInterface.showInterface(); } void keyPressed() { if (keyCode == ALT) ALT_PRESSED = true; if (keyCode == CONTROL) CTRL_PRESSED = true; if (key == 'l') // SET VIEW TO LEFT-SIDE { currentPositionX = 250; currentPositionY = 270; currentPositionZ = 250; currentAngleX = 0.0; currentAngleY = -1.0; currentAngleZ = 0.0; } mousePressed(); } void keyReleased() { ALT_PRESSED = false; CTRL_PRESSED = false; } void mouseReleased() { programInterface.mouseReleased(); } void mousePressed() { tempAngleX = (int)currentAngleX; tempPositionX = currentPositionX; tempY = mouseY; tempAngleY = (int)currentAngleY; tempPositionY = currentPositionY; tempX = mouseX; tempPositionZ = currentPositionZ; programInterface.mousePressed(); } void mouseDragged() { if (ALT_PRESSED && !CTRL_PRESSED) { currentAngleX = tempAngleX + tempY - mouseY; currentAngleY = tempAngleY + mouseX - tempX; } else if (CTRL_PRESSED && !ALT_PRESSED) { currentPositionX = tempPositionX + mouseX - tempX; currentPositionY = tempPositionY + mouseY - tempY; } else if (ALT_PRESSED && CTRL_PRESSED) { currentPositionZ = tempPositionZ + tempY - mouseY; } programInterface.mouseDragged(); } class Interface { Slider[] sliders; PFont font; String tempRow = "ALT + LeftMouse = ROTATE\nCTRL + LeftMouse = TRANSLATE\nALT+CTRL + LeftMouse = ZOOM IN/OUT\nL = Left Side"; public Interface() { font = loadFont("interfaceFont.vlw"); textFont(font); sliders = new Slider[3]; sliders[0] = new Slider(30,450,100,10,"Roll Forward",90); sliders[1] = new Slider(30,465,100,10,"Move Up",100); sliders[2] = new Slider(30,480,100,10,"Roll Back",90); } public void showInterface() { text(tempRow, 0, 10); for (int k = 0; k < sliders.length; k++) sliders[k].mostra(); } public void mousePressed() { for (int k = 0; k < sliders.length; k++) sliders[k].premuto = sliders[k].cliccato(); } public void mouseDragged() { for (int k = 0; k < sliders.length; k++) if (sliders[k].premuto) sliders[k].move(mouseX); } public void mouseReleased() { for (int k = 0; k < sliders.length; k++) sliders[k].premuto = false; } } class Grid { float offsetX, offsetY, offsetZ; float unit; int cellW, cellH; int intensityGray = 100; public Grid(float unit, int cellW, int cellH) { this.unit = unit; this.cellW = cellW; this.cellH = cellH; offsetX = (cellW*unit)/2; offsetY = (cellH*unit)/2; offsetZ = 0; } public void showGrid() { pushMatrix(); rotateX(radians(90)); stroke(intensityGray); for (int cw = 0; cw <= cellW; cw++) line(cw*unit - offsetX, -offsetY, cw*unit - offsetX, cellH*unit - offsetY); for (int ch = 0; ch <= cellH; ch++) line(-offsetX, ch*unit - offsetY, cellW*unit - offsetX, ch*unit - offsetY); popMatrix(); } public void showGridText() { for (int cw = 0; cw <= cellW; cw++) text(""+cw, cw*unit - offsetX, -offsetY); for (int ch = 0; ch <= cellH; ch++) text(""+ch, -offsetX, ch*unit - offsetY); } } class Slider { int x, y,cx, w, h, maxv, value; String nome; boolean premuto = false; Slider(int x, int y, int w, int h, String nome, int maxv) { this.x = x; this.cx = x; this.y = y; this.w = w; this.h = h; this.maxv = maxv; this.nome = nome; value = 0; } void move(int x) { cx = x; if (cx < this.x) cx = this.x; if (cx > this.x + w) cx = this.x + w; value = (cx-this.x)*maxv/(w); } void mostra() { stroke(255); line(x,y,x+w,y); fill(255); stroke(0); rect(cx-3, y-h/2, 6, h); stroke(255); text(nome, x+w+3,y+3); } boolean cliccato() { if (mouseX > cx-3 && mouseX < cx+3 && mouseY > y-h/2 && mouseY < y+h/2) return true; else return false; } }