Sign in to follow this  
Liam M

Tile based collision: What would be best

Recommended Posts

I’ve pretty much finished my tile engine, or at least the barebones, and I’m currently in the process of adding entities (monsters NPC's and players), to my world. My original idea for collision was that each tile had a pointer that was either null, or pointed to o collision structure. This structure contains a list of points, which one after the other create a polygon which is the "solid", piece of that tile. Do you think this is a good idea, or a bit overkill? Keep in mind my tiles are 64*64, and each entity is surrounded by a bounding circle. If you don’t think its a good idea, how would you suggest doing it? And if you do think it’s a good idea, could you recommend any calculations/ideas for the process of testing collision of this type on a tile based world.

Share this post


Link to post
Share on other sites
You seem to have a pretty good idea already but do you intend to store all the solid points of the tiles in the collision structure? An advantage of using tiles is that you can calculate the solid points of each tile using its index and its type, and that way don't need to store all the solid points. This decreases the memory usage but requires some more calculations to be done during runtime.

The basic way to do it is as follows (pseudocode):
1. Calculate the left, upper, back tile which the entity overlaps (the indices of the tile).
2. Calculate the size of the entity in "tiles".
3. Iterate through the overlapped tiles (which are the ones possible to collide with).
3.1. If the tile is solid or depending on the current tile type
3.1.1. Calculate the points making up the surfaces of the current tile
3.1.2. Run the collision test for this particular tile (using the calculated points) and displace the entity/kill it, or something like that depending on what type of tile was/wasn't collided with.

This is how I did it (reproduced from memory):

//1. - calculate the first overlapped tile indices
//using the world coordinates of the entity the dimensions of the tile
//(left - along x-axis, top - y-axis, back - z-axis)
left = entity.Left() / tilebuffer.TileWidth();
top = entity.Top() / tilebuffer.TileHeight();
back = entity.Back() / tilebuffer.TileDepth();

//2. - calculate the size of the entity in tiles
//add 1 to compensate for truncation
width = entity.Width() / tilebuffer.TileWidth() + 1;
height = entity.Height() / tilebuffer.TileHeight() + 1;
depth = entity.Depth() / tilebuffer.TileDepth() + 1;

//3. - do the collision test for each overlapped tile
for (bufferX = left; bufferX < left + width; ++bufferX)
{
for (bufferY = top; bufferY < top + height; ++bufferY)
{
for (bufferZ = back; bufferZ < back + depth; ++bufferZ)
{
//3.1. - instead of testing just TileIsSolid
//the type of the tile could also affect the collision
if (tilebuffer.TileIsSolid(bufferX, bufferY, bufferZ))
{
//3.1.1. - calculate the endpoints of the tile (8 points)
//(could create the polygons instead here)
x1 = bufferX * tilebuffer.TileWidth();
x2 = (bufferX + 1) * tilebuffer.TileWidth();
y1 = bufferY * tilebuffer.TileHeight();
y2 = (bufferY + 1) * tilebuffer.TileHeight();
z1 = bufferZ * tilebuffer.TileDepth();
z2 = (bufferZ + 1) * tilebuffer.TileDepth();

/*3.1.2. - run the collision test on this single solid
(a bounding-box variant in my case, the last argument is
whether the entity should be allowed to enter the tile)*/


entity.BoxCollision(x1, y1, z1, x2, y2, z2, true);
}
}
}
}







That should do to demonstrate what I meant. Good luck!

Share this post


Link to post
Share on other sites
Yes, i am using my tileset to hold my collision information. the main problem i am having is with the collision mathematics. Thanks for the help Jimmy H, but its rather hard to describe exactly what i mean. Il spend this weekend working on it, and post here (maby a demo even), of what i come out with.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this