Collision Detection

Hello guys.

I been working on a prototipe for a game like Mr.Driller and this is what i got so far:

qGc0TqfxUdA

As you can see, its not perfect, there some things in need of improvement, but sence its a prototipe, its k.

So question is:
How to make blocks drop when theres no block below?
i have worked with some code but cant seem to make it work.

p.s.: prototipe is for Android and uses Opengl ES

Thanks!

I think you have a grid like this:
grid[20][100];

ok so grid[15][90] = air;


for(int i = 91; i <= grid[0].lenght; i++){
  if(grid[15][i] != air){
    grid[15][i-1] = grid[15][i];
    grid[15][i] = air;
  }else
    break;
}

This should work :wink:

So… wait… first: im not a very experienced programmer, i just drawed the blocks on those positions.
your saying i first should draw the blocks with this grid?

Please help, i kinda know what you mean, but i have the blocks just drawed there
every block has a model a view and a controler too

Ok, what I should do is this:


int[][] grid = new int[width][height];
Image[] image = new Image[3];
image[0] = xImage;
image[1] = yImage;
image[2] = zImage;

for(int x = 0; x < grid.length; x++){
  for(int y = 0; y < grid[0].length; y++){
    grid[x][y] = (int) (Math.random() *3); //random generated 'world'
  }
}

public void render(){
  for(int x = 0; x < grid.length; x++){
    for(int y = 0; y < grid[0].length; y++){
      if(grid[x][y] != air)
        draw( image[ grid[x][y] ]); //draw our random generated 'world'
    }
  }
}

public void deleteBlock(int x, int y){
  grid[x][y] = air;
  for(int i = y+1; i <= grid[0].lenght; i++){
    if(grid[x][i] != air){
      grid[x][i-1] = grid[15];
      grid[x][i] = air;
  }
}
}

Code is most of the time easier to understand then words :slight_smile:

You Sir, are amazing.
Thank you a LOT.

Some little fix


public void deleteBlock(int x, int y){
    grid[x][y] = air;
    for(int i = y+1; i < grid[0].length; i++){
        if(grid[x][i] != air){
            int _i = i - 1;
            while (grid[x][_i] != air && _i >= 0) {
                _i--;
            }
            if (_i >= 0) {
                grid[x][_i] = grid[x][i];
                grid[x][i] = air;
            }
        }
    }
}