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 › array of moving solids
Page Index Toggle Pages: 1
array of moving solids (Read 695 times)
array of moving solids
Apr 20th, 2010, 5:31pm
 
Hello processing community,

As a good tinkerer, ive been bumping my head on it the whole day. Using this http://processing.org/learning/3d/shapetransform.html.
i would like to make an array of those solids moving on a 2D space, like the little red dots. It just does it for the first solid, so i guess this is an issue with the for... loop. Can you help me out

Code:
int pts = 3, i;
float angle = 0;
float radius = 10;
float cylinderLength = 10;
//vertices
PVector vertices[][];
boolean isPyramid = false;
float angleInc;

int max_Agents = 100, Agents = 20;
float inc = 0.0045; // speed

float[] ptsX = new float[max_Agents];
float[] ptsY = new float[max_Agents];
float[] posX = new float[max_Agents];
float[] posY = new float[max_Agents];  

int frameX = 640, frameY = 480;

void setup(){
 size(frameX, frameY, P3D);
 noStroke();

 angleInc = PI/300.0;
 
   // set arrays
 for (int i = 0; i < max_Agents; i++) {
   ptsX[i] = random(0, frameX);   // write start pts to arrayX
   ptsY[i] = random(0, frameY);   // write start pts to arrayY
   posX[i] = ptsX[i];
   posY[i] = ptsY[i];
 }
}

void draw(){
 background(0);
 lights();
 fill(255,0,0);
 stroke(1);

 for (int i = 0; i < Agents; i++) {
   posX[i] = noise(ptsX[i]) * frameX;
   posY[i] = noise(ptsY[i]) * frameY;

   ptsX[i] = ptsX[i] + inc;
   ptsY[i] = ptsY[i] + inc;
   
   ellipse(posX[i], posY[i], 10 , 10 );  
 }
   
 translate(posX[i],posY[i]);
 rotateX(frameCount*PI/185);
 rotateY(frameCount*PI/-200);
 vertices = new PVector[2][pts+1];
 fill(200,200);

 // fill arrays
 for (int m = 0; m < 2; m++){
   for(int n = 0; n <= pts; n++){
     vertices[m][n] = new PVector();
     if (isPyramid){
       if (m==1){
         vertices[m][n].x = 0;
         vertices[m][n].y = 0;
       }
       else {
         vertices[m][n].x = cos(radians(angle)) * radius;
         vertices[m][n].y = sin(radians(angle)) * radius;
       }
     }
     else {
       vertices[m][n].x = cos(radians(angle)) * radius;
       vertices[m][n].y = sin(radians(angle)) * radius;
     }
           
     vertices[m][n].z = cylinderLength;
     // the .0 after the 360 is critical
     angle += 360.0/pts;
   }
   cylinderLength *= -1;      
 }
 // draw cylinder tube
 beginShape(QUAD_STRIP);
 for(int n = 0; n <= pts; n++){
   vertex(vertices[0][n].x, vertices[0][n].y, vertices[0][n].z);
   vertex(vertices[1][n].x, vertices[1][n].y, vertices[1][n].z);
 }
 endShape();

 //draw cylinder ends
 for (int m = 0; m < 2; m++){
   beginShape();
   for(int n = 0; n < pts; n++){
     vertex(vertices[m][n].x, vertices[m][n].y, vertices[m][n].z);
   }
   endShape(CLOSE);
 }      
 }
Re: array of moving solids
Reply #1 - Apr 20th, 2010, 6:25pm
 
oh yes. i didnt find any connection between the ellipses and the solids until i rearranged the code a little bit. class would help you with keeping the overview.

you really just draw only a single solid. its at the position of the last ball,  because after the loop you translate to its position.
so make translation,rotation for all agents. pushing and poping the transformation.

ramin

Code:

int PTS = 3;
float angle = 0;
float radius = 10;
float cylinderLength = 10;
//vertices
PVector vertices[][];
boolean isPyramid = false;
float angleInc;

int max_Agents = 100, Agents = 20;
float inc = 0.0045; // speed

Agent[] agents = new Agent[max_Agents];

int frameX = 640, frameY = 480;

void setup(){
size(frameX, frameY, P3D);
noStroke();

angleInc = PI/300.0;

// set arrays
for (int i = 0; i < max_Agents; i++) {
agents[i] = new Agent();
}
}

void draw(){
background(0);
lights();
fill(255,0,0);
stroke(1);

for (int i = 0; i < Agents; i++) {
agents[i].render();
}

}

class Agent {

PVector pos,pts;

public Agent() {
pos = new PVector(random(frameX),random(frameY));
pts = new PVector(pos.x,pos.y);
}

void render() {
pos.set(noise(pts.x)* frameX,noise(pts.y)* frameY,0);

pts.add(inc,inc,0);

ellipse(pos.x, pos.y, 10 , 10 );

pushMatrix();
translate(pos.x,pos.y);
rotateX(frameCount*PI/185);
rotateY(frameCount*PI/-200);
vertices = new PVector[2][PTS+1];
fill(200,200);

// fill arrays
for (int m = 0; m < 2; m++){
for(int n = 0; n <= PTS; n++){
vertices[m][n] = new PVector();
if (isPyramid){
if (m==1){
vertices[m][n].x = 0;
vertices[m][n].y = 0;
}
else {
vertices[m][n].x = cos(radians(angle)) * radius;
vertices[m][n].y = sin(radians(angle)) * radius;
}
}
else {
vertices[m][n].x = cos(radians(angle)) * radius;
vertices[m][n].y = sin(radians(angle)) * radius;
}

vertices[m][n].z = cylinderLength;
// the .0 after the 360 is critical
angle += 360.0/PTS;
}
cylinderLength *= -1;
}
// draw cylinder tube
beginShape(QUAD_STRIP);
for(int n = 0; n <= PTS; n++){
vertex(vertices[0][n].x, vertices[0][n].y, vertices[0][n].z);
vertex(vertices[1][n].x, vertices[1][n].y, vertices[1][n].z);
}
endShape();

//draw cylinder ends
for (int m = 0; m < 2; m++){
beginShape();
for(int n = 0; n < PTS; n++){
vertex(vertices[m][n].x, vertices[m][n].y, vertices[m][n].z);
}
endShape(CLOSE);
}
popMatrix();
}



}





Re: array of moving solids
Reply #2 - Apr 25th, 2010, 6:47pm
 
Thank you very much Ramin, this helped me quite a lot.
I wonder though : using this class structure with a vector, can i still connect all the balls / volume with a network of lines. I did it using this nested loop on the previous code :

 for (int i = 0; i < Agents; i++) {
   for (int j = i; j < Agents; j++) {
     if (dist(posX[j], posY[j], posX[i], posY[i]) < 50) {
       line(posX[j], posY[j], posX[i], posY[i]);
     }
   }
 }

I would like to know why you rename the coordinate as pos.x/pos.y and if i can still write something like pos.x[i].

thanks!
Page Index Toggle Pages: 1