Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package checkPoint;
- import java.util.ArrayList;//imports arraylist class
- import edu.princeton.cs.introcs.StdDraw;
- public class System {//helped by Andre Soto and TA Julia
- public double G = 6.67384e-11;//Universal Gravitational Constant
- //creates an array list for our objects to be put into
- ArrayList<Body> BODYLIST;
- //initializes array
- public void system(ArrayList<Body> bodyList) {//calculates acceleration for each body
- BODYLIST = bodyList;
- }
- public double[] computeAcceleration(Body planet) {
- double ax = 0;//x acceleration
- double ay = 0;//y acceleration
- double changeY = 0;//change in y
- double changeX = 0;//change in x
- double a; //acceleration
- double r = 0; //radius
- for (int i = 0; i < BODYLIST.size(); i++) {//for loop to give us an arraylist making our bodies in the system
- if (planet.equals(BODYLIST.get(i))) {
- continue;
- }
- changeX = BODYLIST.get(i).X - planet.X;//changes x
- changeY = BODYLIST.get(i).Y - planet.Y;//changes y
- r = Math.sqrt(changeX*changeX + changeY*changeY);//makes the radius
- a = G * BODYLIST.get(i).MASS / (r*r);//makes the acceleration
- ax += a * changeX / r;
- ay += a * changeY / r;
- }
- double[] acceleration = new double[2];//uses an array to make the acceleration
- acceleration[0] = ax;//x acceleration
- acceleration[1] = ay;//y acceleration
- return acceleration;//gives us acceleration
- }
- public void update(double timestep) {//updates things
- double[] as;
- for (int i = 0; i < BODYLIST.size(); i++) {
- as = computeAcceleration(BODYLIST.get(i)); //updates acceleration
- BODYLIST.get(i).updateVelocity(as[0], as[1], timestep);//updates velocity
- BODYLIST.get(i).updatePosition(timestep);//updates position
- }
- }
- public void draw(double cx, double cy, double pixelsPerMeter) {//drawsthe body
- StdDraw.clear(StdDraw.BLACK);
- for (int i = 0; i < BODYLIST.size(); i++) {
- BODYLIST.get(i).draw(cx, cy);
- }
- StdDraw.show(20);//updates every 20 milliseconds
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement