Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Probably easiest would be to work entirely in a system of (x,y) coordinates to calculate gravity and switch to the array coordinates when you finally need to lookup and store objects.</p> <p>In your example, consider (2, 4) (red) to be the center of gravity; (5, 1) (blue) needs to move in the <em>direction</em> (2-5, 4-1) == (-3, 3) by the <em>distance</em> _n_. You get decide how simple you want <em>n</em> to be -- it could be that you move your objects to an adjoining element, including diagonals, so move (blue) to (5-1, 1+1) == (4, 2). Or perhaps you could move objects by some <a href="http://en.wikipedia.org/wiki/Scalar_multiplication" rel="nofollow">scalar multiple</a> of the <a href="http://en.wikipedia.org/wiki/Unit_vector" rel="nofollow">unit vector</a> that describes the direction you need to move. (Say, heavier objects move further because the attraction of gravity is stronger. Or, lighter objects move further because they have less inertia to overcome. Or objects move further the closer they are to the gravity well, because gravity is an <a href="http://en.wikipedia.org/wiki/Inverse-square_law" rel="nofollow">inverse square law</a>).</p> <p>Once you've sorted out the <em>virtual</em> coordinates of your universe, then convert your numbers (4, 2) via some simple linear formulas: <code>4*columns + 2</code> -- or just use <a href="http://en.wikipedia.org/wiki/Multidimensional_array#Multi-dimensional_arrays" rel="nofollow">multidimensional arrays</a> and <em>truncate</em> your floating-point results to get your array indexes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload