I am making a collision detection system using a Binary Space Partitioning Tree. I have two kinds of objects :
- Dynamic objects (characters, projectiles) whose collisions with other objects need to be checked,
- Static objects (like walls) which are not being tested for collision among each other but can only be tested against a dynamic object.
For that, I am using two data structures : a list for storing all my dynamic objects, and the BSP tree which contains every objects (dynamic or static). Therefore every dynamic object are stored in both structures.
Finally, I am performing my collision detection by looping over my list and using the tree to test each object like so :
foreach(DynamicObject dynobj in myListOfDynamicObjects)
{
//check the collision against every close dynamic or static objects
myBSPtree.CheckCollision(dynobj);
}
However, at this point, I didn't think about something : every collision check between two dynamic objects is made twice. For example :
\ List of dynamic objects : {dynA, dynB} Tree : dynA / \ static1 dynB
Combinations tested :
dynA - dynA(useless case handled)- dynA - static1
- dynA - dynB
- dynB - dynA (same as above)
- dynB - static1
dynB - dynB
In order to skip a useless check, I thought of adding an attribute Order on each object : it would be a unique identifier given at the construction of the object and use like so :
if(dynA.Order < dynB.Order){ CheckCollision(dynA,dynB); }
That way, only one collision check is made for each combination of object.
My question is : if this technique is correct (I mean is it a good design ?), and since every object have a unique reference in C#, can I directly compare their reference like so : (?)
if(dynA.Reference < dynB.Reference){ CheckCollision(dynA,dynB); }
We can use object.ReferenceEquals to see if two references are equals but can we make an actual comparison of these references ?