I noticed that the 10 out front was equal to sqrt(M) for M = 100 and I hypothesized that v = sqrt(Mr). I then created a function in Java called orbit() that takes in a mass and radius and returns a velocity. I set this up to make the planet orbit at a variety of different values of M and r and it worked perfectly. This made me think that perhaps I had messed up the force of gravity calculation in the code and the simulation we were seeing was not accurate. At first glance, I couldn't find anything wrong with it but after stepping through with the debugger, I found the problem. Here was the original code:
Vector2 f = r.nor().scl((float) (m1 * m2 / Math.pow(r.len(), 2)));
This is the basic formula for gravity in vector form. The two masses are multiplied together then divided by the magnitude of the radius squared and finally everything is multiplied by the unit vector in the direction of r. The problem here was that calling the function r.nor() didn't simply return the unit vector of r. It also changed the value of the vector object for r in memory. So then later in that same line when we took the magnitude of r with r.len() we were actually taking the magnitude of the unit vector instead, which was 1. This made get rid of the magnitude of the radius entirely from the formula for the force of gravity and it would just equal (Mm)(r-hat). Then when we calculated the velocity for a circular orbit, we would get v=sqrt(Mr) instead of v=sqrt(M/r) like it should have been. I fixed the code by declaring the magnitude and unit vector before plugging them into the formula. I made sure to put the declaration for r_mag before r_hat so that the magnitude of r wouldn't be affected by the normalizing of r. Everything is now working the way it should.
float r_mag = r.len(); Vector2 r_hat = r.nor(); Vector2 f = r_hat.scl((float) (m1 * m2 / Math.pow(r_mag, 2)));
No comments:
Post a Comment