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.
Page Index Toggle Pages: 1
sphere + translate (Read 1216 times)
sphere + translate
Nov 7th, 2008, 1:28pm
 
hey!
I'm trying to make a grid of spheres. if a change the sphere to rect they all tile perfect but when i'm using the sphere it doesn't even look like a grid..the difference is that i don't have to use the translate command, and think it's the one messing all up..
i've created a class,

class Sphere{
float xCor;
float yCor;
float zCor;
Sphere(float xCor, float yCor, float zCor, int id){
    this.xCor = xCor;
    this.yCor = yCor;
    this.zCor = zCor;
    this.id = id;
  }
  void show(){
    translate(xCor,yCor,zCor);
    sphere(rad);
  }    
}
from main:
//init
for(int i=0; i<arrSize; i++){
   for(int j=0; j<arrSize; j++){
     Sphere s = new Ball(xCor,yCor,zCor,id);
     id++;
     sphereArr[i][j] = s;
     xCor+=50;
   }
   xCor=0;
   yCor+=50;
}  
void draw(){
for(int k=0; k<arrSize; k++){
   for(int l=0; l<arrSize; l++){
      sphereArr[k][l].show();
   }
 }
}

can anyone see what i have done wrong?
Regards
_n


}
Re: sphere + translate
Reply #1 - Nov 7th, 2008, 3:32pm
 
Hi,

It's slightly difficult to diagnose with complete code, but it sounds like pushMatrix() and popMatrix() before and after translate commands might help?

import processing.opengl.*;

// Very slow on my machine if the number of items is large
int numItems = 10;
int row, col;

void setup()
{
 size(800, 800, OPENGL);
 row = width/numItems;
 col = height/numItems;
}

void draw()
{
 background(255);
 lights();
 fill(255, 0, 0);  
 noStroke();
 for(int r = row*2; r < width - row; r+=row)
 {
   for(int c = col*2; c < height - col; c+=row)
   {
     pushMatrix();
     translate(r, c);
     // These rotates aren't required but are a nice effect
     // the order they appear in relation to translate and push/pop
     // is important
     //rotateX(radians(frameCount));
     //rotateY(radians(frameCount));
     //rotateZ(radians(frameCount));      
     // Try box for an alternative layout
     //box(row/2);
     sphere(row/2);
     popMatrix();
   }
 }
}
Page Index Toggle Pages: 1