Browse Source

Keep track of constraints to release them properly before deleting the rigid body.

Lasse Öörni 13 years ago
parent
commit
b636b3a211

+ 6 - 6
Engine/Container/HashMap.h

@@ -483,23 +483,23 @@ private:
     }
     }
     
     
     /// Erase a node from the list. Return pointer to the next element, or to the end if could not erase.
     /// Erase a node from the list. Return pointer to the next element, or to the end if could not erase.
-    Node* EraseNode(Node* toRemove)
+    Node* EraseNode(Node* node)
     {
     {
         // The tail node can not be removed
         // The tail node can not be removed
-        if (!toRemove || toRemove == tail_)
+        if (!node || node == tail_)
             return Tail();
             return Tail();
         
         
-        Node* prev = toRemove->Prev();
-        Node* next = toRemove->Next();
+        Node* prev = node->Prev();
+        Node* next = node->Next();
         if (prev)
         if (prev)
             prev->next_ = next;
             prev->next_ = next;
         next->prev_ = prev;
         next->prev_ = prev;
         
         
         // Reassign the head node if necessary
         // Reassign the head node if necessary
-        if (toRemove == Head())
+        if (node == Head())
             head_ = next;
             head_ = next;
         
         
-        FreeNode(toRemove);
+        FreeNode(node);
         --size_;
         --size_;
         
         
         return next;
         return next;

+ 16 - 16
Engine/Container/HashSet.h

@@ -174,10 +174,10 @@ public:
         if (rhs.size_ != size_)
         if (rhs.size_ != size_)
             return false;
             return false;
         
         
-        ConstIterator i = Begin();
-        while (i != End())
+        ConstIterator it = Begin();
+        while (it != End())
         {
         {
-            if (!rhs.Contains(*i))
+            if (!rhs.Contains(*it))
                 return false;
                 return false;
             ++i;
             ++i;
         }
         }
@@ -191,12 +191,12 @@ public:
         if (rhs.size_ != size_)
         if (rhs.size_ != size_)
             return true;
             return true;
         
         
-        ConstIterator i = Begin();
-        while (i != End())
+        ConstIterator it = Begin();
+        while (it != End())
         {
         {
-            if (!rhs.Contains(*i))
+            if (!rhs.Contains(*it))
                 return true;
                 return true;
-            ++i;
+            ++it;
         }
         }
         
         
         return false;
         return false;
@@ -419,23 +419,23 @@ private:
     }
     }
     
     
     /// Erase a node from the list. Return pointer to the next element, or to the end if could not erase.
     /// Erase a node from the list. Return pointer to the next element, or to the end if could not erase.
-    Node* EraseNode(Node* toRemove)
+    Node* EraseNode(Node* node)
     {
     {
         // The tail node can not be removed
         // The tail node can not be removed
-        if (!toRemove || toRemove == tail_)
+        if (!node || node == tail_)
             return Tail();
             return Tail();
         
         
-        Node* prev = toRemove->Prev();
-        Node* next = toRemove->Next();
+        Node* prev = node->Prev();
+        Node* next = node->Next();
         if (prev)
         if (prev)
             prev->next_ = next;
             prev->next_ = next;
         next->prev_ = prev;
         next->prev_ = prev;
         
         
         // Reassign the head node if necessary
         // Reassign the head node if necessary
-        if (toRemove == Head())
+        if (node == Head())
             head_ = next;
             head_ = next;
         
         
-        FreeNode(toRemove);
+        FreeNode(node);
         --size_;
         --size_;
         
         
         return next;
         return next;
@@ -475,10 +475,10 @@ private:
         for (unsigned i = 0; i < numBuckets_; ++i)
         for (unsigned i = 0; i < numBuckets_; ++i)
             ptrs_[i] = 0;
             ptrs_[i] = 0;
         
         
-        for (Iterator i = Begin(); i != End(); ++i)
+        for (Iterator it = Begin(); it != End(); ++it)
         {
         {
-            Node* node = reinterpret_cast<Node*>(i.ptr_);
-            unsigned hashKey = MakeHash(*i) & (numBuckets_ - 1);
+            Node* node = reinterpret_cast<Node*>(it.ptr_);
+            unsigned hashKey = MakeHash(*it) & (numBuckets_ - 1);
             node->down_ = ptrs_[hashKey];
             node->down_ = ptrs_[hashKey];
             ptrs_[hashKey] = node;
             ptrs_[hashKey] = node;
         }
         }

+ 14 - 14
Engine/Container/List.h

@@ -287,19 +287,19 @@ public:
     /// Return iterator to value, or to the end if not found.
     /// Return iterator to value, or to the end if not found.
     Iterator Find(const T& value)
     Iterator Find(const T& value)
     {
     {
-        Iterator i = Begin();
-        while (i != End() && *i != value)
-            ++i;
-        return i;
+        Iterator it = Begin();
+        while (it != End() && *it != value)
+            ++it;
+        return it;
     }
     }
     
     
     /// Return const iterator to value, or to the end if not found.
     /// Return const iterator to value, or to the end if not found.
     ConstIterator Find(const T& value) const
     ConstIterator Find(const T& value) const
     {
     {
-        ConstIterator i = Begin();
-        while (i != End() && *i != value)
-            ++i;
-        return i;
+        ConstIterator it = Begin();
+        while (it != End() && *it != value)
+            ++it;
+        return it;
     }
     }
     
     
     /// Return whether contains a specific value.
     /// Return whether contains a specific value.
@@ -353,23 +353,23 @@ private:
     }
     }
     
     
     /// Erase and free a node. Return pointer to the next node, or to the end if could not erase.
     /// Erase and free a node. Return pointer to the next node, or to the end if could not erase.
-    Node* EraseNode(Node* toRemove)
+    Node* EraseNode(Node* node)
     {
     {
         // The tail node can not be removed
         // The tail node can not be removed
-        if (!toRemove || toRemove == tail_)
+        if (!node || node == tail_)
             return Tail();
             return Tail();
         
         
-        Node* prev = toRemove->Prev();
-        Node* next = toRemove->Next();
+        Node* prev = node->Prev();
+        Node* next = node->Next();
         if (prev)
         if (prev)
             prev->next_ = next;
             prev->next_ = next;
         next->prev_ = prev;
         next->prev_ = prev;
         
         
         // Reassign the head node if necessary
         // Reassign the head node if necessary
-        if (toRemove == Head())
+        if (node == Head())
             head_ = next;
             head_ = next;
         
         
-        FreeNode(toRemove);
+        FreeNode(node);
         --size_;
         --size_;
         
         
         return next;
         return next;

+ 18 - 18
Engine/Container/Vector.h

@@ -216,8 +216,8 @@ public:
         MoveRange(pos + length, pos, size_ - pos - length);
         MoveRange(pos + length, pos, size_ - pos - length);
         
         
         T* destPtr = Buffer() + pos;
         T* destPtr = Buffer() + pos;
-        for (Iterator i = start; i != end; ++i)
-            *destPtr++ = *i;
+        for (Iterator it = start; it != end; ++it)
+            *destPtr++ = *it;
         
         
         return Begin() + pos;
         return Begin() + pos;
     }
     }
@@ -309,19 +309,19 @@ public:
     /// Return iterator to value, or to the end if not found.
     /// Return iterator to value, or to the end if not found.
     Iterator Find(const T& value)
     Iterator Find(const T& value)
     {
     {
-        Iterator i = Begin();
-        while (i != End() && *i != value)
-            ++i;
-        return i;
+        Iterator it = Begin();
+        while (it != End() && *it != value)
+            ++it;
+        return it;
     }
     }
     
     
     /// Return const iterator to value, or to the end if not found.
     /// Return const iterator to value, or to the end if not found.
     ConstIterator Find(const T& value) const
     ConstIterator Find(const T& value) const
     {
     {
-        ConstIterator i = Begin();
-        while (i != End() && *i != value)
-            ++i;
-        return i;
+        ConstIterator it = Begin();
+        while (it != End() && *it != value)
+            ++it;
+        return it;
     }
     }
     
     
     /// Return whether contains a specific value.
     /// Return whether contains a specific value.
@@ -749,19 +749,19 @@ public:
     /// Return iterator to value, or to the end if not found.
     /// Return iterator to value, or to the end if not found.
     Iterator Find(const T& value)
     Iterator Find(const T& value)
     {
     {
-        Iterator i = Begin();
-        while (i != End() && *i != value)
-            ++i;
-        return i;
+        Iterator it = Begin();
+        while (it != End() && *it != value)
+            ++it;
+        return it;
     }
     }
     
     
     /// Return const iterator to value, or to the end if not found.
     /// Return const iterator to value, or to the end if not found.
     ConstIterator Find(const T& value) const
     ConstIterator Find(const T& value) const
     {
     {
-        ConstIterator i = Begin();
-        while (i != End() && *i != value)
-            ++i;
-        return i;
+        ConstIterator it = Begin();
+        while (it != End() && *it != value)
+            ++it;
+        return it;
     }
     }
     
     
     /// Return whether contains a specific value.
     /// Return whether contains a specific value.

