Managing Entities in Regard to Collision Detection/Handling

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Managing Entities in Regard to Collision Detection/Handling

Post 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.
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: Managing Entities in Regard to Collision Detection/Handling

Post 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,...'
Follow me on twitter!
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Managing Entities in Regard to Collision Detection/Handling

Post 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
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: Managing Entities in Regard to Collision Detection/Handling

Post 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!
Follow me on twitter!
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: Managing Entities in Regard to Collision Detection/Handling

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: Managing Entities in Regard to Collision Detection/Handling

Post 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 )
Follow me on twitter!
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Managing Entities in Regard to Collision Detection/Handling

Post 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
Post Reply