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 › my "planetary orbit" simulator
Page Index Toggle Pages: 1
my "planetary orbit" simulator (Read 1874 times)
my "planetary orbit" simulator
Dec 29th, 2005, 9:36pm
 
http://www.geocities.com/eriknelson2002/gravitybounce/index.html


for a planetary motion simulator (where planets bounce off the wall, just as they would in the real world if the wall were big enough)

hit "space" (with the simulation as the active window) to clear screen while keeping the simulation going.
Re: my "planetary orbit" simulator
Reply #1 - Jan 4th, 2006, 1:39pm
 
Very fun, I like the curves that you get as planets approach the central 'sun'. I'd like to play aronud with the code to see if I can make a more stable simulator around a sun... or two (or more) suns? Running at a slower speed, and without the  'bounce'.

Think theres any way to plot a predicted path/route?

I like the sub interaction between planets as they get close to each other.
Re: my "planetary orbit" simulator
Reply #2 - Jan 4th, 2006, 4:24pm
 
My code for gravity is quite different. I never got round to doing a multiple gravities applet but this thread inspired me to try.

The code I picked up a while back dealt with particles drifting to one point - the mouse. I cobbled an applet together and thought no more of it. (For the orbit I cheated by giving the particles centrifugal motion.)

Below I've reasoned that each planet probably percieves an average of the sum of gravities, and moves towards the gravitational average of the universe.
Code:

Orbiter [] planet;
void setup(){
size(400,350);
planet = new Orbiter[200];
for(int i = 0; i < planet.length; i++){
planet[i] = new Orbiter(random(width), random(height), random(2, 6));
}
ellipseMode(CENTER);
noStroke();
smooth();
}
void draw(){
background(0);
for(int i = 0; i < planet.length - 1; i++){
planet[i].grav(planet);
planet[i].draw();
planet[i].keepOnScreen();
}
}
class Orbiter{
float x, y, xVel, yVel, mass;
Orbiter(float x, float y, float mass){
this.x = x;
this.y = y;
this.mass = mass;
this.xVel = 0.0;
this.yVel = 0.0;
}
void grav(Orbiter [] other){
float distance = 0.0;
for(int i = 0; i < other.length; i++){
distance += dist(other[i].x, other[i].y, x, y);
}
distance /= other.length;
if (distance < 15){
distance = 15;
}
float grav = 0.0;
for(int i = 0; i < other.length; i++){
grav += (mass * other[i].mass) / sq(distance) * 5;
}
grav /= other.length;
float distX = 0.0;
for(int i = 0; i < other.length; i++){
distX += other[i].x - x;
}
float xGrav = 0.0;
for(int i = 0; i < other.length; i++){
xGrav += grav * (distX/distance);
}
xGrav /= other.length;
float xAccel = xGrav / mass;
xVel += xAccel;
x += xVel;
float distY = 0.0;
for(int i = 0; i < other.length; i++){
distY += other[i].y - y;
}
float yGrav = 0.0;
for(int i = 0; i < other.length; i++){
yGrav += grav * (distY/distance);
}
yGrav /= other.length;
float yAccel = yGrav / mass;
yVel += yAccel;
y += yVel;
}
void keepOnScreen(){
if(x - mass > width) x = 0 + mass;
if(y - mass > height) y = 0 + mass;
if(x + mass < 0) x = width - mass;
if(y + mass < 0) y = height - mass;
}
void draw(){
ellipse(x, y, mass*2, mass*2);
}
}

I tried sticking a central sun in but the particles didn't care. I don't think my code is correct. I think the planets should be reacting to each other in some way - rather than some imaginary average mass.
Page Index Toggle Pages: 1