We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Multiple Boxes and shapes
Page Index Toggle Pages: 1
Multiple Boxes and shapes (Read 408 times)
Multiple Boxes and shapes
Feb 13th, 2010, 8:06pm
 
hello
i have this code which you will se that there is a grid showing some perspective lines

i want to place in the area of this grid multiple boxes. Basically i would like to have different sizes of boxes placed randomnly within the grid or in  a sequence. but first i d like to get them

i have tried doing that using the function below:

fill(230);

//  
 for(int i = 0; i < 5; i++){
   translate(450,-i*10,70);
 box(10,20,20);
 }

But unfortunately tha boxes appear in sequence outside the grid.
i an new to this so i took existing codes from libraries as you wil be able to see and then modified them

import processing.opengl.*;

// unlekkerLib - Marius Watz, workshop.evolutionzone.com
//
// Example using a 2-dimensional array of 3D vectors to calculate
// a mesh.

import unlekker.util.*;
import unlekker.geom.*;
import unlekker.data.*;

// 2-dimensional array of 3D vector objects to contain our mesh
Vec3 mesh[][];

// num will decide the mesh dimensions
int num;  



// use the CTRL key for interface
boolean ctrlDown=false;

// translation the Z direction
float xUnit,yUnit, zTrans;
float rotX,rotY,amplitude;
float tx,ty,tz;

void setup() {
 size(800,600,OPENGL);

 // allocate space for the mesh and fill it with empty vectors  
 num=100;
 mesh=new Vec3[num][num];
 meshPlane();
 
 xUnit=width/(float)num;
 yUnit=height/(float)num;

 rotX=30;
 rotY=45;
 amplitude=100;
 zTrans=-width/2;
 
// add a MouseWheelListener so we can use the mouse wheel
 // to zoom with
 frame.addMouseWheelListener(new MouseWheelInput());
 
}

void draw() {
 background(0);



 lights();
 
 // translate to center of screen and use mouse movement
 // to rotate the scene
 translate(width/2,height/2,zTrans);
 rotateX(rotX);
 rotateY(rotY);

 
 stroke(255);
 fill(100,23,65, 44);  
 drawMesh();
 
//FOR PERSPECTIVE LINES
for(int i = 0; i < 25; i++){
   // a rectangleDraw
   line(-500, -50, -i*20,  500, -50, -i*20);
   line( 500, -50, -i*20,  500,  50, -i*20);
   line( 500,  50, -i*20, -500,  50, -i*20);
   line(-500,  50, -i*20, -500, -50, -i*20);
   
   
   
//    line(-500, -250, (i*20)+25,  500, -250, (i*20)+25);
//    line( 500, -250, (i*20)+25,  500,  250, (i*20)+25);
//    line( 500,  250, (i*20)+25, -500,  250, (i*20)+25);
//    line(-500,  250, (i*20)+25, -500, -250, (i*20)+25);
//
//    

   

}



fill(230);

//  
 for(int i = 0; i < 5; i++){
   translate(450,-i*10,70);
 box(10,20,20);
 }
 

 
 
 
 
}


void keyPressed() {
 

 
 if(key==CODED) {
   // check to see if CTRL is pressed

   if(keyEvent.isControlDown()) {
     // do zoom in the Z axis
     if(keyCode==UP) tz=tz+2;
     if(keyCode==DOWN) tz=tz-2;
   }
   // check to see if CTRL is pressed
   else if(keyEvent.isShiftDown()) {
     // do translations in X and Y axis
     if(keyCode==UP) ty=ty-2;
     if(keyCode==DOWN) ty=ty+2;
     if(keyCode==RIGHT) tx=tx+2;
     if(keyCode==LEFT) tx=tx-2;
   }
   else {
     // do rotations around X and Y axis
     if(keyCode==UP) rotX=rotX+radians(2);
     if(keyCode==DOWN) rotX=rotX-radians(2);
     if(keyCode==RIGHT) rotY=rotY+radians(2);
     if(keyCode==LEFT) rotY=rotY-radians(2);
   }
 }
}
// utility class to handle mouse wheel events
class MouseWheelInput implements MouseWheelListener{
 void mouseWheelMoved(MouseWheelEvent e) {
   int step=e.getWheelRotation();
   zTrans=zTrans+step*50;
 }
}

void mouseDragged() {
 // if CTRL key is down, then adjust amplitude
 if(ctrlDown) {
   amplitude=((float)mouseY/(float)height)*height;
   println("amplitude "+amplitude);
 }
 // else rotate the viewport
 else {
   rotX=((float)mouseX/(float)width)*2*PI;
   rotY=((float)mouseY/(float)height)*2*PI;
 }
//  
}

// drawMesh is a custom method that takes care of drawing the
// mesh using beginShape() / endShape()
void drawMesh() {

 for(int i=0; i<num-1; i++) {
   beginShape(QUAD_STRIP);
   for(int j=0; j<num; j++) {
     vertex(mesh[j][i].x,mesh[j][i].y * amplitude ,mesh[j][i].z);
     vertex(mesh[j][i+1].x,mesh[j][i+1].y  * amplitude, mesh[j][i+1].z);
   }
   endShape();    
 }
}

// sets the mesh to an even grid plane
void meshPlane() {
 for(int i=0; i<num; i++)
   for(int j=0; j<num; j++)
     mesh[i][j]=new Vec3((i-num/2)*10,0,(j-num/2)*10);
}

// sets the mesh data to a Perlin noisefield
void meshNoise() {
 float x,y,a,aD,b,bD,val;

 // initialize parameters for the noise field.
 // "a" is our position in the X direction of the noise.
 // "b" is our position in the X direction of the noise.
 // "aD" and "bD" are used to traverse the noise field.
 a=random(1000);
 b=random(1000);
 aD=1.0/random(50,150);
 bD=1.0/random(50,150);

 // set amplitude and noiseDetail for noise field
 noiseDetail((int)random(4,8),random(0.4,0.9));

 for(int i=0; i<num; i++)
   for(int j=0; j<num; j++) {

     // calculate height as a function of 2D noise
     val=(noise(a+aD*(float)i,b+bD*(float)j)-0.5)*2;

     x=((float)i-num/2)*xUnit;;
     y=((float)j-num/2)*yUnit;
     mesh[i][j].set(x,val,y);

   }
}




i would also like to have those lines on top going to the other end og the grid but i cannot figure out the calculation.

if anyone can help it ll be fantastic

thanks!

Re: Multiple Boxes and shapes
Reply #1 - Feb 14th, 2010, 2:30am
 
all those translates() add up - if you translate(0, 10) and draw something then add another translate(0, 10) and draw something the second thing will be translated twice.

so this:
for(int i = 0; i < 5; i++){
 translate(450,-i*10,70);
 box(10,20,20);
}
will just translate and translate and translate...

the way around this is pushMatrix() and popMatrix().

http://processing.org/reference/pushMatrix_.html
Page Index Toggle Pages: 1