+ 1 - 3
Engine/Graphics/Direct3D9/D3D9Graphics.cpp

@@ -1798,9 +1798,7 @@ void Graphics::AddGPUObject(GPUObject* object)
 
 
 void Graphics::RemoveGPUObject(GPUObject* object)
 void Graphics::RemoveGPUObject(GPUObject* object)
 {
 {
-    Vector<GPUObject*>::Iterator i = gpuObjects_.Find(object);
-    if (i != gpuObjects_.End())
-        gpuObjects_.Erase(i);
+    gpuObjects_.Erase(gpuObjects_.Find(object));
 }
 }
 
 
 unsigned Graphics::GetAlphaFormat()
 unsigned Graphics::GetAlphaFormat()

+ 1 - 3
Engine/Graphics/OpenGL/OGLGraphics.cpp

@@ -1670,9 +1670,7 @@ void Graphics::AddGPUObject(GPUObject* object)
 
 
 void Graphics::RemoveGPUObject(GPUObject* object)
 void Graphics::RemoveGPUObject(GPUObject* object)
 {
 {
-    Vector<GPUObject*>::Iterator i = gpuObjects_.Find(object);
-    if (i != gpuObjects_.End())
-        gpuObjects_.Erase(i);
+    gpuObjects_.Erase(gpuObjects_.Find(object));
 }
 }
 
 
 void* Graphics::ReserveDiscardLockBuffer(unsigned size)
 void* Graphics::ReserveDiscardLockBuffer(unsigned size)

+ 16 - 1
Engine/Physics/Constraint.cpp

