Is it wise to check all objects in a given state for collision every cycle?
#1 Members - Reputation: 157
Posted 25 June 2012 - 12:39 PM
I am using XNA 4.0
thanks in advance,
Dartos
#2 Members - Reputation: 1050
Posted 25 June 2012 - 12:43 PM
Only way I can think of doing it is declaring a counter variable. Each update increment the counter by 1 until it hits lets say 5 where we rest it back to 0. Certain objects absolutely required to be updated such as player and bullets you update every time the update function is called. Other objects such as crates only update if count = 1, enemies might only be updates on counts 2 and 4. Backgrounds on count 0.
I've not put this into practise yet so someone with more XNA experience than me might want to say yay or nay to that plan.
#3 Members - Reputation: 3710
Posted 25 June 2012 - 12:45 PM
Store your objects in a tree structure based on location and you can avoid even looking at objects that are far away.
For 2D games a quadtree tends to be fairly efficient (it also works well for 3D games if the world is reasonably flat) , you could also look at octrees or bsp trees.
Edited by SimonForsman, 25 June 2012 - 12:47 PM.
The voices in my head may not be real, but they have some good ideas!
#5 Moderators - Reputation: 7563
Posted 25 June 2012 - 12:53 PM
- Quadtrees
- Octrees
- BSP trees
There are also KD-trees which might be useful depending on your game. Wikipedia has in-depth explanations of all of them :-)
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#6 Members - Reputation: 127
Posted 25 June 2012 - 01:06 PM
6677's suggestion to check for collision on every Nth update is very risky. If you check for a collision on a bullet on every 5th update for example, then the bullet could have passed through a very thin wall in the time between every 5th update.
#7 Members - Reputation: 157
Posted 25 June 2012 - 01:14 PM
Which brings me to this, I looked up those tree types on google but I can't find a good explination on how they can be applied to collision detection. Can anyone give me an example?
#8 Moderators - Reputation: 7563
Posted 25 June 2012 - 03:41 PM
You divide the world up into chunks, based on whatever tree type you select. Then you look at which "chunk" each object is inside of. If you have a bullet in chunk A and a player in chunk B, and you know (based on the tree's properties) that chunks A and B can never overlap, then you don't have to do collision detection between the bullet and the player.
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#9 Members - Reputation: 206
Posted 25 June 2012 - 03:52 PM
A slightly more complex optimization is to deactivate (Bullet library calls this sleeping IIRC) objects which haven't collided with anything for some time. This technique has some subtleties, because you need to be able to detect when to wake an object (if, say, a character is approaching a stack of sleeping boxes).
Another method of culling collisions is to cull based on dynamic bounding regions. For example, say I have a flock of birds that always tend to be close together, rather than check an airplane against each bird, I will check the airplane against a bounding sphere that contains all the birds, and only if it passes I'll check individual collisions.
Collision libraries (like Bullet) generally use all of these methods together. Basically, though, I'm just saying there are many valid methods to skip certain collision checks (sometimes called broad phase collision checking) so don't settle on one that is going to give you fits unless you're sure it's necessary.
Edited by bimmy, 25 June 2012 - 03:53 PM.
#10 Members - Reputation: 1050
Posted 25 June 2012 - 04:06 PM
Thanks, I won't be using that then.6677's suggestion to check for collision on every Nth update is very risky. If you check for a collision on a bullet on every 5th update for example, then the bullet could have passed through a very thin wall in the time between every 5th update.
Might be somewhere else its handy but not here then and not somewhere I can think yet.
Edited by 6677, 25 June 2012 - 04:08 PM.
#11 Crossbones+ - Reputation: 3304
Posted 25 June 2012 - 04:16 PM
Beginner in Game Development? Read here.
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts ![]()
#13 GDNet+ - Reputation: 189
Posted 25 June 2012 - 11:58 PM
Then test which node your bullet is in and only check the collidable objects within that node.
That is the basis of a quad tree. For an Oct tree split the nodes by 4 instead of 2. Here is a link utilizing quad trees for terrain rendering. Its pretty much the same concept just replace vertices with enemies. http://www.rastertek.com/tertut05.html The code is in c++ but you shouldn't have difficulties making the change to c#.
#14 Members - Reputation: 1560
Posted 26 June 2012 - 09:06 AM
The idea would be to place your objects into each grids collide-list (a Vector of pointing to the objects), depending on which one it's in. Every time you move an object, check if it's passed a grid boundary, and remove from old list and place it in the proper grid list. Occasionally, an object will span 2 grids, and it will have to be in 2 lists. Then, each update, you compare only the objects with other objects in the same list.
That's kind of a conceptual way of doing it. Using trees will be much more efficient, but more difficult to implement in the beginning.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#15 Members - Reputation: 294
Posted 27 June 2012 - 05:09 PM
How many objects would your current project have? if you know the answer, chances are you won't be needing any kind of optimization, if the answer is in the thousands or undefined, then we are in business.
As a general rule, unless you are making an educational proyect, unnecessary optimizations are undesirable. If you already have the optimization algorithm at hand you might as well use it, if you want to learn it, do it, otherwise you may be headed into one of the biggest time wastes in development.
If you need to optimize your collision detection you'll probably need to apply a couple of techniques, as others said, tree like structures are one of the most popular methods, they are different ways of applying the same technique, which is space partitioning, breaking down a large, possibly unlimited space into smaller parts where you can fit stuff in to get a certain knowledge of proximity.
If your game had rooms for example, it would be pointless to test for collision between two objects that you know are in to different rooms. That is a space partitioning, you define that rooms are separate spaces and objects cannot be in two of them at the same time.
Another good optimization is avoid checking collisions between static objects, since they usually make up for most of the population of a world. If two objects are truly static, even if they are in collision from the beginning, that is likely as designed by the level editor. If objects are subject to a physics engine, but do not move on their own, its good to flag them as being in static condition when they have found a resting spot. If your objects are mobile those are the ones you need to check all the time.
You can always dig deeper and there is no "best" optimization, they are all good for some cases and bad for others, and as I mentioned before, the cost of research and implementation HAS to be taken into consideration, even if it is the BEST optimization ever.
LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272
#16 Members - Reputation: 157
Posted 01 July 2012 - 09:25 PM
Then test which node your bullet is in and only check the collidable objects within that node.
How do we know when to stop dividing and check for collision? If there is only one object in the node than nothing is colliding. Also what if an object is in more than one node at once?
#17 Moderators - Reputation: 7563
Posted 01 July 2012 - 11:18 PM
As far as objects which overlap multiple nodes: well, you simply check all the nodes it overlaps. Ideally this is a worst-case scenario so you're still saving a lot of calculation over checking everything in the world, and your average case should still be only checking 1-2 nodes tops.
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#18 Members - Reputation: 344
Posted 02 July 2012 - 02:35 AM
And stayed with grid, becouse of performance, simplier implementation(no recursian), much eficient space dividing (that means better collision checking filter), much faster object inserting to algoritm data list,easie getting result lists for colliding, and so on.
infectedbrain if you want i can share c++ code by email. its very simple, its not perfect and optimized, but it works for me great ...
Edited by serumas, 02 July 2012 - 02:37 AM.






