diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index e77134413c..97647501ea 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -37,7 +37,7 @@ EntityTreeElement* EntityTree::createNewElement(unsigned char * octalCode) { void EntityTree::eraseAllOctreeElements(bool createNewRoot) { // this would be a good place to clean up our entities... foreach (EntityTreeElement* element, _entityToElementMap) { - element->cleanupEntities(); + element->cleanupEntities(_physicsWorld); } _entityToElementMap.clear(); Octree::eraseAllOctreeElements(createNewRoot); @@ -91,6 +91,7 @@ void EntityTree::addEntityItem(EntityItem* entityItem) { recurseTreeWithOperator(&theOperator); // check to see if we need to simulate this entity.. + // BOOKMARK -- add entity to physics engine here changeEntityState(entityItem, EntityItem::Static, entityItem->getSimulationState()); _isDirty = true; @@ -133,7 +134,9 @@ bool EntityTree::updateEntity(const EntityItemID& entityID, const EntityItemProp _isDirty = true; EntityItem::SimulationState newState = existingEntity->getSimulationState(); - changeEntityState(existingEntity, oldState, newState); + if (newState != oldState) { + changeEntityState(existingEntity, oldState, newState); + } QString entityScriptAfter = existingEntity->getScript(); if (entityScriptBefore != entityScriptAfter) { diff --git a/libraries/entities/src/EntityTreeElement.cpp b/libraries/entities/src/EntityTreeElement.cpp index 1dea2bcd85..b26db16f89 100644 --- a/libraries/entities/src/EntityTreeElement.cpp +++ b/libraries/entities/src/EntityTreeElement.cpp @@ -16,7 +16,9 @@ #include #include +#include +#include "EntityMotionState.h" #include "EntityTree.h" #include "EntityTreeElement.h" @@ -653,10 +655,16 @@ EntityItem* EntityTreeElement::getEntityWithEntityItemID(const EntityItemID& id) return foundEntity; } -void EntityTreeElement::cleanupEntities() { +void EntityTreeElement::cleanupEntities(PhysicsWorld* physicsWorld) { uint16_t numberOfEntities = _entityItems->size(); for (uint16_t i = 0; i < numberOfEntities; i++) { EntityItem* entity = (*_entityItems)[i]; + EntityMotionState* motionState = entity->getMotionState(); + if (motionState) { + assert(physicsWorld); + physicsWorld->removeEntity(static_cast(motionState)); + entity->destroyMotionState(); + } delete entity; } _entityItems->clear(); diff --git a/libraries/entities/src/EntityTreeElement.h b/libraries/entities/src/EntityTreeElement.h index ab3754749b..7e26aacf1c 100644 --- a/libraries/entities/src/EntityTreeElement.h +++ b/libraries/entities/src/EntityTreeElement.h @@ -21,6 +21,7 @@ class EntityTree; class EntityTreeElement; +class PhysicsWorld; class EntityTreeUpdateArgs { public: @@ -175,7 +176,7 @@ public: EntityItem* getEntityWithEntityItemID(const EntityItemID& id); - void cleanupEntities(); /// called by EntityTree on cleanup this will free all entities + void cleanupEntities(PhysicsWorld* physicsWorld); bool removeEntityWithEntityItemID(const EntityItemID& id); bool removeEntityItem(EntityItem* entity);