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
Gravity 3d (Read 431 times)
Gravity 3d
Apr 10th, 2010, 1:35pm
 
I have a gravity program that i have made and just recently made it 3d but it is now when i start it slowly zooming in or out. Please respond. Here is the code.






PFont font;
int pl = int(random(2,20));
Planet[] planets = new Planet[pl];


void setup() {
 size(1000,1000, P3D);
 stroke(0);



 for (int i = 0; i< planets.length; i++ ) {
   float x = random(1,1000);
   float y = random(1,1000);
   float z = random(1,1000);
   int xy = int (x*1);
   int yx= int (y*1);
   int zz= int ((z*1)-500);
   int co = int(random(1,255));
   int col = int(random(1,255));
   int colo = int(random(1,255));
   float dimeter = random(6,45);
   float velx1 = random(-2.5,2.5);
   float vely1 = random(-2.5,2.5);
   float velz1 = random(-2.5,2.5);
   // float vely1 = -velx1;
   if (i == planets.length-1){
     xy = width/2;
     yx = height/2;
     zz = 0;
     dimeter = 125;
     velx1 = 0;
     vely1 = 0;
   }

   planets[i] = new Planet(dimeter,xy,yx,zz,co,col,colo,i,velx1,vely1,velz1);
 }

}


void draw() {


camera(width/2.0, height/2.0, (height/2) / tan(PI*60.0 / 360.0), width/2.0, height/2.0, 500, 0, 1, 0);
 lights();
 for (int i = 0; i < planets.length; i++ ) {

   planets[i].display(i);
   planets[i].update();
  // planets[i].checkEdges();




 }






}
class Planet {
 int t;
 float xcc = 0;
 float ycc = 0;
 float zcc = 0;
 float xpos;
 float ypos;
 float zpos;
 float diameter;
 int sunypos = 500;
 int sunxpos = 500;
 float gravmax = .1;
 int col1;
 int col2;
 int col3;
 int curplanet;
 int num;
 int i = 1;




 Planet(float diameter_,float xpos_,float ypos_,float zpos_,int co1,int co2,int co3,int planet,float velx,float vely,float velz) {
   diameter =  diameter_;
   xpos = (xpos_);
   ypos = (ypos_);
   zpos = (zpos_);
   col1 = co1;
   col2 =co2;
   col3 =co3;
   curplanet = planet;
   xcc = velx;
   ycc= vely;
   zcc = velz;
 }

 void update() {

   // float area = (((diameter/2)*(diameter/2))*3.1415);

   for (int i = 0;i < planets.length; i++) {
     if(i == curplanet){
       continue;
     }
     if(planets.length-1 ==curplanet){
       continue;
     }
     float area1 = ((((planets[i].diameter/2))*(planets[i].diameter/2)*(planets[i].diameter/2))*3.1415);
     float disty = (planets[i].ypos-ypos);
     float distx = (planets[i].xpos-xpos);
     float distz = (planets[i].zpos-zpos);
     float distfull = sqrt((distz*distz)+(distx*distx+disty*disty));
     if (distfull < (planets[i].diameter/2)){
       continue;
     }
     float acc = (.01) *(area1/(distfull*distfull));
     // print(" dist x="+distx);
     // print(" dist y="+disty);
     // print(" full="+distfull);
     // println(" acc="+acc);
     //.0000000000667*distx
     zcc = zcc +  (acc*distz/distfull);
     xcc = xcc +  (acc*distx/distfull);
     ycc = ycc +  (acc*disty/distfull);
   }
   zpos = zpos + zcc;
   xpos = xpos + xcc;
   ypos = ypos + ycc;




 }
/* void checkEdges() {

   if (xpos > width -(diameter/2)) {
     xcc = -xcc;
   }
   else if (xpos < diameter/2) {
     xcc = -xcc;
   }

   if (ypos > height - (diameter/2)) {
     ycc = -ycc;
   }
   else if (ypos < diameter/2 ) {
     ycc = -ycc;
   }


 }*/

 void display(int ti) {
   t = ti+1;


   stroke(0);
   // print(" ycc" +ycc);
   // println(" xcc" +xcc);
   // println(diameter+"diam");

   if(curplanet == i){
     fill(0,0,0,10);

        background(0);


   }


   if(curplanet != 0){
     fill(col1,col2,col3);
   }
   else{
     fill(255,255,255);
   }
   if(curplanet == planets.length -1){
     fill(255,255,0,233);



   }
   //ellipse(int(xpos),int(ypos),diameter,diameter);

   noStroke();
           pushMatrix();
   translate(xpos, ypos, zpos);

   sphere(diameter);

       popMatrix();



 }





}
Re: Gravity 3d
Reply #1 - Apr 10th, 2010, 5:32pm
 
never mind i don't need a solution any more it was just the sun moving
Page Index Toggle Pages: 1