@@ -108,6 +108,9 @@ void Constraint::ApplyAttributes()
 {
 {
     if (recreateConstraint_)
     if (recreateConstraint_)
     {
     {
+        if (otherBody_)
+            otherBody_->RemoveConstraint(this);
+        
         otherBody_.Reset();
         otherBody_.Reset();
         
         
         Scene* scene = GetScene();
         Scene* scene = GetScene();
@@ -154,6 +157,9 @@ void Constraint::SetOtherBody(RigidBody* body)
 {
 {
     if (otherBody_ != body)
     if (otherBody_ != body)
     {
     {
+        if (otherBody_)
+            otherBody_->RemoveConstraint(this);
+        
         otherBody_ = body;
         otherBody_ = body;
         
         
         // Update the connected body attribute
         // Update the connected body attribute
@@ -213,6 +219,11 @@ void Constraint::ReleaseConstraint()
 {
 {
     if (constraint_)
     if (constraint_)
     {
     {
+        if (ownBody_)
+            ownBody_->RemoveConstraint(this);
+        if (otherBody_)
+            otherBody_->RemoveConstraint(this);
+        
         if (physicsWorld_)
         if (physicsWorld_)
             physicsWorld_->GetWorld()->removeConstraint(constraint_);
             physicsWorld_->GetWorld()->removeConstraint(constraint_);
         
         
@@ -321,8 +332,12 @@ void Constraint::CreateConstraint()
     }
     }
     
     
     constraint_->setUserConstraintPtr(this);
     constraint_->setUserConstraintPtr(this);
-    physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
+    ownBody_->AddConstraint(this);
+    if (otherBody_)
+        otherBody_->AddConstraint(this);
+    
     ApplyLimits();
     ApplyLimits();
+    physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
 }
 }
 
 
 void Constraint::ApplyLimits()
 void Constraint::ApplyLimits()

+ 3 - 9
Engine/Physics/PhysicsWorld.cpp

@@ -299,9 +299,7 @@ void PhysicsWorld::AddRigidBody(RigidBody* body)
 
 
 void PhysicsWorld::RemoveRigidBody(RigidBody* body)
 void PhysicsWorld::RemoveRigidBody(RigidBody* body)
 {
 {
-    PODVector<RigidBody*>::Iterator i = rigidBodies_.Find(body);
-    if (i != rigidBodies_.End())
-        rigidBodies_.Erase(i);
+    rigidBodies_.Erase(rigidBodies_.Find(body));
 }
 }
 
 
 void PhysicsWorld::AddCollisionShape(CollisionShape* shape)
 void PhysicsWorld::AddCollisionShape(CollisionShape* shape)
@@ -311,9 +309,7 @@ void PhysicsWorld::AddCollisionShape(CollisionShape* shape)
 
 
 void PhysicsWorld::RemoveCollisionShape(CollisionShape* shape)
 void PhysicsWorld::RemoveCollisionShape(CollisionShape* shape)
 {
 {
-    PODVector<CollisionShape*>::Iterator i = collisionShapes_.Find(shape);
-    if (i != collisionShapes_.End())
-        collisionShapes_.Erase(i);
+    collisionShapes_.Erase(collisionShapes_.Find(shape));
 }
 }
 
 
 void PhysicsWorld::AddConstraint(Constraint* constraint)
 void PhysicsWorld::AddConstraint(Constraint* constraint)
@@ -323,9 +319,7 @@ void PhysicsWorld::AddConstraint(Constraint* constraint)
 
 
 void PhysicsWorld::RemoveConstraint(Constraint* constraint)
 void PhysicsWorld::RemoveConstraint(Constraint* constraint)
 {
 {
-    PODVector<Constraint*>::Iterator i = constraints_.Find(constraint);
-    if (i != constraints_.End())
-        constraints_.Erase(i);
+    constraints_.Erase(constraints_.Find(constraint));
 }
 }
 
 
 void PhysicsWorld::AddDelayedWorldTransform(const DelayedWorldTransform& transform)
 void PhysicsWorld::AddDelayedWorldTransform(const DelayedWorldTransform& transform)

+ 15 - 0
Engine/Physics/RigidBody.cpp

@@ -691,10 +691,25 @@ const PODVector<unsigned char>& RigidBody::GetNetAngularVelocityAttr() const
     return attrBuffer_.GetBuffer();
     return attrBuffer_.GetBuffer();
 }
 }
 
 
+void RigidBody::AddConstraint(Constraint* constraint)
+{
+    constraints_.Push(constraint);
+}
+
+void RigidBody::RemoveConstraint(Constraint* constraint)
+{
+    constraints_.Erase(constraints_.Find(constraint));
+}
+
 void RigidBody::ReleaseBody()
 void RigidBody::ReleaseBody()
 {
 {
     if (body_)
     if (body_)
     {
     {
+        // Release all constraints which refer to this body
+        for (PODVector<Constraint*>::Iterator i = constraints_.Begin(); i != constraints_.End(); ++i)
+            (*i)->ReleaseConstraint();
+        constraints_.Clear();
+        
         if (physicsWorld_)
         if (physicsWorld_)
         {
         {
             btDiscreteDynamicsWorld* world = physicsWorld_->GetWorld();
             btDiscreteDynamicsWorld* world = physicsWorld_->GetWorld();

+ 6 - 0
Engine/Physics/RigidBody.h

@@ -193,6 +193,10 @@ public:
     void SetNetAngularVelocityAttr(const PODVector<unsigned char>& value);
     void SetNetAngularVelocityAttr(const PODVector<unsigned char>& value);
     /// Return network angular velocity attribute.
     /// Return network angular velocity attribute.
     const PODVector<unsigned char>& GetNetAngularVelocityAttr() const;
     const PODVector<unsigned char>& GetNetAngularVelocityAttr() const;
+    /// Add a constraint that refers to this rigid body.
+    void AddConstraint(Constraint* constraint);
+    /// Remove a constraint that refers to this rigid body.
+    void RemoveConstraint(Constraint* constraint);
     /// Remove the rigid body.
     /// Remove the rigid body.
     void ReleaseBody();
     void ReleaseBody();
     
     
@@ -216,6 +220,8 @@ private:
     btCompoundShape* compoundShape_;
     btCompoundShape* compoundShape_;
     /// Physics world.
     /// Physics world.
     WeakPtr<PhysicsWorld> physicsWorld_;
     WeakPtr<PhysicsWorld> physicsWorld_;
+    /// Constraints that refer to this rigid body.
+    PODVector<Constraint*> constraints_;
     /// Mass.
     /// Mass.
     float mass_;
     float mass_;
     /// Attribute buffer for network replication.
     /// Attribute buffer for network replication.