- #1
whatisreality
- 290
- 1
I'm trying to write a class for the gravitational field of any planet (I tested it with values for Earth though), and it gives completely the wrong answer! I suspect this is either a mathematical error, or an issue with my declarations of stuff as public, private, static etc. Compiles, just doesn't actually work! I can also post my main class if helpful.
I have access to a class called PhysicsVector, which can perform pretty much every operation imaginable on vectors.
Here's my code, trying to use Newton's law of Gravity:
I have access to a class called PhysicsVector, which can perform pretty much every operation imaginable on vectors.
Here's my code, trying to use Newton's law of Gravity:
Java:
import java.lang.Math;
public class GravField{
public static final double G = 6.674*Math.pow(10,-11); //Gravitational constant, same for everything
private double planetMass=0;
private double planetRadius=0;
private double projectileX=0;
private double projectileY=0;
PhysicsVector projectilePosition = new PhysicsVector(a, projectileY); //Haven't declared a yet, don't know if that's allowed
PhysicsVector gravityAcceleration = new PhysicsVector();
public GravField(double planetMass, double planetRadius, double projectileX, double projectileY){
double distance = Math.sqrt(projectileX*projectileX+projectileY*projectileY); //Distance of projectile from Earth
double a = planetRadius + projectileX; //Newton's law assumes origin at planet centre,
//my co-ord system starts at planet surface
double x = (-G*planetMass*projectileX)/(distance*distance*distance); //Probably some maths mistake here
double y = (-G*planetMass*projectileY)/(distance*distance*distance);
gravityAcceleration.setVector(x, y); //setVector is in PhysicsVector class and makes vector
gravityAcceleration.print2D(); //xi + yj
}
public static double magnitude(PhysicsVector gravityAcceleration){
double magnitudeOfGravField = gravityAcceleration.magnitude(); //again magnitude is in PhysicsVector class
return magnitudeOfGravField;
}
}