From fbff3627dcadc06532c91ee370d6d8ae34045498 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 20 Apr 2015 11:13:51 -0700 Subject: [PATCH] attempt to claim simulation ownership of entities the avatar crashes with --- libraries/physics/src/EntityMotionState.cpp | 2 ++ libraries/physics/src/PhysicsEngine.cpp | 37 ++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 95a3dff6af..37472695c6 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -305,6 +305,8 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, uint32_ qCDebug(physics) << "EntityMotionState::sendUpdate()... calling queueEditEntityMessage()..."; #endif + qCDebug(physics) << "EntityMotionState::sendUpdate()"; + entityPacketSender->queueEditEntityMessage(PacketTypeEntityAddOrEdit, id, properties); } else { #ifdef WANT_DEBUG diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index aa9921be64..af4349a66e 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -23,8 +23,9 @@ uint32_t PhysicsEngine::getNumSubsteps() { return _numSubsteps; } -PhysicsEngine::PhysicsEngine(const glm::vec3& offset) - : _originOffset(offset) { +PhysicsEngine::PhysicsEngine(const glm::vec3& offset) : + _originOffset(offset), + _characterController(NULL) { } PhysicsEngine::~PhysicsEngine() { @@ -367,6 +368,12 @@ void PhysicsEngine::stepNonPhysicalKinematics(const quint64& now) { void PhysicsEngine::computeCollisionEvents() { BT_PROFILE("computeCollisionEvents"); + + const btCollisionObject* characterCollisionObject = + _characterController ? _characterController->getCollisionObject() : NULL; + auto nodeList = DependencyManager::get(); + const QUuid& myNodeID = nodeList->getSessionUUID(); + // update all contacts every frame int numManifolds = _collisionDispatcher->getNumManifolds(); for (int i = 0; i < numManifolds; ++i) { @@ -382,12 +389,34 @@ void PhysicsEngine::computeCollisionEvents() { // which will eventually trigger a CONTACT_EVENT_TYPE_END continue; } - + void* a = objectA->getUserPointer(); void* b = objectB->getUserPointer(); if (a || b) { // the manifold has up to 4 distinct points, but only extract info from the first - _contactMap[ContactKey(a, b)].update(_numContactFrames, contactManifold->getContactPoint(0), _originOffset); + const ContactKey& contactKey = ContactKey(a, b); + ContactInfo& contactInfo = _contactMap[contactKey]; + contactInfo.update(_numContactFrames, contactManifold->getContactPoint(0), _originOffset); + + // if our character capsule is colliding with something dynamic, claim simulation ownership. + // do this by setting the entity's simulator-id to NULL, thereby causing EntityMotionState::sendUpdate + // to set ownership to us. + if (objectA == characterCollisionObject && !objectB->isStaticOrKinematicObject() && b) { + ObjectMotionState* B = static_cast(b); + EntityItem* entityB = static_cast(B)->getEntity(); + if (entityB->getSimulatorID() != myNodeID) { + qDebug() << "CLAIMING B"; + entityB->setSimulatorID(NULL); + } + } + if (objectB == characterCollisionObject && !objectA->isStaticOrKinematicObject() && a) { + ObjectMotionState* A = static_cast(a); + EntityItem* entityA = static_cast(A)->getEntity(); + if (entityA->getSimulatorID() != myNodeID) { + qDebug() << "CLAIMING A"; + entityA->setSimulatorID(NULL); + } + } } } }