Java模拟弹丸运动以找到粒子行进的水平距离 -

时间:2017-12-03 20:06:25

标签: java simulation physics motion

对任何格式问题道歉 - 这是我在这里的第一个问题。

我有一个java模拟,需要调用多个类才能模拟被抛出的球的运动。我的代码将运行并编译,但给出错误的答案。

我应该在代码中输入的值是: 1E-2 1000。 100。

应该产生20437(然后是一些小数)。事情是,我得到10.0。我几乎可以肯定我的问题是粒子类没有更新,但我不完全确定它为什么不是 - 所以任何和所有的帮助都会深深感激 - 我是新编码(它一直是关于我开始工作2个月后,所以我为代码的风格和缺乏基本的Java知识而道歉。

我的代码如下: Project.java(主要方法):

import java.util.Scanner;
import java.lang.Math;

public class Project {
    /**
     * Main Method - defines the data regarding Earth (mass and Radius) as well
     * as mass of the particle (which I have taken to be 1).
     * It also introduces a scanner to input the time step, and initial
     * velocities in the x and y directions.
     *
     * @param earthMass    the mass of the Earth
     * @param earthRadius  the radius of the Earth
     * @param particleMass the mass of the particle being moved as a projectile
     * @param deltaT       the time step - as selected by the user
     * @param vx0          the initial velocity in the x direction - as selected by the
     *                     user
     * @param vy0          the initial velocity in the y direction - as selected by the
     *                     user
     *                     The main method also initialises various PhysicsVectors and calls the
     *                     Euler Method from the Particle class.
     **/

    public static void main(String[] argv) {
        Scanner sc = new Scanner(System.in);
        double earthMass = 5.974e24;
        double earthRadius = 6380e3;
        double particleMass = 1.0; //Mass of moving particle, 

        PhysicsVector initialProjectilePosition = new PhysicsVector(0.0, 0.0);
        double deltaT = sc.nextDouble(); //time step
        double vx0 = sc.nextDouble(); //Initial velocity in x
        double vy0 = sc.nextDouble(); //Initial velocity in y

        PhysicsVector initialVelocity = new PhysicsVector(vx0, vy0);

        PhysicsVector projectilePosition = new
                PhysicsVector(initialProjectilePosition);

        PhysicsVector planetPosition = new PhysicsVector(0.0, -earthRadius); //Surface defined by (0, 0) - y-coordinate being defined as centre of planet.

        GravField earth = new GravField(earthMass, planetPosition); //Earth's 
        Gravitational Field

        Particle projectile = new Particle(1.0, projectilePosition,
                initialVelocity);
        Particle finalProjectile;

        do {
            projectile.eulerMethod(earth, deltaT);

        } while (projectilePosition.getY() > 0);
        sc.close();
    }

}

GravField.java:

import java.lang.Math;

public class GravField {

    private static final double G = 6.67408 * Math.pow(10, -11);
    private double planetMass = 5.974E24;
    private double planetRadius = 6830E3;
    private PhysicsVector planetPosition;

    /**
     * Constructor
     *
     * @param planetMass
     * @param planetPosition
     */
    public GravField(double planetMass, PhysicsVector planetPosition) {
        this.planetMass = planetMass;
        this.planetPosition = planetPosition;

    }

    /**
     * Method
     *
     * @param projectilePosition
     * @return gravity
     */
    public PhysicsVector gravityAcceleration(PhysicsVector projectilePosition) {
        PhysicsVector distanceVector = PhysicsVector.subtract(this.planetPosition, projectilePosition);
        PhysicsVector gravity = distanceVector.getUnitVector();

        double distance = distanceVector.magnitude(); //distance between particle being projected and the Earth's surface
        // PhysicsVector gAcceleration;
        // gAcceleration = ((-G * this.planetMass * distanceVector.getX())/(distance * distance * distance), (-G * this.planetMass * distanceVector.getY())/(distance * distance * distance), 0);
        double gMagnitude;
        gMagnitude = (-G * this.planetMass) / (distance * distance);
        gravity.scale(gMagnitude);
        return gravity; //returned as a PhysicsVector
    }
}

和Particle.java:

public class Particle {

    double mass; // mass of particle
    PhysicsVector Position = new PhysicsVector(); // start position vector
    PhysicsVector Velocity = new PhysicsVector(); // start velocity
    double deltaT; // size of time step

    /**
     * Constructor - initialises a particle with the specified position, velocity and mass.
     *
     * @param mass            the mass of the particle
     * @param initialPosition coordinates of the initial position in Cartesian coordinates
     * @param initialVelocity coordinates of the initial velocity in Cartesian coordinates
     **/
    public Particle(double mass, PhysicsVector initialPosition,
                    PhysicsVector initialVelocity) {
        this.mass = mass;
        this.Position = new PhysicsVector(initialPosition);
        this.Velocity = new PhysicsVector(initialVelocity);
    }


    public PhysicsVector getFinalV(GravField earth, double deltaT) {
        PhysicsVector at = PhysicsVector.scale(deltaT, earth.gravityAcceleration(new PhysicsVector(Velocity)));
        PhysicsVector v = PhysicsVector.add(at, Velocity);
        v = Velocity;
        return Velocity;
    }

    public PhysicsVector getFinalPos(PhysicsVector z, double t) {
        PhysicsVector vt = PhysicsVector.scale(deltaT, Velocity);
        PhysicsVector p = PhysicsVector.add(Position, vt);
        p = Position;
        return Position;
    }

    /**
     * Constructor - initialises the gravitational field due to the Earth, and prints out the x-coordinate of the position vector.
     *
     * @param gAccleration the acceleration due to gravity on Earth - calls GravField class.
     **/
    public void eulerMethod(GravField earth, double deltaT) {
        PhysicsVector gAcceleration = new PhysicsVector();
        gAcceleration = earth.gravityAcceleration(new PhysicsVector(Position));

        Velocity.increaseBy(PhysicsVector.scale(deltaT, gAcceleration));

        Position.increaseBy(PhysicsVector.scale(deltaT, Velocity));
        System.out.println(Position.getX());
    }
}

0 个答案:

没有答案