How To Handle Object/Item Collision

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

How To Handle Object/Item Collision

Post by dandymcgee »

I've never done this before, and quite frankly don't even know where to start. I'm curious as to how others have handled things like collision with an object in an RPG style game.

"Object" is a very general term which I am using to group together pretty much anything a player could interact with in some way.
For Example: Obtainable Items, Chests, Doors, Stairs, maybe even NPCs

Would you store the x and y positions of every object in a certain area, and have your collision detections check against each object to decide how to react to a player's actions, or would this method be too slow or inefficient to be plausible?

I've tried searching around, but good examples of the more specific details of game design such as this are difficult to come by.

Any help is much appreciated. ;)
Last edited by dandymcgee on Wed Apr 29, 2009 6:12 pm, edited 1 time in total.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How To Handle Objects/Items?

Post by MarauderIIC »

If your layout is a grid, you can just check the adjacent grid squares. For an RPG-type thing this should work fine, right?
As for something that needs everything updated frequently, like an FPS, I'm actually not sure :P You might change the topic of your post, though, to something involving the word "collision" :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: How To Handle Objects/Items?

Post by dandymcgee »

MarauderIIC wrote:If your layout is a grid, you can just check the adjacent grid squares. For an RPG-type thing this should work fine, right?
As for something that needs everything updated frequently, like an FPS, I'm actually not sure :P You might change the topic of your post, though, to something involving the word "collision" :)
Well that's my question, when "checking adjacent grid squares" where do I get the data from? Would you store what sort of object was on that tile inside the tile class, in some sort of globally accessible array, or what? That's what I'm trying to figure out. :mrgreen:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How To Handle Object/Item Collision

Post by MarauderIIC »

tilesystem.h

Code: Select all

class TileSystem {
    Tile tiles[10][10];
public:
    bool hasObject(int x, int y);
};
tilesystem.cpp

Code: Select all

bool TileSystem::hasObject(int x, int y) {
    return (tiles[x][y] != 0);
}

Code: Select all

//0,0 at top-left
if ((int)yvel != 0)
    y_direction = (int)yvel/abs((int)yvel);
if ((int)xvel != 0)
    x_direction = (int)xvel/abs((int)xvel);
That might not work so well at xvel between 0 and 1, at which point you may just want to use if (yvel > 0) y_dir = 1...

Code: Select all

Game::someFunction() {
 //is there an object in front of us
    bool objAhead =
        tileSystemInstance.hasObject(player.x + player.x_direction, player.y) ||
        tileSystemInstance.hasObject(player.x, player.y + player.y_direction);
}
Something like that, anyway.

Of course if you do it that way you can get one heck of a huge array.

Edit: To keep array size down, something more like

Code: Select all

struct MapSquare {
    Tile* tile;
    Object* obj;
    Character* char;
};

class Game {
    MapSquare mapData[x][y];
public:
    Object* checkObj(int x, int y);
    Character* checkChar(int x, int y);
};

Code: Select all

Game::checkObj(int x, int y) {
    return mapData[x][y].obj;
}

Game::someFunction() {
    bool objAhead =
        (checkObj(player.x+player.x_dir, player.y) != NULL &&
        checkObj(player.x, player.y+player.y_dir) != NULL);
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: How To Handle Object/Item Collision

Post by dandymcgee »

Thanks for the basic idea Marauder. I'm going to do some work on this tomorrow afternoon, and I did find a couple articles after searching a few more hours. I'll post back after I've played a bit and let you know how it goes. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: How To Handle Object/Item Collision

Post by Ginto8 »

Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How To Handle Object/Item Collision

Post by MarauderIIC »

lol.. with your sig and all,
http://lazyfoo.net/SDL_tutorials/lesson17/index.php
just gonna put that out there ;)
Bam, schooled by a 12-year-old.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: How To Handle Object/Item Collision

Post by eatcomics »

MarauderIIC wrote:lol.. with your sig and all,
http://lazyfoo.net/SDL_tutorials/lesson17/index.php
just gonna put that out there ;)
Bam, schooled by a 12-year-old.
heheh, rofltaco... 12-year-old... lol
Image
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: How To Handle Object/Item Collision

Post by MadPumpkin »

back in my days of making flash games i would, calculate the distance from target to target using minor trigonometry, sine and cosine i would set a maximum distance of how close other objects could get to it.

however, are you doing 3D or 2D game? this is actually harder to do in 2D because you usually use grid/tile based games
in an engine where objects are place by coordinates and not by what tile they are on, it would be much easier to do this
to make this possible to do/easier to do in a tile based game, your going to need to ONLY set starting coords for the objects you want to have special attributes, because
if you have a constant place for them to keep near, then you wont be able to do anything along those lines... well you actually wouldn't need to, you would just make it a solid tile with nice attributes

but in 3D the engines are already usually based on distance from target to target for all things, not just wall collision... which is reeally not easy in 3D lol

later i'll post somthing of how i would go about doing this in flash, and maybe you could fix it into C++? or someone else could because i don't want to have to do it lol
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: How To Handle Object/Item Collision

Post by dandymcgee »

Ginto8 wrote:http://lazyfoo.net/SDL_tutorials/lesson17/index.php
just gonna put that out there ;)
Thanks, but basic rectangular collision has absolutely nothing to do with how to manage object interaction... ;)
(Hah, Ginto - 1 Dandy - 1).

@MadPumpkin - It's a 2D tile-based RPG style game.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
deathsangel
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Apr 18, 2009 8:17 pm

Re: How To Handle Object/Item Collision

Post by deathsangel »

The way I handle objects/items is using layers.

I have one layer for the ground floor
1 layer for items

so then i do a simple check

#define chest 1

for (int r = 0; r < rows; r++) // loop through all the rows
for (int c = 0; c < columns; c++) // loop through all the columns
if (item_map[r][c] == chest) // check the index of the item
if (check_collisions(player, item_map[r][c])) // check collisions
// player touched the item do something...

get it?
Thanks to gyro vorbis for inspiring me to learn how to make a 2d graphics engine :)
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How To Handle Object/Item Collision

Post by MarauderIIC »

Can be slow, you're checking against every item. If you can narrow it down to nearby items (as above)...
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: How To Handle Object/Item Collision

Post by thejahooli »

do you know how you would do this if the player is not tile aligned but the objects are
I'll make your software hardware.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: How To Handle Object/Item Collision

Post by dandymcgee »

thejahooli wrote:do you know how you would do this if the player is not tile aligned but the objects are
I still don't think anyone understood what I was trying to do with this, and it's one of the main reasons I stopped working on this project. I was trying to figure out where to store information about what actually happens when you collide with an Object/Item (Collision Response), not handling the actual collision detection. I still thank everyone for trying to help out. ;)

To answer you question, it's essentially the same thing, but you're checking actual screen coordinates instead of tile positions. So rather than

Code: Select all

if( character.x > tile.x ) // where character.x is something like 4 and tile.x is 2
{
     return true;
}
It would be more like this

Code: Select all

if( character.x > (tile.x * tile.w) ) // where character.x is something like 143 and tile.x * tile.w is 64
{
     return true;
}
Does that help at all?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: How To Handle Object/Item Collision

Post by thejahooli »

I know how I would do the collision detection. What I'm asking is how would you tell which objects to check for collision so that you don't have to loop through all of them.
I'll make your software hardware.
Post Reply