Are you set on solving in via physics\gravity? Since the system in the video has nothing to do with one:
- You just have a grid (stored as 2d array) - the
State
, and have a function likeEvolve(State)
; Evolve(State)
analyse the state and if anymoves
are possible -performs
it.- Probably it have a feature like “process only top element of each column per
Evolve(State)
call”, but it’s something you may experiment with later;
- Probably it have a feature like “process only top element of each column per
- The
move
is a something like:- Check
Grid[i][j]
element:- If it’s a stone and it have a place to go left - go left
- In code it’s something along the lines “check i’m the stone, check left, check left and down, perform”:
if(Grid[i][j].NotEmpty() && Grid[i+0][j+1].Empty() && Grid[i+1][j+1].Empty()) {PerformMove(i,j,i+1,j+1);};
- In code it’s something along the lines “check i’m the stone, check left, check left and down, perform”:
- if it have a place to go right - go right
- if it have no places to go - do nothing
- If it’s a stone and it have a place to go left - go left
- repeat for every
Grid[i][j]
(optimizations are possible, but it’s the simplest solution)
- Check
perform
is a two steps:- make actual move of element
Grid[i][j]
toGrid[i+1][j+1]
- animate the move
- once animation is over - go for next iteration of
Evolve(State)
- once animation is over - go for next iteration of
- make actual move of element
- Once
Evolve(State)
not changing theState
anymore - you may let user perform input and add a new element into theGrid
Note that the Grid
in the video have an offset of 0.5 element for odd rows - your manipulations with indexes of the Grid
have to properly handle this feature.
Well, overall it’s something like this.