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.
IndexDiscussionExhibition › Starfield 3D
Page Index Toggle Pages: 1
Starfield 3D (Read 867 times)
Starfield 3D
May 5th, 2008, 7:38am
 
Hi folks,

I just got into "Processing" and I like it!


A simple 3D starfield

Code:

/// 3D Starfield

int numstars=400;
final int SPREAD=64;
int CX,CY;
final float SPEED=1.9;

Star[] s = new Star[numstars];

void setup(){
size(240,320,JAVA2D);
colorMode(RGB,255);
noStroke();
frameRate(30);
CX=width/2 ; CY=height/2;
/// s = new Star[numstars];
for(int i=0;i<numstars;i++){
s[i]=new Star();
s[i].SetPosition();
}
}

void draw(){
background(0,0,0);
for(int i=0;i<numstars;i++){
s[i].DrawStar();
}
}

class Star {
float x=0,y=0,z=0,sx=0,sy=0;
void SetPosition(){
z=(float) random(200,255);
x=(float) random(-1000,1000);
y=(float) random(-1000,1000);
}
void DrawStar(){
if (z<SPEED){
this.SetPosition();
}
z-=SPEED;
sx=(x*SPREAD)/(z)+CX;
sy=(y*SPREAD)/(4+z)+CY;
if (sx<0 | sx>width){
this.SetPosition();
}
if (sy<0 | sy>height){
this.SetPosition();
}
fill(color(255 - (int) z,255 - (int) z,255 - (int) z));
rect( (int) sx,(int) sy,2,3);
}
}
Page Index Toggle Pages: 1