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 & HelpPrograms › Spinning a Sphere
Page Index Toggle Pages: 1
Spinning a Sphere (Read 1413 times)
Spinning a Sphere
Mar 12th, 2010, 8:35am
 

Hallo guys,

I`ve taken a Processing course this semester and I was very excited about the effects that a Designer can achieve with some few lines of code! We made some assignments and everything was running nicely...until I`ve started my course project.

Now to the question. I want to create a Sphere made out of dots and rotate this sphere with different speeds and directions. I have the code to create the sphere, but I cant start spinning it in any way....

I would be very very thankful if any of you guys could give me some clues to start with, because the end of the term is comming...

Code:

void drawSphere()
{
 float posy = height*.25;
 float sizey = height/2;
 int nRings = 29;
 for(int i=1; i<nRings; i++)
 {
   float phase = i*1./nRings;
   float ringCenterPosX = width / 2;
   float ringCenterPosY = posy + (sin(phase*PI+HALF_PI)*.5+.5)*sizey;
   float ringSizeX = sin(phase*PI)*sizey/2;
   float ringSizeY = ringSizeX / 4;
 
   drawRing(ringCenterPosX, ringCenterPosY, ringSizeX, ringSizeY);
     
 }

}

void drawRing(float posx, float posy, float sizex, float sizey)
{
 noStroke();
 fill(1,.5);
 int n = 100;
 for(int i=0; i<n; i++)
 {
   float phase = i*1./n;
   float r = 2;
   ellipse(posx + sizex*cos(phase*TWO_PI), posy + sizey*sin(phase*TWO_PI), r,r);
 }
}

My final goal is to achieve an ingenious effect I saw on flicker,but I`m not allowed to post a link on the forum :( neither can I upload a photo of the sphere...
Re: Spinning a Sphere
Reply #1 - Mar 12th, 2010, 9:09am
 
post a link and change the url by adding spaces or writing "dot"
Re: Spinning a Sphere
Reply #2 - Mar 12th, 2010, 9:20am
 
Re: Spinning a Sphere
Reply #3 - Mar 12th, 2010, 10:15am
 
looking at your code i would say the problem is that it is not really 3d. its actually just a 2d sphere that looks 3d
you can use this formula to create a real 3d sphere out of points

x = cos(phi) * sin(theta);
y = cos(theta);
z = sin(phi) * sin(theta);

you can than simply use rotate
Re: Spinning a Sphere
Reply #4 - Mar 13th, 2010, 4:11am
 
Thanks, I`ll plunge into the Arc code in a second and try to implement it in a way?! ...

let me try to post the link to flickr:
flickr(dot)com/photos/alexposada/sets/72157622778283574/
flickr(dot)com/photos/alexposada/sets/72157623578136140/

So this is what I`m after. It looks astonishing and I`m quite sure it is not that hard to realize in Processing. I only have to come over this spinning problem to get this brilliant motion effect....

So back to work. Further questions to come...
Re: Spinning a Sphere
Reply #5 - Mar 13th, 2010, 4:28am
 
Hi Cedric,

yes you are totally right. my code just simulates 3d. I looked at your suggestion and try to use it, but I cant figure out how to find the (theta) angle ? for (phi) I would use my (phase)  that creates the iteration for building the rings ...

Oh God, my maths it just too poor Sad

x = cos(phi) * sin(theta);
y = cos(theta);
z = sin(phi) * sin(theta);

you can than simply use rotate [/quote]
Re: Spinning a Sphere
Reply #6 - Mar 13th, 2010, 4:50am
 
Code:
void setup()
{
 size(400,400);
 //colorMode(RGB,1);
 background(0);

 cX = width/2;
 cY = height/2;

 phi = 30.0;
 theta = 90.0;
 
 angle = 0.0;
 speed = 0.04;
 noLoop();
}


void draw()
{
float phase = angle*speed;

x = cos(phase)*sin(theta) + cX;
y = cos(theta) + cY;
z = sin(phase)*sin(theta);

angle+=10;

size(10, 10, P3D);
beginShape(POINTS);
vertex(x, y, z);
endShape();

}



no. cant get it working in any way.... and yep, I`m a novice.... but still trying and trying. If you would help me I`ll be very very grateful !

Tanks in advance
Re: Spinning a Sphere
Reply #7 - Mar 13th, 2010, 5:15am
 
i quickly put together a sketch showing what i was talking about. we placed the boxes n 3d space using the sin/cos and then can simply use rotate to rotate the sphere

Code:
void setup() {
size(600,400,P3D);
smooth();
background(255);

}

int radius = 100;
void draw() {
fill(200);
stroke(150);

translate(width/2,height/2);
rotateY(radians(mouseX));
rotateZ(radians(mouseY));
background(255);

for (int i = 1; i < 36; i++){
for (int j = 0; j < 36; j++){
pushMatrix();
float cx = cos(i) * sin(j)*radius ;
float cy = cos(j)*radius;
float cz = sin(i) * sin(j)*radius;
translate(cx,cy,cz);
box(3);
popMatrix();
}
}

}



Re: Spinning a Sphere
Reply #8 - Mar 13th, 2010, 6:19am
 
Thank you very much Cedrik!

I guess this should do the work. as soon as I have any results I will let you know Smiley

Right....

--------------------------

Is there a way to use a normal point and not a 3d box, because as soon as I increase the number of the elements performance decreases drastically?


may be something like:

Code:

pushMatrix();
float cx = cos(i) * sin(j)*radius ;
float cy = cos(j)*radius;
float cz = sin(i) * sin(j)*radius;
translate(cx,cy,cz);

stroke(0);
strokeWeight(5);
beginShape(POINTS);
vertex(cx, cy, cz);
endShape();


?

ThnU!

--------------------

it works. nice!!

now I have to bring in the color.
Re: Spinning a Sphere
Reply #9 - Mar 13th, 2010, 7:47am
 
sure, just replace :

translate(cx,cy,cz);
box(3);

with

point(cx,cy,cz);
Page Index Toggle Pages: 1