Page 1 of 1

Managing Entities in Regard to Collision Detection/Handling

Posted: Tue Aug 17, 2010 11:32 pm
by XianForce
Well, I'm currently doing a lot of code refactoring, and one of the things I think I really need to hit on is Entity Management. Really, I just want to hear how you guys manage your entities (with collision handling/detection specifically in mind).

Do you have a single container to hold all your entities? Do you separate Entities by their types? Etc?

I'm just looking for more ideas here :D, thanks for any response :p.

Re: Managing Entities in Regard to Collision Detection/Handling

Posted: Wed Aug 18, 2010 3:31 am
by Milch
I'm not sure if this is an actual entity system I'm using, but anyway.

I have a base class for entities 'Base_Entity' and all entities inherit from this class.
The collision system works like this:
Each entity can have n collision objects, and all these collision objects are registered in a collision manager.
This way I can change the implementation of the collision manager pretty fast.
The collision manager now 'thinks' and calls the 'Touch' function of the both colliding entities, with a pointer to the other entity as a parameter.
( e.g like this 'void Touch( Base_Entity* other )' )
I'm thinking about handling over the pointer of the collision-object that triggered the event - so I can have something like 'hitzones,...'

Re: Managing Entities in Regard to Collision Detection/Handling

Posted: Wed Aug 18, 2010 9:46 am
by XianForce
Milch wrote:I'm not sure if this is an actual entity system I'm using, but anyway.

I have a base class for entities 'Base_Entity' and all entities inherit from this class.
The collision system works like this:
Each entity can have n collision objects, and all these collision objects are registered in a collision manager.
This way I can change the implementation of the collision manager pretty fast.
The collision manager now 'thinks' and calls the 'Touch' function of the both colliding entities, with a pointer to the other entity as a parameter.
( e.g like this 'void Touch( Base_Entity* other )' )
I'm thinking about handling over the pointer of the collision-object that triggered the event - so I can have something like 'hitzones,...'

Well, I'm not really talking about that... I'm more along the lines of storage of entities :p

Re: Managing Entities in Regard to Collision Detection/Handling

Posted: Wed Aug 18, 2010 11:02 am
by Milch
I just have a big list of entities, because everything in my game engine is an entity ( even the game menu, intro screen,... ), but I think it depends on what kind of entity system you have ( e.g Just map objects or simply everything )
The only important hint that I can give you is use a singly linked list - because most of the time a doubly linked list eats up to much memory and isnt worth it!

Re: Managing Entities in Regard to Collision Detection/Handling

Posted: Wed Aug 18, 2010 12:55 pm
by dandymcgee
I definitely recommend you separate them at least into collision groups. Checking collision of every entity against every entity is never going to be beneficial (what's the last game you played where a floor tile collided with the game menu?). Even with a quad tree it's still wasteful to have an unsorted glob of entities in a giant list.

Separating them into basic lists by type would be a start:

Code: Select all

std::vector<Entity*> floorTiles;
std::vector<Entity*> wallTiles;
std::vector<Entity*> items;
std::vector<Entity*> chars;
std::vector<Entity*> warps;
std::vector<Entity*> menus;
Entity* mouse;  //This probably isn't how you handle input, but you did mention menus being entities.

//Check Character against Floor, Wall, Item, Warp

//Check Item against Floor, Wall (If item has physics)

//Check Mouse against Menu, Item (Pick up items), Character (NPCs), Floor/Tile/Warp (in edit mode)
Obviously the types and interaction checks would be customized to suit your game.

Re: Managing Entities in Regard to Collision Detection/Handling

Posted: Wed Aug 18, 2010 1:28 pm
by Milch
Not each entity has a collision box! That wouldn't be very efficient!
Each entity has the ability to register collision-objects ( e.g RegisterObject() )
So a entity can have n-objects ( like I said before, hitzones )

Re: Managing Entities in Regard to Collision Detection/Handling

Posted: Wed Aug 18, 2010 2:49 pm
by XianForce
dandymcgee wrote:I definitely recommend you separate them at least into collision groups. Checking collision of every entity against every entity is never going to be beneficial (what's the last game you played where a floor tile collided with the game menu?). Even with a quad tree it's still wasteful to have an unsorted glob of entities in a giant list.

Separating them into basic lists by type would be a start:

Code: Select all

std::vector<Entity*> floorTiles;
std::vector<Entity*> wallTiles;
std::vector<Entity*> items;
std::vector<Entity*> chars;
std::vector<Entity*> warps;
std::vector<Entity*> menus;
Entity* mouse;  //This probably isn't how you handle input, but you did mention menus being entities.

//Check Character against Floor, Wall, Item, Warp

//Check Item against Floor, Wall (If item has physics)

//Check Mouse against Menu, Item (Pick up items), Character (NPCs), Floor/Tile/Warp (in edit mode)
Obviously the types and interaction checks would be customized to suit your game.
This is what I was looking for. Alright, I think what I'll probably do is have my Entity Manager contain a list for each type of entity, and then have it check for collisions every frame, and if a collision happens, I'll add the information regarding that collision to a queue, where they can later be handled by a CollisionManager :p