我写了一个用来模拟引力场的类,以及一个使用它的主类。我确信我的公式是正确的,但是当代码编译时,当我运行它时,它返回'NaN‘。这是我第一次使用多个类,所以我想知道我是否正确地链接了它们:这是我的GravField类:
import java.lang.Math;
public class GravField{
public static final double G = 6.674*Math.pow(10,-11); //defines the constant G
private double planetMass;
private double planetRadius;
private double projectileX;
private double projectileY;
private double a = planetRadius + projectileY;
PhysicsVector projectilePosition = new PhysicsVector(projectileX, a); //PhysicsVector class was written
PhysicsVector gravityAcceleration = new PhysicsVector(); //lecturer, so that won't be the problem!
public GravField(double planetMass, double planetRadius, double projectileX, double projectileY){
double distance = Math.sqrt(projectileX*projectileX+projectileY*projectileY);
double x = (-G*planetMass*projectileX)/(distance*distance*distance);
double y = (-G*planetMass*a)/(distance*distance*distance);
gravityAcceleration.setVector(x, y); //setVector is a method in the PhysicsVector class
gravityAcceleration.print(); //print is also in PhysicsVector
}
public double magnitude(){
double magnitudeOfGravField = gravityAcceleration.magnitude();
return magnitudeOfGravField;
}
public double componentX(){
double xComponent = gravityAcceleration.getX(); //gravityAcceleration for some reason can't be
return xComponent; //directly accessed by main class, so use this instead
}
public double componentY(){
double yComponent = gravityAcceleration.getY();
return yComponent;
}
}然后在我的主类中:
import java.util.Scanner;
import java.lang.Math;
public class ParticleSim{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the size of the time step:");
double timeStep = scanner.nextDouble();
System.out.println("Please enter the initial x velocity:");
double initialXVelocity = scanner.nextDouble();
System.out.println("Please enter the initial y velocity:");
double initialYVelocity = scanner.nextDouble();
PhysicsVector projectilePosition = new PhysicsVector();
double earthMass = 5.972*Math.pow(10,24);
double earthRadius = 6371000;
GravField earth = new GravField(earthMass, earthRadius, 0, 0);
double y=0.0;
double x=0.0;
double yVelocity=initialYVelocity;
double xVelocity=initialXVelocity;
do{ //either this loop or my GravField class is the problem, I think!
System.out.println("Hi! I'm in the loop!");
y += yVelocity * timeStep; //Euler's algorithm to calculate positions and velocities
x += xVelocity*timeStep;
yVelocity += earth.componentY() * timeStep;
xVelocity += earth.componentX() * timeStep;
PhysicsVector velocity = new PhysicsVector();
PhysicsVector position = new PhysicsVector();
velocity.setVector(xVelocity, yVelocity);
position.setVector(x, y);
velocity.print();
position.print();
timeStep += timeStep;
}while(y>0);
}
}每个GravField对象都会有一个变量gravityAcceleration,对吗?通过编写earth.componentY(),我请求地球上的gravityAcceleration的Y分量?如果你能费力读完我的代码,我真的很感激!谢谢你的帮助!
发布于 2015-11-18 06:42:16
您确实通过调用earth.componentx()调用了gravityAcceleration的x,但我认为这个缺陷使您的逻辑变得混乱
double distance = Math.sqrt(projectileX*projectileX+projectileY*projectileY);
double x = (-G*planetMass*projectileX)/(distance*distance*distance);
double y = (-G*planetMass*a)/(distance*distance*distance);在GravField的构造函数内部。这只会在你创建地球时被调用一次。所以地球从0x和0y的gravityAcceleration开始,这永远不会改变。
你需要移动东西,这样它才能在循环中发生,并且你可以重新计算距离和射弹的每一步的向量。
发布于 2015-11-18 08:00:50
不,GravField本身不会有一个变量gravityAccerlation,因为它是在PhysicsVector内部定义的,但是,您可以通过使用,例如,projectilePosition.gravityAcceleration来访问它;
当您调用earth.componentY()时,您正在访问PhysicsVector的Y值,并将其赋值给GravField的Y组件,因此,您实际上是在获取地球的Y组件。
在ParticleSim类中,由于(y>0)条件,您永远不能开始循环,因为您已经将y定义为等于0。
https://stackoverflow.com/questions/33768062
复制相似问题