Java代码出乎意料地表现出来

时间:2014-04-03 23:01:33

标签: java swing

我正在编写一个java太阳系类型模拟器,我的物理学出了点问题,我无法弄清楚原因。我确定它在某处是一个简单的单线错误。发生了什么,一个身体会移动,看起来它会开始绕另一个身体运行,然后身体将无缘无故地飞向太空。这是GitHub上的项目:https://github.com/Ronin11/FunWithGit/tree/dev

这是物理学:

import java.util.ArrayList;


public class physics {
private final static double G = 6.6738489E-11; //Newton's "Big" G
private final static double Multiplier = 10E9; //Gravity Equation Multiplier

private static double calculateGravity(Body m1, Body m2, boolean returnX){
    /***** Newton's universal law of gravitation with
            the distance formula substituted for r^2 ****/
    double force = ((G*m1.getMass()*m2.getMass())/
                     (StrictMath.pow((m2.getposx()-m1.getposx()),2) 
                       + StrictMath.pow((m2.getposy() - m1.getposy()), 2)));
    //System.out.println(force);
    double angle = StrictMath.atan((m2.getposy()-m1.getposy())/(m2.getposx()-m1.getposx()));
    //System.out.println(angle*180);
    if(returnX)
        force = force * StrictMath.cos(angle);
    else
        force = force * StrictMath.sin(angle);
    return force;
}

private static double calculateAcceleration(double Force, Body m){
    /** Force = Mass * Acceleration **/
    double Acc = Force/m.getMass();
    return Acc;
}

/** Change the velocity with the force calculations**/
private static void velChanges(Body b,ArrayList <Body> system){
    double xAccel = 0;
    double yAccel = 0;
    for (Body body: system){
        if(b != body){
            xAccel += calculateAcceleration(calculateGravity(b,body,true),b);
            yAccel += calculateAcceleration(calculateGravity(b,body,false),b);
        }
    }
    xAccel = Multiplier * xAccel; // Static Multipliers to
    yAccel = Multiplier * yAccel; //  make things play nicer
    b.setvelx(b.getvelx()+xAccel);
    b.setvely(b.getvely()+yAccel);
}

/** Change the position to the new position **/
private static void posChanges(Body b){
    b.setposx(b.getposx()+b.getvelx());
    b.setposy(b.getposy()+b.getvely());
}

/** Nice little method for GUI.java to call **/
public static void calculateChanges(ArrayList <Body> system){
    for (Body b: system) {
    velChanges(b,system);
    posChanges(b);
    }
}

}

这是Body类:

import java.awt.Color;
/**
 * 
 * @author Ronin
 * 
 *  The body class is a simple object used in the Gravity program.
 *  All bodies will have the six variables associated with them to
 *  run the simulation. Eventually the color variable will be 
 *  determined by the density of the body.
 *
 */

public class Body {
public Body(){
    mass = 100;
    size = 1;
    velX = 0;
    velY = 0;
    posX = 0;
    posY = 0;
    color = Color.white;
}

public Body(int mass, int size, Color color, double velX, double velY, double posX, double posY){
    this.mass = mass;
    this.size = size;
    this.color = color;
    this.velX = velX;
    this.velY = velY;
    this.posX = posX;
    this.posY = posY;

}
//Getters
public int getMass(){return mass;}
public int getSize(){return size;}
public double getvelx(){return velX;}
public double getvely(){return velY;}
public double getposx(){return posX+velX;}
public double getposy(){return posY+velY;}
public Color getColor(){return color;}
//Setters
public void setMass(int m){mass = m;}
public void setSize(int s){size = s;}
public void setvelx(double v){velX = v;}
public void setvely(double v){velY = v;}
public void setposx(double p){posX = p;}
public void setposy(double p){posY = p;}
public void setColor(Color c){color = c;}

//Member Variables
private Color color;
private int mass;
private int size;
private double velX;
private double velY;
private double posX;
private double posY;

}

谢谢!

0 个答案:

没有答案
相关问题