It looks like I was using degrees instead of radians. I fixed that. Now I'm just running into a physics problem. The planets should speed up as they approach the perihelion and should slow down as they approach the aphelion. It's doing the opposite.....
I wouldn't suppose you have any ideas?
- class Planet {
-
- float a, e, phi;
- float period, angularVelocity;
- List<PVector> path;
- int timeStep;
- int pointCounter;
- Orbit orbit;
-
- Planet(float a, float e, float phi){
- this.a=a;
- this.e=e;
- this.phi=phi;
- this.period =(float)( (2 * Math.PI) * Math.sqrt(Math.pow(a, 3) / MU));
- this.angularVelocity =(float)( (2 * Math.PI) / period);
- this.path = new ArrayList<PVector>();
- this.timeStep=10;
- this.pointCounter=0;
-
- double r,x,y,z;
- float theta;
- for (int i = 0; i < period; i += timeStep) {
- theta = angularVelocity * i;
- theta %= (2 * Math.PI);
- r = getRadius(theta);
-
- x = width/2.0 + r*sin(theta)*cos(phi);
- y = height/2.0 + r*sin(theta)*sin(phi);
- z = r*cos(theta);
- path.add(new PVector((float)x,(float)y,(float)z));
- }
- this.orbit = new Orbit();
- }
-
- PVector getLocation(){
- return path.get(pointCounter);
- }
-
- void incTimeStep(){
- pointCounter++;
- pointCounter%=path.size();
- }
-
- I_PathGen getOrbitPath(){
- return orbit;
- }
-
- double getRadius(double theta){
- return ((a) * (1 - Math.pow(e, 2)) / (1 + (e * Math.cos(theta))));
- }
-
- void draw(){
- PVector currLoc = getLocation();
- pushMatrix();
- translate(currLoc.x, currLoc.y, currLoc.z);
- sphere(10);
- popMatrix();
- }
-
- class Orbit implements I_PathGen {
- public float x(float t) {
- // change this to return the calculated x
- // position along the path
- float theta = radians(t*360);
- float r = (float)getRadius(theta);
- float x = width/2.0 + r*sin(theta)*cos(phi);
- return x;
- }
-
- public float y(float t) {
- // change this to return the calculated y
- // position along the path
- float theta = radians(t*360);
- float r = (float)getRadius(theta);
- float y = height/2.0 + r*sin(theta)*sin(phi);
- return y;
- }
-
- public float z(float t) {
- // change this to return the calculated z
- // position along the path
- float theta = radians(t*360);
- float r =(float) getRadius(theta);
- float z = r*cos(theta);
- return z;
- }
- }
- }
It seems like the star is at the wrong foci in the ellipse...
I did run into this problem when I did this in 2D so I added 2*a*e to the x coordinate and it drew it correctly, although I'm not sure this will work here.
Edit: I also welcome cc.
Thank you for all you're doing for me :)
Edit 2: I figured out my problem with the physics. I needed to offset my z-axis. /shrugs
Anyway everything is working great. I also picked up PeasyCam and that helped a lot.
Thank you very much for everything you did, quarks.