jher
YaBB Newbies
Offline
Posts: 21
Re: rects with rounded corners & rotation
Reply #6 - Feb 7th , 2008, 2:39pm
Thanks guys... Im trying to zoom into these rects and the rects in the center of the screen look really good when they zoom up, but when the ones ones on the side of the screen zoom to the left or the right, and I cant seem to make them zoom up without perspective. I am using the z parameter of the translate method. If I scale the width and height instead of modifying the z parameter of the translate, then it looks right until I rotate the rect and there is some clipping against the neighboring cell. below is my code float SELECTED_WIDTH = 300, SELECTED_HEIGHT = 220; float STANDARD_WIDTH = 215, STANDARD_HEIGHT = 157; boolean timerSet = false; Cell[] cellArray = new Cell[20]; int selectedIndex; void setup() { size(1280, 720, P3D); bezierDetail(50); // ortho(0, width, 0, height, -100, 100); float xPos = 0; float yPos = 0; selectedIndex = 0; //strokeCap(ROUND); stroke(255,255,255); for(int i=0; i < cellArray.length; i++) { if (xPos > (width - STANDARD_WIDTH)) { xPos =0; yPos += STANDARD_HEIGHT+3; } xPos += STANDARD_WIDTH+3; cellArray[i] = new Cell( (int)xPos, (int)yPos); } cellArray[selectedIndex].isHovered = true; } void draw() { background(0); translate(-10, 115); for(int i=0; i < cellArray.length; i++) { if(i != selectedIndex )cellArray[i].update(); } cellArray[selectedIndex].update(); } void keyPressed() { if(keyCode == 10) setSelection(); if (key == CODED) { switch(keyCode) { case LEFT: changeSelection(-1); break; case RIGHT: changeSelection(1); break; case 10: setSelection(); break; case DOWN: changeSelection(5); break; case UP: changeSelection(-5); break; } } } void changeSelection(int delta) { if(selectedIndex + delta >= 0 && selectedIndex + delta < cellArray.length) { cellArray[selectedIndex].isHovered = false; cellArray[selectedIndex].isSelected = false; selectedIndex += delta; cellArray[selectedIndex].isHovered = true; } } void setSelection() { cellArray[selectedIndex].isSelected = true; } class Cell { float xPos, yPos; boolean isSelected = false, isHovered = false; float cellWidth, cellHeight; float rotationVal = 0.0; float zoomLevel = 0.0; int timeZoomed=-1; Cell(int x, int y) { xPos = x; yPos = y; cellWidth = STANDARD_WIDTH; cellHeight = STANDARD_HEIGHT; } void update() { if(isHovered) { zoom(); } else { cellWidth = STANDARD_WIDTH; cellHeight = STANDARD_HEIGHT; unZoom(); } if(isSelected) rotate(); else unRotate(); draw(); } void rotate() { //ortho(-width/2, width/2, -height/2, height/2, -10, 10); rotationVal += .2; if(rotationVal > PI/2) fill(255); else fill(100); if(rotationVal > PI) rotationVal = PI; } void unRotate() { //ortho(-width/2, width/2, -height/2, height/2, -10, 10); rotationVal -= .2; if(rotationVal < PI/2) fill(100); else fill(255); if(rotationVal < 0) rotationVal = 0; } void zoom() { zoomLevel += 3; if(zoomLevel > 100) { zoomLevel = 100; if(!timerSet) { timeZoomed = millis(); timerSet = true; println(timeZoomed); } else if(millis() - timeZoomed >= 5000) { this.isSelected = true; timerSet =false; } } /*cellWidth += (SELECTED_WIDTH - cellWidth)/5; if(cellWidth > SELECTED_WIDTH) cellWidth = SELECTED_WIDTH; cellHeight += (SELECTED_HEIGHT - cellHeight)/5; if(cellHeight > SELECTED_HEIGHT) cellHeight = SELECTED_HEIGHT; */ } void unZoom() { zoomLevel -= 6; if(zoomLevel < 0) { zoomLevel = 0; } /*cellWidth += (SELECTED_WIDTH - cellWidth)/5; if(cellWidth > SELECTED_WIDTH) cellWidth = SELECTED_WIDTH; cellHeight += (SELECTED_HEIGHT - cellHeight)/5; if(cellHeight > SELECTED_HEIGHT) cellHeight = SELECTED_HEIGHT; */ } void draw() { pushMatrix(); translate(xPos, yPos, zoomLevel); rotateY(rotationVal); strokeWeight(3); bezierRect(-cellWidth/2, -cellHeight/2, cellWidth, cellHeight, -10, -10); popMatrix(); } void bezierRect(float x, float y, float w, float h, float xr, float yr) { float w2=w/2f, h2=h/2f, cx=x+w2, cy=y+h2; beginShape(); vertex(cx,cy-h2); bezierVertex(cx+w2-xr, cy-h2, cx+w2, cy-h2+yr, cx+w2, cy); bezierVertex(cx+w2, cy+h2-yr, cx+w2-xr, cy+h2, cx, cy+h2); bezierVertex(cx-w2+xr, cy+h2, cx-w2, cy+h2-yr, cx-w2, cy); bezierVertex(cx-w2, cy-h2+yr, cx-w2+xr, cy-h2, cx, cy-h2); endShape(); } }