# Suggestions for collision detection ...

This topic is 4040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I just need a suggestion as to how to implement collision detection in my game. The game is top-down ( like cannon fodder ) and has tiles which are drawn half-grass and half-forest. Enter stage left: reason for uncertainty - At the minute the map is stored in a multi-integer array mTileMap[x][y][l], and all of the characters in the game are floats. The [x] and [y] are obviously the tile positions, and the [l] is of size int 3, and is used for the tile information. At first I just wanted to use [l] to store an integer value for passable or not, but the tiles are too big to allow for this, so I need a more precise way of declaring ONLY PARTS of tiles passable. Take an example of a tile being half forest (impassable) and half grass (passable), so I would have to declare this half-half. and thoughts / ideas would be greatly appreciated ... -Thanks [Edited by - LintfordPickle on January 26, 2007 3:29:44 AM]

##### Share on other sites
You could simply make an integer listing what type of landscape is in the spot attempting to be moved to; and for each type of player object have an array of booleans if they can move on the different types? then do a quick check?

if(object.canMove[type])
goToMoveMethod();//?

##### Share on other sites

That was my problem, because the tiles I have are larger than the units, 1 integer to represent a whole tile wouldn't be enough. e.g.

A tile of size 64x64 pixels:
___________
| . . . . . . . .| = water
|. . . . . . . .|
|-----------|
|/////////////| = grass
|__________|

On this tile, the lower half should be passable, and the top half not, and I don't know the best way to represent a half tile.

I think at the minute, it might be better to use a linked list of rectangles, all of which I can't pass, and just check the units movement, but I want to do a map editor in the future and don't know if this will be do-able.

##### Share on other sites
well, make the array have four ints, representing the tile. One to represent each corner or something. If your going to make a map editor it would be much more simple to just identify the type of landscape, rather then splitting the leval up as passable or not. Then have the checks during movement.

Just an oppinion.

##### Share on other sites
You could have a different size, seperate walkability grid to tile grid. In fact you could use both :
two-tiered

##### Share on other sites
Hi,

I'll take a look at the 2-tiered method, but for the moment maybe you could tell me ( in advance of me finishing it ) whether the following would work, I have the int array mTileMap[x][y][l].

X, Y = Position.

L = Layer and the values are 1 through 20, representing the following :

1 = texture to display for the current co-ord.

2 = walk / swim ( 1 / 0 )

3 - 19 = Each tile [X][Y] will be divided into 16 smaller squares, numbered 3 - 19, and the integer value will represent the 'passable-ness' of each square. Then all I have to do is check the movement of the player based on this last integer. ( 1 / 0 )

Do you think this will work, or are there some problems that I'm not taking into consideration? ( like int mTileMap[100][100][20] would be too big etc. etc. )

##### Share on other sites
I personally consider breaking up a single grid coordinate into several pieces very time consuming. It is doing many checks each frame.

I would just set up polygon collision lines, and check only the lines that could possibly be within the player's sprite.

To check whether or not the player hits water or not is another question. You could use that line to determine which half the player is crossing the most of and do it that way.

Just a thought.

##### Share on other sites
Wouldn't it be better for your tilemap array to contain an array of tile objects? That way one of the attributes can be 'walkability' and hold an actual
value (e.g. 5 for grass, 1 for swamp etc). Your way will probably work but you're going to end up stuck for something else (e.g. when destroyed what tile should be displayed or this tile contains a trap or whatever).

##### Share on other sites
This may be a noob suggestion or question - whichever.
Given the above setup - why not define your tiles yourself, with say 0-99 as walkable (grass, sand, beach, etc.) and 100 and up being impassable.
This would effectively give you 99 tiles that are passable and many more that would be impassable.
A movement/collision check would be something like if blah < 100 then passable else impassable.
Just a noob trying to make since of this stuff.

##### Share on other sites
I would also use tonyg's idea...

For every tile-object you could implement a check Method to see if the "next step" is possible.

tile[j].canMove(x, y);

The objects could save if the tile is blocked vertical or horizontal
Horizontal:____________|. . . . . .| = water|. . . . . .||-----------||///////////| = grass|___________|Vertical:____________|/ / /|. . .| |/ / /|. . .||/ / /|. . .||/ / /|. . .| |/_/_/|._._.|

So if you try to access tile 2,3 on the pixel 5,5 (horizontal blocked)you will get a false from the method, because the tileObject is blocked...

##### Share on other sites
Hi all,

I haven't logged on for a bit, but thanks for all your replies.

I apologize ( yet again ) because I think you are misunderstanding the question, the problem is not blocking tiles, I could block 1 whole tile should the contents be impassable( which probably wouldn't be too hard ), but the problem is, that I can't find a way to represent the actual CONTENTS of one tile. Even if I declared my tile map array to contain an array of tile objects, I would still have to represent the CONTENTS of a tile. ( the only way I can think is to brake it down into smaller pieces, or to draw / define a bunch of rectangles over the pre-rendered map to represent impassable areas ).

I have drawn each tile as 64x64 pixels in a bitmap. One tile has, for example, a river following down the middle, BUT there is STILL GRASS within the same tile, the problem is determining which part of the tile is grass, and which part water.

Looking at the solutions, they all seem to provide a way to only block the full tile, and not a portion of one. Sorry if I haven't understood a perfectly valid solution already posted,

and thanks for all your help, I appreciated it :)

-Lintford

##### Share on other sites
Maybe its not a perfect way to do it, but it should work...

You have a tile like this: (sorry.. programmers art)

If you would save tileObjects in the field array you could easy check how far you can "walk" on the tile by checking the offset of the "blockline".

For example you have a tile like the example picture above.
The attributes block_x would be set to 30 (because blockline is at px 30 of the tile) and block_y would be set to -1 so you know that the blockline is running vertical.

This method only works for tiles like this, If you have tiles like this

it will not work. In this case it might be an idea to store rectangle coordinates for the blocked area of the tile.

I'm not sure if this is an ideal method, because you maybe have to store some more information. But I'm pretty sure this would work :)

Cheers Zerd

##### Share on other sites
... and depending on how big the map is I might still suggest the higher detailed walkability grid. (e.g. tiles 64*64, walkability 8*8). Alternatively, you have a seperate tilegrid the same size as the map with simple black/white shapes for walkability. (like some platform games)