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 › adding spheres to array
Page Index Toggle Pages: 1
adding spheres to array (Read 553 times)
adding spheres to array
Dec 19th, 2007, 10:38pm
 
Hi, totally new to Processing...
I need to create a few spheres and manipulate them. So normally (actionscript) Smiley I would create shapes and push them in an array. How do I do that in Processing? It looks like sphere() does not return anything... thanks.
Re: adding spheres to array
Reply #1 - Dec 20th, 2007, 4:06am
 
hi

sphere() draws a sphere directly to the screen.
http://processing.org/learning/3d/primitives3d.html
Re: adding spheres to array
Reply #2 - Dec 20th, 2007, 5:23am
 
that is correct. so it does not answer my question how i create a few of them and then let's say move them around the screen.
Re: adding spheres to array
Reply #3 - Dec 20th, 2007, 5:52am
 
Can you store an array of positions and then use a for loop to draw the spheres using those arrays?
Re: adding spheres to array
Reply #4 - Dec 20th, 2007, 6:17am
 
so, i would continuously re-draw the spheres and manipulate the positions?
Re: adding spheres to array
Reply #5 - Dec 20th, 2007, 7:35am
 
If you are using a continuous sketch the spheres are drawing every frame anyway. Storing the xyz of a translate will let you move the spheres around.
Re: adding spheres to array
Reply #6 - Dec 20th, 2007, 9:12am
 
Quote:
so, i would continuously re-draw the spheres and manipulate the positions?

exactly.

Actually, you want an array of spheres, but Processing doesn't have built-in sphere objects : only a graphic sphere() method which draws a sphere at a certain position, that's all.

Start with a custom Sphere class describing what a sphere is, from your point of view, and then simply put them in a array :

Code:
class Sphere {

// position, radius and color
float x, y, z;
float r;
color c;

// simple constructor
public Sphere(float x, float y, float z, float r, color c) {
this.x = x;
this.y = y;
this.z = z;
this.r = r;
this.c = c;
}

// display the sphere on the screen
void display() {
fill(c);
pushMatrix();
translate(x, y, z);
sphere(r);
popMatrix();
}

}


Code:
Sphere[] arrayOfSpheres;

void setup() {
// set up the array
arrayOfSpheres = new Sphere[10];
arrayOfSpheres[0] = new Sphere(0, 0, 0, 5, color(100));
// ...
arrayOfSpheres[9] = new Sphere(10, 0, 0, 7, color(50));
}

void draw() {
// clear the screen
background(255);
// display the spheres
for (int i = 0; i < arrayOfSpheres.length; i++) {
arrayOfSpheres[i].display();
}
}
Re: adding spheres to array
Reply #7 - Dec 20th, 2007, 3:53pm
 
thanks antiplastik!!!
that's the solution I have been looking for.. just did not know how to approach it yet in Processing.
Re: adding spheres to array
Reply #8 - Dec 20th, 2007, 7:52pm
 
one more question -
when I draw spheres, here's the display method from the Sphere class -
void display() {
   
   fill(c);
   smooth();
   noStroke();
   pushMatrix();
  translate(x, y, z);
  sphere(r);
  popMatrix();

 }

the spheres turn out to be segmented. like I can see faces that comprise a sphere. is there a way to make the sphere surface continues?
Re: adding spheres to array
Reply #9 - Dec 20th, 2007, 8:32pm
 
http://processing.org/reference/sphereDetail_.html
Re: adding spheres to array
Reply #10 - Dec 20th, 2007, 9:24pm
 
sorry, I probably was not clear enough.. when I use smooth() and noStroke()  I can see the faces being separated; in other words - where the stroke would go, I can see a gap.  
If I do not use smooth() the gaps disappear but then the edges are extremely aliased. So using smooth () is the only option but then how do I get rid of those gaps?
Page Index Toggle Pages: 1