Detect collision object in visual object

Post Reply
physics_boii
Posts: 3
Joined: Wed Oct 30, 2019 9:28 pm

Detect collision object in visual object

Post by physics_boii » Fri Dec 06, 2019 10:57 pm

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?

User avatar
drleviathan
Posts: 567
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Detect collision object in visual object

Post by drleviathan » Sat Dec 07, 2019 4:06 pm

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.

physics_boii
Posts: 3
Joined: Wed Oct 30, 2019 9:28 pm

Re: Detect collision object in visual object

Post by physics_boii » Tue Dec 10, 2019 5:17 pm

Awesome, thanks for the reply! Would this be portable to python in some way as well?

User avatar
drleviathan
Posts: 567
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Detect collision object in visual object

Post by drleviathan » Wed Dec 11, 2019 5:37 pm

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.

Post Reply