2
\$\begingroup\$

I'm planning to use a quadtree for optimizing collision detection. But this quadtree can handle only one type of objects (so I used a base class for my objects).

If I use double dispatch after two object pointers are returned by the quadtree in a collision (derived from the same base class that the quadtree can handle) knowing their ID that identifies their type (an int in the base class) , how am I able to use these pointers with their respective type in order to double dispatch?

A LIMITED SOLUTION:

Before double dispatching, I could make a resolver function that combines both IDs as they were flags into a single parameter, and make a switch that decides which type are obj1 and obj2.

However, its still not very elegant. Any solution or better way to do this? thanks.

\$\endgroup\$
  • \$\begingroup\$ Why can your tree only have one type of object? Maybe it would be best to only insert OBBs or AABBs into the quad tree. Each collision shape can calculate it's AABB and give it to the quad tree. The quad tree can in return give an ID for the AABB node in the tree itself to the shape that inserted it's AABB. \$\endgroup\$ – RandyGaul Sep 13 '14 at 19:03
  • \$\begingroup\$ Look into double dispatch: en.wikipedia.org/wiki/Double_dispatch \$\endgroup\$ – JarkkoL Sep 13 '14 at 19:04
2
\$\begingroup\$

It depends how you implement double dispatching how elegant it is. You are effectively trying to dispatch to a function that's able to handle collision between types X and Y and execute function for that type pair. This is basically a matrix lookup and you could implement it by registering collision functions to this matrix.

Let say your type-ID is 0-based number. You can allocate a 2D array of function pointers of type bool(const void*, const void*) and register functions to the array look-up based on the type-ID's. Because this matrix/array is symmetric about the diagonal (e.g. testing OOBB vs Capsule is the same as Capsule vs OOBB with the parameters swapped), you can register only the lower-triangle of this matrix. At the dispatch you check which type-ID is smaller and set that as your first argument to cut the collision test functions in half. Then fetch the collision test function from the matrix using the type ID's as indices to the matrix.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.