Author Topic: Gravity Engine - dev blog  (Read 11074 times)

0 Members and 1 Guest are viewing this topic.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #15 on: April 21, 2010, 10:26:34 PM »
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.

Where N 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.  :>

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #16 on: April 21, 2010, 10:30:06 PM »
In other news, I just managed to persuade my flatmate to make me not one, but TWO cups of tea.  At the same time.  I just finished drinking the first, starting on the (still lukewarm) second tea now.

njursten

  • Seedling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
Re: Gravity Engine - dev blog
« Reply #17 on: April 22, 2010, 09:51:48 PM »
Yeah, you're right. It would have made more sense to start with explaining vectors and matrices and their operations properly first. Hm, I'd recommend you to read up on it. Wolfram's MathWorld might be a good place to start (http://mathworld.wolfram.com/Vector.html). Though maybe it's a bit too scientific... I don't know what other resources there are though. Maybe Wikipedia?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #18 on: April 23, 2010, 02:44:22 PM »
I watched a few mathematics lectures on youtube
I watched an entire series of physics lectures by Prof. Richard A. Muller, about 30 hours in total
I spent a lot of time talking to my cousin who works for Sony making fancy 3D stuff (and knows lots about linear algebra)
I visited various websites - of which Wolfram's Mathworld was one - learning about Linear Algebra
I spent time scribbling on A4 pieces of paper at evenings and weekends, practicing problem questions and trying to visualise what is going on.

I actually learned a lot more than was strictly necessary to solve this problem - but just getting used to thinking about vectors really helped me to see what was wrong in the end.  I went back over my first initial posts and looked at what I had been proposing, and realised the mistake I had made.  Hopefully the coding part will be that much easier as a result of the groundwork I've put in.



Tonight I will make a concerted attempt to figure out what code would create a triangular matrix like the one I posted previously.

Might even start the actual coding this weekend, too...  :>

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #19 on: April 23, 2010, 02:44:47 PM »
By the way, regarding this:

Quote
No, it won't be positive all the time, because xdiff and ydiff are not always positive. If asteroid B is to the left of asteroid A, then xdiff is negative and otherwise positive. Similarly for ydiff. Another important point is that you can't really do it any other way, you have to see how far into the quadrant the asteroid is. That more or less boils down to my equations.

The comments I made that you are referring to, I made before having understood what the problem was.
Therefore no longer relevant.  :>

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #20 on: April 23, 2010, 06:16:53 PM »
Hmm, I wonder... If I use the following command:

Code: [Select]
for j = 0,0 do

Does that mean that it will basically skip over anything inside the For loop, ie it won't even run those commands once?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #21 on: April 23, 2010, 06:32:06 PM »
aaaaaand that's a negative.  It runs once.

for test = 0,-1 do runs zero times.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #22 on: April 23, 2010, 06:49:54 PM »
So this code should create the triangular matrix.

Code: [Select]
--COLUMN

for i = 0,roidnumber do

--ROW
for j = (i + 1),roidnumber do


-- calculate!!


end
end


annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #23 on: April 23, 2010, 06:59:19 PM »
Code: [Select]
for i = 0,numAsteroids do
   for j=i+1,numAsteroids do
      ForceMatrix[i][j] = Fg
      ForceMatrix[j][i] = Fg
   end
end


Oh.  You already had it there.  lol.
Well, I honestly did figure it out on my own as well! :P

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #24 on: April 23, 2010, 10:31:31 PM »
Started coding.  Got a brand new error that I've never seen before!  :>



annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #25 on: April 23, 2010, 11:13:40 PM »
Am having difficulty with the part where I need to calculate seperation distance between asteroids based on their coordinates.  Need moar maths..

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #26 on: April 24, 2010, 12:18:20 AM »
Giving up for tonight.  Made some good progress I guess, the triangular matrix seems to work good, and the asteroids move....just not the way they are supposed to.

The problem at the moment is that I am improperly using a scalar to calculate the acceleration.  The vector is there too and I am sure I can use it, but my brain is tired for tonight so I am gonna get some sleep and work on it more in the morning.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #27 on: April 24, 2010, 12:17:33 PM »
Question for Njursten;  how is xdiff and ydiff calculated?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #28 on: April 24, 2010, 01:04:27 PM »
Sweet!!  Got it working! :D


edit... wait, nope.. :p

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Gravity Engine - dev blog
« Reply #29 on: April 24, 2010, 01:27:43 PM »
ok... seems to be working now.  Got 2 asteroids orbiting one another.  :>