Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Collision detection when considering drawing through iteration.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 EricsonWillians   Members   -  Reputation: 288

Like
0Likes
Like

Posted 31 March 2014 - 10:20 PM

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, 31 March 2014 - 10:46 PM.

Creator and only composer at Poisone Wein and Übelkraft dark musical projects:

 


Sponsor:

#2 jHaskell   Members   -  Reputation: 1154

Like
0Likes
Like

Posted 01 April 2014 - 07:33 AM

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.



#3 EricsonWillians   Members   -  Reputation: 288

Like
0Likes
Like

Posted 01 April 2014 - 08:54 AM

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:

screen.png

 

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.


Creator and only composer at Poisone Wein and Übelkraft dark musical projects:

 


#4 Aardvajk   Crossbones+   -  Reputation: 7080

Like
4Likes
Like

Posted 01 April 2014 - 10:44 AM

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, 01 April 2014 - 10:45 AM.


#5 jHaskell   Members   -  Reputation: 1154

Like
1Likes
Like

Posted 01 April 2014 - 11:05 AM

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.



#6 ferrous   Members   -  Reputation: 2248

Like
0Likes
Like

Posted 01 April 2014 - 01:37 PM

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. 



#7 EricsonWillians   Members   -  Reputation: 288

Like
0Likes
Like

Posted 01 April 2014 - 05:11 PM

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.


Creator and only composer at Poisone Wein and Übelkraft dark musical projects:

 


#8 Aardvajk   Crossbones+   -  Reputation: 7080

Like
1Likes
Like

Posted 02 April 2014 - 02:17 AM

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.



#9 jHaskell   Members   -  Reputation: 1154

Like
3Likes
Like

Posted 02 April 2014 - 06:13 AM


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. 



#10 Aardvajk   Crossbones+   -  Reputation: 7080

Like
0Likes
Like

Posted 02 April 2014 - 07:01 AM


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.



#11 EricsonWillians   Members   -  Reputation: 288

Like
0Likes
Like

Posted 02 April 2014 - 11:52 AM

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, 02 April 2014 - 11:55 AM.

Creator and only composer at Poisone Wein and Übelkraft dark musical projects:

 





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS