Page 1 of 1
Detect collision object in visual object
Posted: Fri Dec 06, 2019 10:57 pm
by physics_boii
I'd like to be able to detect when a collision object has passed into a certain box/space (a visual object would be perfect for this), is there any way that this can be implemented?
Re: Detect collision object in visual object
Posted: Sat Dec 07, 2019 4:06 pm
by drleviathan
Here is one way to do it:
(1) Derive a class from
btGhostObject and copy the base
addOverlappingObjectInternal() method into a modified override like this:
Code: Select all
class TriggerObject : puclic btGhostObject {
public:
void btGhostObject::addOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btBroadphaseProxy* thisProxy) override
{
btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
btAssert(otherObject);
///if this linearSearch becomes too slow (too many overlapping objects) we should add a more appropriate data structure
int index = m_overlappingObjects.findLinearSearch(otherObject);
if (index == m_overlappingObjects.size())
{
//not found
m_overlappingObjects.push_back(otherObject);
// BEGIN CUSTOM CODE
std::cout << (void*)(this) << " TriggerObject: add overlap " << (void*)(otherObject) << std::endl;
// END CUSTOM CODE
}
}
};
(2) Add a
btGhostPairCallback to the
btDynamicsWorld to enable the optional feature
Code: Select all
dynamicsWorld->getPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
(3) Add your
TriggerObject instances to the world where necessary.
Note: by default the
btGhostObject has no "shape" and adds happen on AABB overlap, as tracked by the broad-phase. To get more detailed shape overlap you would have to examine the implementation of
btGhostPairCallback and modify its
addOverlappingPair() method to perform a narrow-phase check before adding.
Re: Detect collision object in visual object
Posted: Tue Dec 10, 2019 5:17 pm
by physics_boii
Awesome, thanks for the reply! Would this be portable to python in some way as well?
Re: Detect collision object in visual object
Posted: Wed Dec 11, 2019 5:37 pm
by drleviathan
I don't know how the python stuff works. I think it uses the CAPI under the hood and if so: that would probably have to be enhanced to get your custom feature exposed. However, perhaps there is already an event you can handle in python when a "ghost overlap add" happens, dunno.