39 void onTrigger(physx::PxTriggerPair *pairs, physx::PxU32 count)
override
48 const auto replacesLostOverlap = [pairs, count](physx::PxU32 i) {
49 if (pairs[i].status != physx::PxPairFlag::eNOTIFY_TOUCH_FOUND)
51 for (physx::PxU32 j = 0; j < count; j++) {
52 if (pairs[j].status == physx::PxPairFlag::eNOTIFY_TOUCH_LOST
53 && pairs[j].triggerActor == pairs[i].triggerActor
54 && pairs[j].otherActor == pairs[i].otherActor) {
64 enum Pass { ReplacedOverlaps, EveryPair };
65 for (
const Pass pass : { ReplacedOverlaps, EveryPair }) {
66 for (physx::PxU32 i = 0; i < count; i++) {
67 if (pass == ReplacedOverlaps && !replacesLostOverlap(i))
72 & (physx::PxTriggerPairFlag::eREMOVED_SHAPE_TRIGGER
73 | physx::PxTriggerPairFlag::eREMOVED_SHAPE_OTHER))
78 auto *triggerCandidate =
79 static_cast<QAbstractPhysicsNode *>(pairs[i].triggerActor->userData);
81 QAbstractPhysicsNode *otherNode =
82 static_cast<QAbstractPhysicsNode *>(pairs[i].otherActor->userData);
84 if (!triggerCandidate || !otherNode) {
85 qWarning() <<
"QtQuick3DPhysics internal error: null pointer in trigger "
94 if (world->isNodeRemoved(triggerCandidate) || world->isNodeRemoved(otherNode))
98 auto *triggerNode = qobject_cast<QAbstractTriggerRegion *>(triggerCandidate);
106 const QAbstractTriggerRegion::ShapePair shapes {
107 frontendShape(pairs[i].triggerShape), frontendShape(pairs[i].otherShape)
114 if (pairs[i].status == physx::PxPairFlag::eNOTIFY_TOUCH_FOUND)
115 triggerNode->registerCollision(otherNode, shapes);
116 else if (pairs[i].status == physx::PxPairFlag::eNOTIFY_TOUCH_LOST)
117 triggerNode->deregisterCollision(otherNode, shapes);
126 void onContact(
const physx::PxContactPairHeader &pairHeader,
const physx::PxContactPair *pairs,
127 physx::PxU32 nbPairs)
override
129 constexpr physx::PxU32 bufferSize = 64;
130 physx::PxContactPairPoint contacts[bufferSize];
132 for (physx::PxU32 i = 0; i < nbPairs; i++) {
133 const physx::PxContactPair &contactPair = pairs[i];
135 if (contactPair.events & physx::PxPairFlag::eNOTIFY_TOUCH_FOUND) {
136 QAbstractPhysicsNode *trigger =
137 static_cast<QAbstractPhysicsNode *>(pairHeader.actors[0]->userData);
138 QAbstractPhysicsNode *other =
139 static_cast<QAbstractPhysicsNode *>(pairHeader.actors[1]->userData);
141 if (!trigger || !other || world->isNodeRemoved(trigger)
142 || world->isNodeRemoved(other) || !trigger->m_backendObject
143 || !other->m_backendObject)
146 const bool triggerReceive =
147 trigger->receiveContactReports() && other->sendContactReports();
148 const bool otherReceive =
149 other->receiveContactReports() && trigger->sendContactReports();
151 if (!triggerReceive && !otherReceive)
154 physx::PxU32 nbContacts = pairs[i].extractContacts(contacts, bufferSize);
156 QList<QVector3D> positions;
157 QList<QVector3D> impulses;
158 QList<QVector3D> normals;
160 positions.reserve(nbContacts);
161 impulses.reserve(nbContacts);
162 normals.reserve(nbContacts);
164 for (physx::PxU32 j = 0; j < nbContacts; j++) {
165 physx::PxVec3 position = contacts[j].position;
166 physx::PxVec3 impulse = contacts[j].impulse;
167 physx::PxVec3 normal = contacts[j].normal;
169 positions.push_back(QPhysicsUtils::toQtType(position));
170 impulses.push_back(QPhysicsUtils::toQtType(impulse));
171 normals.push_back(QPhysicsUtils::toQtType(normal));
174 QList<QVector3D> normalsInverted;
175 normalsInverted.reserve(normals.size());
176 for (
const QVector3D &v : std::as_const(normals)) {
177 normalsInverted.push_back(QVector3D(-v.x(), -v.y(), -v.z()));
181 world->registerContact(other, trigger, positions, impulses, normals);
183 world->registerContact(trigger, other, positions, impulses, normalsInverted);