diff --git a/include/bounce/cloth/force.h b/include/bounce/cloth/force.h index 04c456f..9e9132f 100644 --- a/include/bounce/cloth/force.h +++ b/include/bounce/cloth/force.h @@ -46,6 +46,9 @@ public: // b3Force* GetNext(); + + // + virtual bool HasParticle(const b3Particle* particle) const = 0; protected: friend class b3List2; friend class b3Cloth; diff --git a/include/bounce/cloth/spring_force.h b/include/bounce/cloth/spring_force.h index aee0425..f7da89f 100644 --- a/include/bounce/cloth/spring_force.h +++ b/include/bounce/cloth/spring_force.h @@ -67,6 +67,8 @@ public: float32 GetDampingStiffness() const; b3Vec3 GetActionForce() const; + + bool HasParticle(const b3Particle* particle) const; private: friend class b3Force; friend class b3Cloth; @@ -127,4 +129,9 @@ inline b3Vec3 b3SpringForce::GetActionForce() const return m_f; } +inline bool b3SpringForce::HasParticle(const b3Particle* particle) const +{ + return m_p1 == particle || m_p2 == particle; +} + #endif \ No newline at end of file diff --git a/src/bounce/cloth/cloth.cpp b/src/bounce/cloth/cloth.cpp index 0550732..85b12dd 100644 --- a/src/bounce/cloth/cloth.cpp +++ b/src/bounce/cloth/cloth.cpp @@ -257,8 +257,7 @@ b3Cloth::~b3Cloth() { b3Force* f0 = f; f = f->m_next; - f0->~b3Force(); - b3Free(f0); + b3Force::Destroy(f0); } } @@ -280,6 +279,19 @@ b3Particle* b3Cloth::CreateParticle(const b3ParticleDef& def) void b3Cloth::DestroyParticle(b3Particle* particle) { B3_ASSERT(particle->m_vertex == ~0); + + b3Force* f = m_forceList.m_head; + while (f) + { + b3Force* f0 = f; + f = f->m_next; + + if (f0->HasParticle(particle)) + { + m_forceList.Remove(f0); + b3Force::Destroy(f0); + } + } m_particleTree.RemoveNode(particle->m_treeId);