class SpaceObject { //--------------- float x, y, z; // position float vx, vy, vz;// hastighet float fx, fy, fz;// kraft Trail trail; float mass; color clr; SpaceObject next; //--------------- SpaceObject(float x, float y, float z, float vx, float vy, float vz, float mass, color clr){ this.x = x; this.y = y; this.z = z; this.vx = vx; this.vy = vy; this.vz = vz; this.mass = mass; this.clr = clr; trail = new Trail(x,y,z, 100, clr ); } //--------------- void add(SpaceObject obj){ SpaceObject spob = next; next = obj; obj.next = spob; } //--------------- void update(){ vx += fx / mass; vy += fy / mass; vz += fz / mass; fx = fy = fz = 0.0; if( abs(x)>5000 || abs(y)>5000 || abs(z)>5000 ) vx = vy = vz = 0f; x += vx; y += vy; z += vz; trail.update(x,y,z); } //--------------- // ritar ut pelleten. större massa leder till vitare pellet void visualize(){ trail.visualize(); stroke( clr ); point(x, y, z); } //--------------- // summerar graviationsbidraget från alla andra pelletsar // sparar resultaten i variablerna fx, fy och fz void computeForces(){ float r, F; for( SpaceObject obj = objs ; obj!=null ; obj = obj.next ){ // inget gravitationsbidrag från sig själv if( obj != this ){ r = dist(x,y,z, obj.x,obj.y,obj.z); F = G*obj.mass*mass / pow(r,3); fx += F*(obj.x-x); fy += F*(obj.y-y); fz += F*(obj.z-z); } } } //--------------- } class Star { //--------------- float x, y, z; // position color clr; //--------------- Star(float radius, color clr){ this.clr = clr; sphereFormation(radius); } //------------------ void cubeFormation(){ x = random(-1,1); y = random(-1,1); z = random(-1,1); } //------------------ void sphereFormation(float radius){ cubeFormation(); if( dist(0,0,0,x,y,z) > 1 ) cubeFormation(); if( dist(0,0,0,x,y,z) > 1 ) cubeFormation(); if( dist(0,0,0,x,y,z) > 1 ) cubeFormation(); if( dist(0,0,0,x,y,z) > 1 ) cubeFormation(); if( dist(0,0,0,x,y,z) > 1 ) cubeFormation(); float f = radius/dist(0,0,0,x,y,z); x *= f; y *= f; z *= f; } //--------------- void visualize(){ stroke(clr); point(x, y, z); } //--------------- }