Introduction

In this tutorial, we're going to construct a Grid to check for collisions in a 2D map. Here are some of the variables we'll be using.

1 2 3

intmapWidth; // Width of map (in pixels)intmapHeight; // Height of map (in pixels)intcellSize; // The size of each cell in the grid

And here's the basic Entity class we'll be using.

1 2 3 4 5 6 7

publicclassEntity {

publicintx, y, width, height;

// Your entity methods

}

Why using Grids

If the entities in the map are less than 50, the brute force collision method is fine. But it is dead slow if the number of entities tend to increase. So if the map has 100 entities, we need 4950 checks for collision detection. So here comes the idea of binary partitioning. There are many forms of it. Some of them are QuadTrees, Octrees (or) kdTrees, BSPTrees, bins (or) Grids.

For 2D maps, QuadTrees and Grids are the most popular. And BSPTrees are for 3D space. Quadtrees should be faster than grids on scenes with lots of entities. However, if the target platforms have slow rams, grids are way to go since you get penalty for accessing non-contigous data in memory. So let's implement a Grid.

Our Goals with Grids

We can use grids to

Differentiate the entities in to Cells

Retrieve a list of entities which are likely to collide with a given entity and

publicEntitygetNearest(Entitye) {// Calculate and get the nearest entity }

}

Calculating the cells the Entity is in

To add an entity to the grid, we have to find the cells in which the entity fits.

1 2

intcellX = entity.x / cellSize;intcellY = entity.y / cellSize;

The problem with this code is that it only finds the cell which is the top left in the cells the entity is in. But we need all the cells the object is in. So we calculate the top left and bottom right points and add all the cells in between.

Shouldn't it be noted that it's faster to repopulate the grid every tick, than to remove an object from the cells and then add it to another? (At least that's what I've gathered from reading this forum)

I've used grids before doing some 13h particle physics based on Hugo Elias' grid article (forgot the linky).

While grids are "sometimes" faster than a tree-based space partitioning system, these sentences needs a little edit:

Quote

And BSPTrees are for 3D space. Though QuadTrees are usefull for some extent, they are less faster than Grids. So let's implement a Grid.

You might want to change "BSPTrees" to octrees and kdtrees since BSP trees does not work like a grid.

Quadtrees should be faster than grids on scenes with lots of entities. However, if your target platforms have slow rams, grids are way to go since you get penalty for accessing non-contigious data in memory. Then again you are using lists for your grid. '*)

SHC, I suppose creating new objects every tick would create a lot of garbage. Only way, that I can see, around that, would be to store all entities in a list and then iterate through that list and add them to the grid. (Not sure how much extra ram this'd use extra though, probably not twice as much because of pointers.) Thus when you remove an entity from the list of entities, it'll be removed from the grid 'automatically.'

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org