Followers 0

# Collision detection when considering drawing through iteration.

## 10 posts in this topic

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
0

##### 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.

0

##### 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.

0

##### 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.

1

##### 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.

0

##### 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.

0

##### 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.

1

##### 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.

0

##### Share on other sites

I've handled things in a somewhat unconventional way, I suppose. First of all, I don't exactly move things in my engine by pixel. I've created a generic Grid class that takes in its constructor a number of tiles in width, a number of tiles in height, the width of each tile and the height of each tile. I've created another class called GridMap, where I permute every possible "tile position combination" in the map (20x20 tiles, por example, than I would have: (0,0), (0,1), (0,2), ........ (5,7).... and so on). These tuples are keys in a dictionary, and I store the surfaces as a value for each key (And I draw them by iterating over the whole HashMap (Actually is more complicated, because of the XML file. So each value for each key-position of the hashmap has a tile ID and a surface asssociated with it.)). I did not show code because I would need to explain firstly how I've implemented the whole "grid system".. It's all based in keys/hashmaps, and things move by keys instead of pixels (And one can use more than one grid in the engine. Big tiles for drawing and small tiles for locomotion, for example.. But everything always fixed).

Edited by EricsonWillians
0

## 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