Last night I learned the equation for calculating the force of attraction between two masses.
It is: Fg = (G * CheckedRoid * DistantRoid) / SeperationDistance^2
Fg,
G,
CheckedRoid,
DistantRoid, and
SeperationDistance are all variables.
- Fg is a variable describing the amount of gravitational force acting on an object
- G is the gravitational constant (in reality 0.667 x 10^-11), which I will probably change to something more suited to the scale of a Eufloria level
- CheckedRoid is the asteroid whose gravitational forces we are calculating on this pass
- DistantRoid is the asteroid to which we are measuring the gravitational pull
- SeperationDistance is the distance between CheckedRoid and DistantRoid - calculated using pythagoras theorum: SeperationDistance = math.sqrt((horizontal difference between checkedroid and distantroid)^2 * (vertical difference between checkedroid and distantroid)^2)
I will use a
For loop to check all of the asteroids in turn.
Then, for each asteroid I check (ie
checkedroid), I will use another
For loop to check all the gravitational forces from other asteroids that are acting upon it.
Then, once I have all the different forces acting on
checkedroid, I combine them using Force Vector Summing, another crazy maths concept I learned about last night.
Basically imagine one of the gravitational forces. It has a direction that it is pulling, and it has a magnitude. You could imagine this like an arrow pointing from
checkedroid towards
distantroid. You can imagine that the length of the arrow is indicative of the amount of force it is exerting. So a large
distantroid with a lot of gravity would exert a large force on
checkedroid, and would mean a longer arrow.
So when you plot all of these arrows, you basically get lots of arrows spiking outward from
checkedroid, and pointing at all the
distantroids. The size and the distance of the distant asteroids would be the determining factors of the length of the arrows.
To add all of these arrows up into a single combined arrow, first you pick up all of the arrows except for the very first one.
Where the first one ends, you place the second one's start.
At the end of the second arrow, you place the start of the third arrow, and so on.
If all of the forces acting upon the object are equal and cancel each other out, the line you draw with these arrows will always end up back at
checkedroid where it started - regardless of the order you put the arrows down in.
If the tip of the final arrow is some way off from
checkedroid, you can draw a line between that point and
checkedroid and this line that you draw is the combined direction and magnitude of all of the asteroids.
So that's the principle behind force vector summing. To do it mathematically I have to find something called the
dot product of all of the different vectors.
Here is some pseudo code representing how I intend to do this:
for passnumber = 1, number of asteroids do
if distantroid is in the North East Quadrant compared to checkedroid then:
summedvectorx = summedvectorx + x[passnumber]
summedvectory = summedvectory - y[passnumber]
end
if distantroid is in the North West Quadrant compared to checkedroid then:
summedvectorx = summedvectorx - x[passnumber]
summedvectory = summedvectory - y[passnumber]
end
if distantroid is in the South East Quadrant compared to checkedroid then:
summedvectorx = summedvectorx + x[passnumber]
summedvectory = summedvectory + y[passnumber]
end
if distantroid is in the South West Quadrant compared to checkedroid then:
summedvectorx = summedvectorx - x[passnumber]
summedvectory = summedvectory + y[passnumber]
end
end
At the end of that,
summedvectorx and
summedvectory will be the values needed to show where to draw the force vector for the combined force. Some quick pythagoras gets the distance, and thus the magnitude of the force, and it should be no problem to calculate the angle.
Then its some Newton laws or something, to calculate the effect that force has on that mass, given its present momentum and direction. I will research that later I guess.