From ed1761945c64d16fb3a42c009c5eeb1c75b3015e Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 6 Apr 2018 07:22:05 -0700 Subject: [PATCH] add/remove entities to physics using workload output --- interface/src/Application.cpp | 1 + interface/src/workload/PhysicsBoundary.h | 56 ++++--------------- .../src/EntityTreeRenderer.h | 2 +- libraries/physics/CMakeLists.txt | 2 +- .../physics/src/PhysicalEntitySimulation.cpp | 6 +- .../physics/src/PhysicalEntitySimulation.h | 3 + libraries/physics/src/PhysicsEngine.cpp | 2 + libraries/workload/src/workload/Proxy.h | 7 --- 8 files changed, 22 insertions(+), 57 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 1b85563969..1105e7461c 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -4705,6 +4705,7 @@ void Application::init() { }, Qt::QueuedConnection); _gameWorkload.startup(getEntities()->getWorkloadSpace(), _main3DScene, _entitySimulation); + _entitySimulation->setWorkloadSpace(getEntities()->getWorkloadSpace()); } void Application::updateLOD(float deltaTime) const { diff --git a/interface/src/workload/PhysicsBoundary.h b/interface/src/workload/PhysicsBoundary.h index 4109418e27..7eb3644375 100644 --- a/interface/src/workload/PhysicsBoundary.h +++ b/interface/src/workload/PhysicsBoundary.h @@ -15,19 +15,6 @@ #include "render/Scene.h" -/* -class FooJobConfig : public workload::Job::Config { - Q_OBJECT - Q_PROPERTY(bool fubar MEMBER fubar NOTIFY dirty) -public: - bool fubar{ false }; -signals: - void dirty(); -protected: -}; -*/ - -#include // adebug #include #include #include "PhysicalEntitySimulation.h" @@ -45,43 +32,20 @@ public: } void run(const workload::WorkloadContextPointer& context, const Inputs& inputs) { - const auto& regionChanges = inputs.get1(); auto space = context->_space; if (!space) { return; } - uint32_t listSize = (uint32_t)regionChanges.size(); - uint32_t totalTransitions = 0; - for (uint32_t i = 0; i < listSize; ++i) { - totalTransitions += (uint32_t)regionChanges[i].size(); - } - // we're interested in things entering/leaving R3 - uint32_t regionIndex = workload::Region::R3; - uint32_t exitIndex = 2 * regionIndex; - uint32_t numExits = (uint32_t)regionChanges[exitIndex].size(); - for (uint32_t i = 0; i < numExits; ++i) { - int32_t proxyID = regionChanges[exitIndex][i]; - auto owner = space->getOwner(proxyID).get(); - if (owner) { - //EntityItem* entity = static_cast(owner); - std::cout << "adebug - " - //<< owner - << " '" << owner->getName().toStdString() << "'" - << std::endl; // adebug - } - } - - uint32_t enterIndex = exitIndex + 1; - uint32_t numEntries = (uint32_t)regionChanges[enterIndex].size(); - for (uint32_t i = 0; i < numEntries; ++i) { - int32_t proxyID = regionChanges[enterIndex][i]; - auto owner = space->getOwner(proxyID).get(); - if (owner) { - // EntityItem* entity = static_cast(owner); - std::cout << "adebug + " - //<< owner - << " '" << owner->getName().toStdString() << "'" - << std::endl; // adebug + GameWorkloadContext* gameContext = static_cast(context.get()); + PhysicalEntitySimulationPointer simulation = gameContext->_simulation; + const auto& regionChanges = inputs.get0(); + for (uint32_t i = 0; i < (uint32_t)regionChanges.size(); ++i) { + const workload::Space::Change& change = regionChanges[i]; + auto entity = space->getOwner(change.proxyId).get(); + if (entity) { + simulation->changeEntity(entity); + qCDebug("physics") << change.proxyId << " : " << "'" << entity->getName() << "' " + << (uint32_t)(change.prevRegion) << " --> " << (uint32_t)(change.region); } } } diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.h b/libraries/entities-renderer/src/EntityTreeRenderer.h index fa9c2ac6d2..a425ad4676 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.h +++ b/libraries/entities-renderer/src/EntityTreeRenderer.h @@ -121,7 +121,7 @@ public: static bool shouldRenderDebugHulls() { return _renderDebugHullsOperator(); } // Access the workload Space - const workload::SpacePointer getWorkloadSpace() const { return _space; } + workload::SpacePointer getWorkloadSpace() const { return _space; } signals: void enterEntity(const EntityItemID& entityItemID); diff --git a/libraries/physics/CMakeLists.txt b/libraries/physics/CMakeLists.txt index ad082c1a6e..96a55c8477 100644 --- a/libraries/physics/CMakeLists.txt +++ b/libraries/physics/CMakeLists.txt @@ -1,6 +1,6 @@ set(TARGET_NAME physics) setup_hifi_library() -link_hifi_libraries(shared fbx entities graphics) +link_hifi_libraries(shared task workload fbx entities graphics) include_hifi_library_headers(networking) include_hifi_library_headers(gpu) include_hifi_library_headers(avatars) diff --git a/libraries/physics/src/PhysicalEntitySimulation.cpp b/libraries/physics/src/PhysicalEntitySimulation.cpp index d799577fc2..f05f5a94f5 100644 --- a/libraries/physics/src/PhysicalEntitySimulation.cpp +++ b/libraries/physics/src/PhysicalEntitySimulation.cpp @@ -120,8 +120,10 @@ void PhysicalEntitySimulation::changeEntityInternal(EntityItemPointer entity) { QMutexLocker lock(&_mutex); assert(entity); EntityMotionState* motionState = static_cast(entity->getPhysicsInfo()); + uint8_t region = _space->getRegion(entity->getSpaceIndex()); + bool shouldBePhysical = region < workload::Region::R3 && entity->shouldBePhysical(); if (motionState) { - if (!entity->shouldBePhysical()) { + if (!shouldBePhysical) { // the entity should be removed from the physical simulation _incomingChanges.remove(motionState); _physicalObjects.remove(motionState); @@ -133,7 +135,7 @@ void PhysicalEntitySimulation::changeEntityInternal(EntityItemPointer entity) { } else { _incomingChanges.insert(motionState); } - } else if (entity->shouldBePhysical()) { + } else if (shouldBePhysical) { // The intent is for this object to be in the PhysicsEngine, but it has no MotionState yet. // Perhaps it's shape has changed and it can now be added? _entitiesToAddToPhysics.insert(entity); diff --git a/libraries/physics/src/PhysicalEntitySimulation.h b/libraries/physics/src/PhysicalEntitySimulation.h index 7b6fe221fb..3f04347f18 100644 --- a/libraries/physics/src/PhysicalEntitySimulation.h +++ b/libraries/physics/src/PhysicalEntitySimulation.h @@ -19,6 +19,7 @@ #include #include +#include #include "PhysicsEngine.h" #include "EntityMotionState.h" @@ -45,6 +46,7 @@ public: ~PhysicalEntitySimulation(); void init(EntityTreePointer tree, PhysicsEnginePointer engine, EntityEditPacketSender* packetSender); + void setWorkloadSpace(const workload::SpacePointer space) { _space = space; } virtual void addDynamic(EntityDynamicPointer dynamic) override; virtual void applyDynamicChanges() override; @@ -102,6 +104,7 @@ private: VectorOfEntityMotionStates _owned; VectorOfEntityMotionStates _bids; + workload::SpacePointer _space; uint64_t _nextBidExpiry; uint32_t _lastStepSendPackets { 0 }; }; diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 1d4c385f07..945b9c86dd 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -72,6 +72,7 @@ uint32_t PhysicsEngine::getNumSubsteps() { // private void PhysicsEngine::addObjectToDynamicsWorld(ObjectMotionState* motionState) { + qCDebug(physics) << "templog addObject" << (void*)(motionState); // TODO: remove this assert(motionState); btVector3 inertia(0.0f, 0.0f, 0.0f); @@ -193,6 +194,7 @@ void PhysicsEngine::removeObjects(const VectorOfMotionStates& objects) { for (auto object : objects) { btRigidBody* body = object->getRigidBody(); if (body) { + qCDebug(physics) << "removeObject" << (void*)(body->getMotionState()); // TODO: remove this removeDynamicsForBody(body); _dynamicsWorld->removeRigidBody(body); diff --git a/libraries/workload/src/workload/Proxy.h b/libraries/workload/src/workload/Proxy.h index d204bd8544..8e0be6f6f7 100644 --- a/libraries/workload/src/workload/Proxy.h +++ b/libraries/workload/src/workload/Proxy.h @@ -20,28 +20,21 @@ public: Owner() = default; Owner(const Owner& other) = default; Owner& operator=(const Owner& other) = default; - template Owner(const T& data) : _concept(std::make_shared>(data)) {} - ~Owner() {} - template const T get() const { return std::static_pointer_cast>(_concept)->_data; } - protected: class Concept { public: virtual ~Concept() = default; - }; template class Model : public Concept { public: using Data = T; Data _data; - Model(const Data& data) : _data(data) {} virtual ~Model() = default; }; - private: std::shared_ptr _concept; };