Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- public class Planet {
- double myXPos;
- double myYPos;
- double myXVel;
- double myYVel;
- double myMass;
- String myFileName;
- double gConst;
- public Planet(double xp, double yp, double xv, double yv, double mass, String filename) {
- this.myXPos = xp;
- this.myYPos = yp;
- this.myXVel = xv;
- this.myYVel = yv;
- this.myMass = mass;
- this.myFileName = filename;
- this.gConst = 6.67 * Math.pow(10, -11);
- }
- public Planet(Planet p) {
- this.myXPos = p.myXPos;
- this.myYPos = p.myYPos;
- this.myXVel = p.myXVel;
- this.myYVel = p.myYVel;
- this.myMass = p.myMass;
- this.myFileName = p.myFileName;
- }
- public double calcDistance(Planet otherPlanet) {
- double xres = Math.pow(otherPlanet.myXPos - this.myXPos, 2);
- double yres = Math.pow(otherPlanet.myYPos - this.myYPos, 2);
- return Math.sqrt(xres + yres);
- }
- public double calcForceExertedBy(Planet otherPlanet) {
- double force = (gConst * otherPlanet.myMass * this.myMass) / Math.pow(calcDistance(otherPlanet), 2);
- return force;
- }
- public double calcForceExertedByX(Planet otherPlanet) {
- double dx = otherPlanet.myXPos - this.myXPos;
- double dy = otherPlanet.myYPos - this.myYPos;
- double radius = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
- double force = calcForceExertedBy(otherPlanet);
- return (force * dx) / radius;
- }
- public double calcForceExertedByY(Planet otherPlanet) {
- double dx = otherPlanet.myXPos - this.myXPos;
- double dy = otherPlanet.myYPos - this.myYPos;
- double radius = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
- double force = calcForceExertedBy(otherPlanet);
- return (force * dy) / radius;
- }
- public double calcNetForceExertedByX(Planet[] planets) {
- double total = 0.0;
- for (Planet planet : planets) {
- if (!this.equals(planet)) {
- total += calcForceExertedByX(planet);
- }
- }
- return total;
- }
- public double calcNetForceExertedByY(Planet[] planets) {
- double total = 0.0;
- for (Planet planet : planets) {
- if (!this.equals(planet)) {
- total += calcForceExertedByY(planet);
- }
- }
- return total;
- }
- public void update(double seconds, double xforce, double yforce) {
- double accelerationx = xforce / myMass;
- double accelerationy = yforce / myMass;
- this.myXVel = myXVel + seconds * accelerationx;
- this.myYVel = myYVel + seconds * accelerationy;
- this.myXPos = myXPos + seconds * myXVel;
- this.myYPos = myYPos + seconds * myYVel;
- }
- public void draw() {
- StdDraw.picture(myXPos, myYPos, "images/" + myFileName);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement