How To Handle Object/Item Collision
Moderator: Coders of Rage
- dandymcgee
- 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
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.
"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!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: How To Handle Objects/Items?
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" :)
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.
- dandymcgee
- 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?
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.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"
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: How To Handle Object/Item Collision
tilesystem.h
tilesystem.cpp
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...
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
class TileSystem {
Tile tiles[10][10];
public:
bool hasObject(int x, int y);
};
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);
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);
}
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.
- dandymcgee
- 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
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!
- Ginto8
- 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
http://lazyfoo.net/SDL_tutorials/lesson17/index.php
just gonna put that out there
just gonna put that out there
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: How To Handle Object/Item Collision
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.
Re: How To Handle Object/Item Collision
heheh, rofltaco... 12-year-old... lolMarauderIIC 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.
- MadPumpkin
- 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
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
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
For God so loved the world that he blessed the thugs with rock
- dandymcgee
- 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
Thanks, but basic rectangular collision has absolutely nothing to do with how to manage object interaction...Ginto8 wrote:http://lazyfoo.net/SDL_tutorials/lesson17/index.php
just gonna put that out there
(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!
-
- Chaos Rift Newbie
- Posts: 30
- Joined: Sat Apr 18, 2009 8:17 pm
Re: How To Handle Object/Item Collision
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?
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
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: How To Handle Object/Item Collision
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.
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: How To Handle Object/Item Collision
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.
- dandymcgee
- 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
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.thejahooli wrote:do you know how you would do this if the player is not tile aligned but the objects are
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;
}
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;
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: How To Handle Object/Item Collision
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.