Testing collisions between entities

Started by
3 comments, last by nhold 12 years, 6 months ago
Hi, In my game, I add all entities to an entity list. I have numerous different classes inheriting from Entity. e.g. Character,Item,Weapon

My question is, how do people go about checking collisions for all the entities? If, for example,


  • there are 5 enemy characters on screen, each of those will have to check collisions with the floo
  • Player character is also on screen and must do the same
  • then Any items that are in the level must also do the same
  • Then somehow I must check collisions between all these:- Human&Item, Human & enemy weapon/bullet, Human weapon/Bullet & enemy
I don't really know how to go about it...was my design to make a list of entities a bad idea?
How could I make a new "list" or "array" which holds every entities position so I can then only check collisions at their specific area? Will that work?
Advertisement
You really only have to check collision for mobile objects if your game is a game and not an editor of sorts. You could make a separate data structure as an attribute to any mobile object. This structure would include collision items that are within a certain distance of said mobile object, and then you can only check collision on the objects that are added to that structure, instead of going into some ridiculous triple nested for loop or something.... just an idea.
so like, Say for the human player...He would have a structure containing a list of positions of all powerups in the level? Or Would I give the Item class a handle to the player object?so It can check for collisions if the player is nearing its position?

so like, Say for the human player...He would have a structure containing a list of positions of all powerups in the level? Or Would I give the Item class a handle to the player object?so It can check for collisions if the player is nearing its position?


You could do either, but keep in mind that actors will still need to know about other actors in their vicinity... which is why i suggested only giving mobile objects this data structure.

You would probably want to add and remove objects from this data structure in an update method somewhere to keep it small and efficient for iterations. If you already have a loop somewhere iterating over all entities in your game, you could use that, if it is in an appropriate place, to check to see if collision objects are near, then add or remove as necessary.

At the end of the day though, I'm still a beginner in the grand scheme of things here, and there's probably a better solution than what I'm explaining, though it works very well for me...
Hey ravinDavin,

You may want to look at Box2D, it handles collisions very nicely.

If you don't want to do that, here is a simple way:

When you create an entity, also create a PhysicsObject and give it to the PhysicsSystem.

  • A PhysicsObject contains a Shape reference.
  • A Shape is something that defines the shape of the collision (Shape, Circle, Polygon) and holds the data for it.
  • A PhysicsObject contains a Position reference.
  • A PhysicsObject contains the entity that it is used for.
  • A PhysicsObject contains a vector of PhysicsObjects, which is cleared when GetCollisions is called, which returns this vector.
  • May also want to make physicsobjects that are just triggers (So a bool isTrigger so it won't act solid but still report it's collision)

  • A PhysicsSystem contains a vector of PhysicsObjects
  • A PhysicsSystem has an update function, which checks all PhysicsObjects for collisions and places PhysicsObjects into other PhysicsObjects if a collision occured.
  • You could even do collision response with this

An entity could then check what entity is attached to the physicsobject by using a name or reinterpret_cast or something and do something based on that. You could also make it so physicsobjects can contain multiple shapes so you can define stuff using simple shapes.

EDIT: You should have a list of entities somewhere, because you will most probably need to call an update function on them to do any logic that they contain (Like scale up and down or something). But your game logic (entity update function) shouldn't really be apart of your physics, your physics or collisions should 'just happen'. (IMO)

Engineering Manager at Deloitte Australia

This topic is closed to new replies.

Advertisement