Ok, having finally understood it I can safely say the way you have explained it was....overcomplicated. :P Perhaps simple if you know a LOT about mathematics, but I am a bit of an armchair scientist when it comes to that sort of stuff. Thankyou for pointing out that there was some flaw in my logic though.
I have been studying the use of a triangular matrix to store the force vectors for each gravitational relationship.
The efficiency is becoming clear. For a level with 5 asteroids, a standard square matrix would store 25 pieces of data. With the triangular matrix I can have the same information, but with just 10 pieces of data.
is the number of asteroids in the level, the number of pieces of data needed in a square matrix is N^2
. The number of pieces of information that one needs to store in the triangular matrix is given by (N^2 / 2) - (N / 2)
Pictured here is possible data structures for a level with 6 asteroids.
Using the above formulas...
6*6 = 36. You need to calculate 36 pieces of data every game cycle if using the Square matrix.
6*6 = 36 / 2 = 18, and 6/2 = 3... so 18 - 3 = 15. You need just 15 pieces of data if using the triangular matrix. Go ahead and count the boxes to check, if you want. :>
So for a level with 50 asteroids, the square matrix would balloon to 2500 pieces of data. But the triangular matrix would need to store 1225 pieces of data. When you consider each of these pieces of data must be recalculated every game cycle
, the desirability of an efficient data structure becomes clearer. It should hopefully result in smoother (ie less jerky) movement of the asteroids. :>