# Collision detection when considering drawing through iteration.

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

## Recommended Posts

Given a N number of surfaces within a grid, each one being a game obstacle where collision makes itself an unavoidable necessity, how can I detect the collision of a player or any game object if the whole map or grid of surfaces is being drawn through iteration? Suppose that the whole map has a total of 20 tiles of walls (The grid has "free spaces" displaced randomly between the walls creating paths), now imagine that each tile is a surface (a loaded image), and that their x and y positions are being set through iteration (Considering that the map is actually a XML file from Tiled software, and, therefore, there's iteration because of the necessity of reading).

These 20 tiles, each one with their own x and y positions (and width and height) need to be detected by the player or game object. Considering that I'm programming in Python and that each one of these tiles are stored in a list, I can easily iterate through all of them... But then comes the problem:

If I iterate through all the tiles and get the x and y positions through the index, I can't test wether the player is above or below or left or right anything, because all the tiles are being accessed "at once"! (Not technically speaking) At the same time, I need to iterate over everything, because the map could have 3000x3000 tiles. If I test if the player is above the index y position, the condition will be true many times because there'll be many tiles below it as a whole.

I did not post any code because the whole is quite complex, and I'm afraid I would not make myself sufficiently clear (I don't know if I've made it anyway). It's a complex problem and I'm still trying to solve it. I had the idea of "projecting" an invisible tile at the next position of the player considering it's moving-direction in the grid to test wether there is an obstacle-tile there or not, but I'm still thinking about it.

I appreciate any idea/comment, for it would be much helpful.

Edited by EricsonWillians

##### Share on other sites

Does this list represent a regular grid of tiles in a specific order?  If so, you shouldn't have to iterate over the entire list to perform collision detection with the player.  Just determine the player's position within the list and then determine the 8 or so adjacent tiles in the list.  No need to perform collision detection against non-adjacent tiles, and since you have random access to the list elements, no need to iterate over the entire list.

##### Share on other sites

Does this list represent a regular grid of tiles in a specific order?  If so, you shouldn't have to iterate over the entire list to perform collision detection with the player.  Just determine the player's position within the list and then determine the 8 or so adjacent tiles in the list.  No need to perform collision detection against non-adjacent tiles, and since you have random access to the list elements, no need to iterate over the entire list.

The list does not contain the "empty" tiles (I'm developing a platform game, therefore, the "empty tiles" are actually the background). Here's a screenshot of my engine/game:

The map information is a XML file:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="10" height="10" tilewidth="175" tileheight="150">
<tileset firstgid="1" name="ts1" tilewidth="175" tileheight="150">
<image source="../../Evil Shadow/GFX/ts1.png" width="875" height="300"/>
</tileset>
<layer name="Tile Layer 1" width="10" height="10">
<data>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="3"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="4"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
<tile gid="1"/>
</data>
</layer>
</map>



As you can see, the game obstacles are the gids above 0, and the 0 ones are the "empty" tiles. The final result of my parser gets all tile-objects above 0 and put them in a list, and each one has a x and y position (The width and height are default in the specified grid). I can't determine the position of the player within the list because the list are just the obstacles, but the player moves within the same grid.

##### Share on other sites

Firstly, I have very little idea what on earth you are talking about. Perhaps it is a language barrier issue but you seem to be describing this in a very complex way.

Second: if this is tile based and storing the tiles in a list is proving problematic, store the tiles in a 2D array. Then you can use the player's co-ordinates to figure out the cell the player is in and just look at the surrounding cells. player_x / tilewidth is the cell and player_x % tilewidth is the offset into the cell for example.

Third: I'm currently working on a 2D game that uses a list of polygons for the level and I'm struggling to understand what you are trying to do here that is different. I currently have no broadphase so a highly inefficient system but I just iterate through every shape and if the player's shape intersects the list shape, I modify the player's position by the minimum translation vector to separate them.

An easy improvement to this will be to map the polygons into some kind of spatial partioning structure, but if you are using a tiled grid, that is exactly what you already have, you are just not representing it as one.

Hope this helps.

Edited by Aardvajk

##### Share on other sites

I would recommend putting the empty tiles into the list as well.  As long as they are flagged appropriately, your drawing code can ignore those tiles.  Your tiles should be lightweight objects, so this shouldn't incur much of an increase in memory footprint, and the benefit of that additional memory usage is greatly simplified access to adjacent tiles in your code, as well as better overall performance of the program.

##### Share on other sites

It's okay to have a list and an array.  One for drawing and one for navigation/collision.

Though honestly, having to iterate over 3000x3000 tiles, unless you are actually drawing a good portion of them on the screen, seems expensive, compared to say, looking at your viewport, determining the start x,y and end x,y and then doing a for loop over those tiles.

##### Share on other sites

I would recommend putting the empty tiles into the list as well.  As long as they are flagged appropriately, your drawing code can ignore those tiles.  Your tiles should be lightweight objects, so this shouldn't incur much of an increase in memory footprint, and the benefit of that additional memory usage is greatly simplified access to adjacent tiles in your code, as well as better overall performance of the program.

I was walking in the streets today and I was "programming mentally" as I usually do.. And I was philosophizing in what you've just said earlier: "The player within the list..".. And I kept saying to myself: "The player within the list.. The player within the list.." And then I said: "Bloody hell! The guy was right." Then, it was clear as hell that I needed to include the zeros/empty-tiles on the list as well (Together with the player) to serve me as reference (Just what you said now). I'll implement it now :).

I can't detect collision through iteration because the list has only the obstacle-tiles (Only their positions), and I need to move the player through the list (Through the zeros). In that way I'll be able to check wether the tile is an obstacle or not.

##### Share on other sites

Glad to hear you have a solution, but I'd still stress that for a 2D tile map, a list is the wrong way to represent the tiles. An array containing the tiles that you can jump to the correct tile based on an X and Y is far better.

Multidimensional arrays are a bit unwieldy in C++ so would suggest using a vector with some kind of subscript access:

class Map
{
public:
Map(int w, int h) : d(w * h), w(w), h(h) { /* fill tile data */ }

Tile &operator()(int x, int y){ return d[(y * w)] + x; }
const Tile &operator()(int x, int y) const { return d[(y * w)] + x; }

private:
std::vector<Tile> d;
int w, h;
};

Tile getTileAt(int worldX, int worldY, const Map &map)
{
return map(worldX / tileWidth, worldY / tileHeight);
}


You can then do things like, for example, only iterating across the visible tiles when you are rendering, and only considering tiles adjacent to the player when doing collision detection, so the complexity of your operations is no longer bound by the size of your map.

##### Share on other sites

Glad to hear you have a solution, but I'd still stress that for a 2D tile map, a list is the wrong way to represent the tiles. An array containing the tiles that you can jump to the correct tile based on an X and Y is far better.

He said he was using Python, and Python lists are randomly accessible.  I take that to mean that they are implemented using arrays under the surface, likely similar to C++ vectors.  C# lists are similar.  I always love when different languages cross-pollute terminology.

##### Share on other sites

He said he was using Python, and Python lists are randomly accessible.  I take that to mean that they are implemented using arrays under the surface, likely similar to C++ vectors.  C# lists are similar.  I always love when different languages cross-pollute terminology.

Oops, thanks and apologies for the confusion I've added to the thread.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11