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 › Gravity program
Page Index Toggle Pages: 1
Gravity program (Read 1282 times)
Gravity program
Feb 26th, 2010, 5:52pm
 
this is my first program and it creates planets which all have gravity according to mass and orbit each other it was made by me
i don't know how to give the objects more gravity when they get closer to each other(right now it is when they are farther).


Planet[] planets = new Planet[6];
void setup() {
 size(1000,1000);
 stroke(0);
 
 
 for (int i = 0; i< planets.length; i++ ) {
    float x = random(1,1000);
    float y = random(1,1000);
    int xy = int (x*1);
    int yx= int (y*1);
    float dimeter = random(1,50);
    if (i == planets.length-1){
      xy = width/2;
      yx = height/2;
      dimeter = 250;
    }
   
    planets[i] = new Planet(dimeter,xy,yx);
 }
 
}


void draw() {
 
   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;
 int xpos;
 int ypos;
 float diameter;
 int sunypos = 500;
 int sunxpos = 500;
 

 
 
 
 
 Planet(float diameter_,int xpos_,int ypos_) {
   diameter =  diameter_;
   xpos = xpos_;
   ypos = ypos_;
 }
 
 void update() {
   
   float area = (((diameter/2)*(diameter/2))*3.1415);
   
   for (int i = 0;i < planets.length; i++) {
     float area1 = ((((planets[i].diameter/2))*(planets[i].diameter/2))*3.1415);
     xcc = xcc + ((((planets[i].xpos-xpos)*(area1/area))/(100000)));
     ycc = ycc + ((((planets[i].ypos-ypos)*(area1/area))/(100000)));
   }
   xcc = xcc;
   ycc = ycc;
   xpos = xpos + int (xcc);
   ypos = ypos + int (ycc);
   println(area+"are");
   
 }
 void checkEdges() {

   if (xpos > width + diameter) {
     xcc = -xcc;
   } else if (xpos < 0 + diameter) {
     xcc = -xcc;
   }
   
   if (ypos > height + diameter) {
     ycc = -ycc;
   } else if (ypos < 0 ) {
     ycc = -ycc;
   }
   

 }
 
 void display(int ti) {
   t = ti+1;
   
   stroke(0);
   print(ycc+"ycc");
   println(diameter+"diam");
   
   

   
   
   
   ellipse(xpos,ypos,diameter,diameter);

   
   
   
 }

}

Re: Gravity program
Reply #1 - Feb 26th, 2010, 9:36pm
 
Hi, if gravity is a function of distance / dist() between two objects, and you want it to increase in inverse proportion to the distance, you can cross products, inverting the value, or use the map() function to achieve this.  The basic idea is that [maximum distance] - [actual distance] is to be mapped to [0 to N gravity].  map() works this way if you put the maximum first, rather than the minimum, in the first range-pair.  g = map(maxDist,actualDist,0,maxG);
Re: Gravity program
Reply #2 - Feb 27th, 2010, 10:14am
 
Thank you for that
Re: Gravity program
Reply #3 - Feb 27th, 2010, 2:13pm
 
Benhem, mapping would give a linear gravity function.....which would be interesting (maybe more interesting) but not how real planets behave.

Ahem... may I present my own humble program with orbiting objects:
http://www.openprocessing.org/visuals/?visualID=6407 The code is here, feel free to use it.

I also did this which would show you how to make every planet affect every other planet... http://www.openprocessing.org/visuals/?visualID=6456
Re: Gravity program
Reply #4 - Feb 27th, 2010, 2:30pm
 
These ones are quite cool, though - the inverse gravity. Like planets attached to other planets with rubber bands.....Could give some really interesting behaviour when they are all affecting each other.
Re: Gravity program
Reply #5 - Feb 27th, 2010, 8:46pm
 
Giles, I meant that would give the inverse distance, mapped to another range, for use in a formula -- but you're quite right, the formula for gravitational force uses distance, raw:

F=G([m1*m2]/D^2)

(whoops)
Re: Gravity program
Reply #6 - Feb 28th, 2010, 1:04pm
 
isnt G((m1*m2)/(D^2))
the force between both not the acceleration of one? what would be?
Re: Gravity program
Reply #7 - Feb 28th, 2010, 5:10pm
 
Yes, that's the force for both objects - as a = f/m, the two accelerations are different. Divide the acceleration of each object by its own mass to get acceleration.
Page Index Toggle Pages: 1