java grid collsions detection

hey guys i have been trying to browse youtube and JGO for any tutorials on grid based collision detection but i can’t seem to find any also i need to be able to build maps out of blocks so i need the grid for that aswell :stuck_out_tongue: any help appreciated as its my first game ever and im struggling a bit :stuck_out_tongue:

When you say grid collision, is each entity contained only inside a grid cells?
If you store all your entities in an array [ ][ ], then you can check neighboring cells, with a range of entity’s x-1 to entity’s x+1 and entity’s y-1 to entity’s y+1. Or just check the cell the entity is moving into.
For storing you can use Entity[ x ][ y ] (or Entity[ x ][ y ][ z ] ).

Aazimon

i really don’t know lol, never worked with a grid before but i am going to check for collisions with objects in the cell yes… but i havfe no idea of how to create “the grid” and so on xD

Don’t worry about problems too much before you’ve even started.

And the way Aazimon said will work. Can’t/shouldn’t have 2 different things on the same tile/cell.

yea i see :stuck_out_tongue: but does anyone know of any tutorials or just something that explains the way to make it ?

You’re probably using java 2D, but oh well…
http://cokeandcode.com/index.html?page=tutorials/tilemap1
made by kevglass (duke on this forum)
it’s for Slick, I think, but explains the idea pretty well. There’s also an A* pathfinding tutorial there.

yea but it still does not explain how to make the grid, it just repeats everything i have read elsewhere :stuck_out_tongue: and i understand all of that i just don’t know how to create the grid

Well, creating the grid is just taking an array[][]. I really don’t know how you are attempting to make the grid. Are you using images, Java2D, OpenGL, Slick?

Regardless, if you want to make a simple 10x10 grid and your “block” objects are 16x16. Here is a snippet that might help (assuming Java2D).



//You can use an array to store images
Image[][] blockarray = new Image[10][10];

public void render(Graphics g)
{
     g.setColor(Color.BLUE);
     for(int i = 0; i < 10; i++){
           for(int j = 0; j < 10; j++){
                 g.fillRect(16*i, 16*j, i, j);//Normal grid with Blue rectangles
                 g.drawImage(blockarray[i][j], i*16, j*16, <Component>);//Image grid
           }
     }
}

Are you wanting to make the Grid lines?
This piece of code will draw grid lines for you.


g2d.setColor(Color.BLACK);
// make the lines dotted lines.
g2d.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[]{3.0f, 3.0f}, 0.0f));
for (int x = 0; x< totalWidth; x+= gridWidth) {
  g2d.drawLine(x, 0,x, totalHeight);
}
for (int y = 0; y< totalHeight; y+= gridHeight) {
   g2d.drawLine(0,y,totalWidth,y);
}