Page 1 of 2
How To Handle Object/Item Collision
Posted: Wed Apr 29, 2009 6:01 pm
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.
Re: How To Handle Objects/Items?
Posted: Wed Apr 29, 2009 6:05 pm
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" :)
Re: How To Handle Objects/Items?
Posted: Wed Apr 29, 2009 6:15 pm
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.
Re: How To Handle Object/Item Collision
Posted: Wed Apr 29, 2009 6:53 pm
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);
}
Re: How To Handle Object/Item Collision
Posted: Wed Apr 29, 2009 7:41 pm
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.
Re: How To Handle Object/Item Collision
Posted: Wed Apr 29, 2009 8:04 pm
by Ginto8
Re: How To Handle Object/Item Collision
Posted: Wed Apr 29, 2009 8:08 pm
by MarauderIIC
lol.. with your sig and all,
Re: How To Handle Object/Item Collision
Posted: Wed Apr 29, 2009 8:35 pm
by eatcomics
MarauderIIC wrote:lol.. with your sig and all,
heheh, rofltaco... 12-year-old... lol
Re: How To Handle Object/Item Collision
Posted: Wed Apr 29, 2009 10:49 pm
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
Re: How To Handle Object/Item Collision
Posted: Thu Apr 30, 2009 2:14 pm
by dandymcgee
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.
Re: How To Handle Object/Item Collision
Posted: Fri May 01, 2009 8:16 pm
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?
Re: How To Handle Object/Item Collision
Posted: Fri May 01, 2009 11:42 pm
by MarauderIIC
Can be slow, you're checking against every item. If you can narrow it down to nearby items (as above)...
Re: How To Handle Object/Item Collision
Posted: Wed Jun 10, 2009 12:51 pm
by thejahooli
do you know how you would do this if the player is not tile aligned but the objects are
Re: How To Handle Object/Item Collision
Posted: Wed Jun 10, 2009 2:07 pm
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?
Re: How To Handle Object/Item Collision
Posted: Wed Jun 10, 2009 3:52 pm
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.