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 › A 'sphere' of 3D spheres
Pages: 1 2 
A 'sphere' of 3D spheres? (Read 1360 times)
A 'sphere' of 3D spheres?
Mar 4th, 2008, 12:28pm
 
Hey, I'm trying to create something I have visualised in my head, but am fairly new to programming, especially with 3D work.

What i'm trying to achieve are a number of spheres positioned in a 'sphere' configuration in 3D, if you see what I mean. Imagine the nucleus of an atom, but with space between the spheres.

I can do a pseudo-2D version (although I think i'm doing it the long way), which might help to show what I'd like...

Code:
float x, y, z;
float angle;
float radius = 180.0;
float frequency = 45;

void setup() {
size(400, 400);
background(0);
smooth();
noStroke();
}

void draw() {
background(0);
for (int i=0; i<360; i+=45) {
x = width/2 + cos(radians(angle))*(radius/2);
y = height/2 + sin(radians(angle))*(radius/2);
rectMode(CENTER);
fill(255);
ellipse (x, y, 10, 10);
angle -= frequency;
}

for (int i=0; i<360; i+=45) {
x = width/2 + cos(radians(angle))*(radius);
y = height/2 + sin(radians(angle))*(radius);
rectMode(CENTER);
fill(255);
ellipse (x, y, 20, 20);
angle -= frequency;
}
}


However, I'm not sure how to go to 3D and get the desired result - the steps needed? Obviously I need to add a Z dimension! Any ideas of what I need to do?
Re: A 'sphere' of 3D spheres?
Reply #1 - Mar 4th, 2008, 1:03pm
 
Whilst a circle is fairly easy, it gets quite a bit more complicated on a sphere if you want an even spacing of small-spheres.
See: http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere
or
http://www.ogre.nu/sphere.htm
for a couple of methods/discussion of the problem.
Re: A 'sphere' of 3D spheres?
Reply #2 - Mar 4th, 2008, 4:05pm
 
Hey thanks for the links - interesting reading.

I don't know if you've heard of a program called Mathematica, initiated by that chap Wolfram (who wrote 'A New Kind Of Science'). Any, it has similarities to Processing, and there is an example program with something similar to what i'd like to do here: http://demonstrations.wolfram.com/ClusterOf92Spheres/

Although, this screenshot is probably more relevant: http://demonstrations.wolfram.com/ClusterOf92Spheres/HTMLImages/index.en/popup_3.gif

I've look at the source code for this, but it's in a completely different language, and anyway it just seems like it has an in-built shape ('icosahedron') which it plots the spheres on (some on faces, some on vertices) etc.
Re: A 'sphere' of 3D spheres?
Reply #3 - Mar 4th, 2008, 4:25pm
 
The surfaceLib which allows you to create spheres has the methode: coords()(http://www.eskimoblood.de/surfacelib/surface_method_coords.htm) which gives you the coordinates of your sphere surface.

Code:

Sphere s =new Sphere(this, 10, 10); // gives you a sphere with 10*10 points
float [][][] corrds =s.coords(); //get the coords array
for(int i=0; i<coords.length; i++){
for(int j=0; j<coords[i].length; j++){
float[] xyz = <coords[i][j]; //an array with the x,y and z coordinate
}
}


Re: A 'sphere' of 3D spheres?
Reply #4 - Mar 7th, 2008, 8:40pm
 
Thanks, that looks like it could be an answer! I'll check it out...
Re: A 'sphere' of 3D spheres?
Reply #5 - Mar 9th, 2008, 2:20pm
 
I've had a play with this (great library by the way)...

Code:

import surface.calculation.*;
import surface.*;

Sphere s;

void setup(){
size(500,300,P3D);

s=new Sphere(g, 10, 10);
float [][][] coords =s.coords(); //get the coords array
print(coords);
for(int i=0; i<coords.length; i++){
for(int j=0; j<coords[i].length; j++){
float[] xyz = coords[i][j]; //an array with the x,y and z coordinate
//print(xyz);
print(coords[i][j]);
}
}
}

void draw(){
background(#FAFAFA);
lights();

translate(width/2,height/2);
rotateX(radians(frameCount/PI));
rotateY(radians(frameCount/PI));
rotateZ(radians(frameCount/PI));
s.setScale(150);

noStroke();
fill(#FF6724);

s.draw();
}


And the array only seems to spit out (i'm printing it):
Code:
[F@193a66f
[F@93d6bc
[F@be0e27...
...etc.


Although it is 100 (as you would expect as the sphere is 10*10)?
Re: A 'sphere' of 3D spheres?
Reply #6 - Mar 9th, 2008, 7:23pm
 
Every array holds the coordinates for the 3d point, so you need to translate with this point and draw your sphere:

Code:

void draw(){
float [][][] coords =s.coords(); //get the coords array
for(int i=0; i<coords.length; i++){
for(int j=0; j<coords[i].length; j++){
float[] xyz = coords[i][j]; //an array with the x,y and
pushMatrix();
translate(xyz[0], xyz[1], xyz[2]);
drawYourSpheresHere
popMatrix();
}
}
}
Re: A 'sphere' of 3D spheres?
Reply #7 - Mar 9th, 2008, 7:41pm
 
Oh I see, thanks...! So it's like a hierachy of arrays?

Arrays and texturing 3D surfaces are definately my weaknesses in Processing at the moment.
Re: A 'sphere' of 3D spheres?
Reply #8 - Mar 9th, 2008, 9:31pm
 
Yes, the coordinates are stored in a three dimensional array.
Re: A 'sphere' of 3D spheres?
Reply #9 - Mar 17th, 2008, 5:33pm
 
Hey, still having problems with this.

Drawing a sphere results in it being displayed in the centre.

Using the following...
Code:

println("X equals " + xyz[0]);
println("Y equals " + xyz[1]);
println("Z equals " + xyz[2]);


Shows that it's spitting out really odd values...
Code:

X equals 0.0
Y equals -0.0
Z equals 1.0
X equals 0.05939133
Y equals -0.33682406
Z equals 0.9396926
X equals 0.11161919
Y equals -0.6330222
Z equals 0.76604444
X equals 0.15038413
Y equals -0.8528685
Z equals 0.49999997
X equals 0.17101052
Y equals -0.96984625
Z equals 0.1736481
X equals 0.17101051
Y equals -0.9698462
Z equals -0.1736483
X equals 0.15038411
Y equals -0.85286844
Z equals -0.50000006
X equals 0.11161916
Y equals -0.633022
Z equals -0.7660446
X equals 0.059391305
Y equals -0.3368239
Z equals -0.9396927
X equals -1.5180845E-8
Y equals 8.609462E-8
Z equals -1.0
etc.....


If they were between 1.0 and -1.0 I could probably see why, but odd values like 8.609462E-8 and -1.5180845E-8 get thrown up (that even include letters). Any clues?
Re: A 'sphere' of 3D spheres?
Reply #10 - Mar 17th, 2008, 5:40pm
 
8.609462E-8 means 0.00000008609462
"E-8" is notation for "times ten to the power -8" and it just shorthand for expressing really small (or big) numbers without having lots of zeros flying around.
Re: A 'sphere' of 3D spheres?
Reply #11 - Mar 17th, 2008, 8:00pm
 
Ah right I see...

Isn't it odd though that they are really small numbers?
Re: A 'sphere' of 3D spheres?
Reply #12 - Mar 17th, 2008, 8:59pm
 
No it isn't, cause these are values for an sphere with a radius of 1. So all you have to do is to multiply this values with the radius of your choice.
Re: A 'sphere' of 3D spheres?
Reply #13 - Mar 18th, 2008, 2:29pm
 
Ah OK - I used s.setScale(150); before that part so I assumed it would change the values also.
Re: A 'sphere' of 3D spheres?
Reply #14 - Mar 20th, 2008, 8:59pm
 
I've got this working, and it looks quite cool - lots of potential here methinks...

...

However it does run quite slow - but this is understandable because it is calculating the coordinates and then creating an array with all those values every time around the draw() loop!
Pages: 1 2