diff --git a/examples/testbed/tests/tension_mapping.h b/examples/testbed/tests/tension_mapping.h index fe61e7a..463113d 100644 --- a/examples/testbed/tests/tension_mapping.h +++ b/examples/testbed/tests/tension_mapping.h @@ -97,7 +97,7 @@ public: { Test::Step(); - b3ClothMesh* mesh = m_cloth->GetMesh(); + const b3ClothMesh* mesh = m_cloth->GetMesh(); b3StackArray tension; tension.Resize(mesh->vertexCount); @@ -124,9 +124,9 @@ public: { b3ClothMeshTriangle* t = m_rectangleClothMesh.triangles + i; - b3Vec3 v1 = mesh->particles[t->v1]->GetPosition(); - b3Vec3 v2 = mesh->particles[t->v2]->GetPosition(); - b3Vec3 v3 = mesh->particles[t->v3]->GetPosition(); + b3Vec3 v1 = m_cloth->GetVertexParticle(t->v1)->GetPosition(); + b3Vec3 v2 = m_cloth->GetVertexParticle(t->v2)->GetPosition(); + b3Vec3 v3 = m_cloth->GetVertexParticle(t->v3)->GetPosition(); b3Draw_draw->DrawSegment(v1, v2, b3Color_black); b3Draw_draw->DrawSegment(v2, v3, b3Color_black); diff --git a/include/bounce/controllers/cloth_dragger.h b/include/bounce/controllers/cloth_dragger.h index 9faaa73..b77060a 100644 --- a/include/bounce/controllers/cloth_dragger.h +++ b/include/bounce/controllers/cloth_dragger.h @@ -63,9 +63,9 @@ public: m_triangle = m_mesh->triangles + rayOut.triangle; m_x = rayOut.fraction; - b3Particle* p1 = m_mesh->particles[m_triangle->v1]; - b3Particle* p2 = m_mesh->particles[m_triangle->v2]; - b3Particle* p3 = m_mesh->particles[m_triangle->v3]; + b3Particle* p1 = m_cloth->GetVertexParticle(m_triangle->v1); + b3Particle* p2 = m_cloth->GetVertexParticle(m_triangle->v2); + b3Particle* p3 = m_cloth->GetVertexParticle(m_triangle->v3); b3Vec3 v1 = p1->GetPosition(); b3Vec3 v2 = p2->GetPosition(); @@ -151,13 +151,13 @@ public: } else { - b3Particle* p1 = m_mesh->particles[m_triangle->v1]; + b3Particle* p1 = m_cloth->GetVertexParticle(m_triangle->v1); p1->ApplyTranslation(dx); - b3Particle* p2 = m_mesh->particles[m_triangle->v2]; + b3Particle* p2 = m_cloth->GetVertexParticle(m_triangle->v2); p2->ApplyTranslation(dx); - b3Particle* p3 = m_mesh->particles[m_triangle->v3]; + b3Particle* p3 = m_cloth->GetVertexParticle(m_triangle->v3); p3->ApplyTranslation(dx); } } @@ -166,8 +166,6 @@ public: { B3_ASSERT(IsDragging() == true); - m_cloth = nullptr; - if (m_spring) { m_cloth->DestroyForce(m_s1); @@ -177,24 +175,21 @@ public: } else { - b3Particle* p1 = m_mesh->particles[m_triangle->v1]; - p1->SetType(m_t1); - - b3Particle* p2 = m_mesh->particles[m_triangle->v2]; - p2->SetType(m_t2); - - b3Particle* p3 = m_mesh->particles[m_triangle->v3]; - p3->SetType(m_t3); + m_cloth->GetVertexParticle(m_triangle->v1)->SetType(m_t1); + m_cloth->GetVertexParticle(m_triangle->v2)->SetType(m_t2); + m_cloth->GetVertexParticle(m_triangle->v3)->SetType(m_t3); } + + m_cloth = nullptr; } b3Vec3 GetPointA() const { B3_ASSERT(IsDragging() == true); - b3Vec3 A = m_mesh->particles[m_triangle->v1]->GetPosition(); - b3Vec3 B = m_mesh->particles[m_triangle->v2]->GetPosition(); - b3Vec3 C = m_mesh->particles[m_triangle->v3]->GetPosition(); + b3Vec3 A = m_cloth->GetVertexParticle(m_triangle->v1)->GetPosition(); + b3Vec3 B = m_cloth->GetVertexParticle(m_triangle->v2)->GetPosition(); + b3Vec3 C = m_cloth->GetVertexParticle(m_triangle->v3)->GetPosition(); return m_u * A + m_v * B + (1.0f - m_u - m_v) * C; } @@ -212,7 +207,7 @@ private: b3World* m_world; b3Cloth* m_cloth; - b3ClothMesh* m_mesh; + const b3ClothMesh* m_mesh; b3ClothMeshTriangle* m_triangle; float32 m_u, m_v; diff --git a/include/bounce/dynamics/cloth/cloth.h b/include/bounce/dynamics/cloth/cloth.h index 81fc4fb..7687045 100644 --- a/include/bounce/dynamics/cloth/cloth.h +++ b/include/bounce/dynamics/cloth/cloth.h @@ -60,7 +60,7 @@ struct b3ClothDef } // Cloth mesh - b3ClothMesh* mesh; + const b3ClothMesh* mesh; // Cloth density in kg/m^3 float32 density; @@ -106,7 +106,10 @@ public: bool RayCast(b3RayCastOutput* output, const b3RayCastInput* input, u32 triangleIndex) const; // Return the cloth mesh proxy. - b3ClothMesh* GetMesh() const; + const b3ClothMesh* GetMesh() const; + + // Return the particle associated with the given vertex. + b3Particle* GetVertexParticle(u32 i); // Return the list of particles in this cloth. const b3List2& GetParticleList() const; @@ -148,8 +151,11 @@ private: void Solve(float32 dt, const b3Vec3& gravity); // Proxy mesh - b3ClothMesh* m_mesh; + const b3ClothMesh* m_mesh; + // Vertex particles + b3Particle** m_vertexParticles; + // Mesh density float32 m_density; @@ -180,7 +186,7 @@ inline b3World* b3Cloth::GetWorld() return m_world; } -inline b3ClothMesh* b3Cloth::GetMesh() const +inline const b3ClothMesh* b3Cloth::GetMesh() const { return m_mesh; } diff --git a/include/bounce/dynamics/cloth/cloth_mesh.h b/include/bounce/dynamics/cloth/cloth_mesh.h index 8aea759..593f6dc 100644 --- a/include/bounce/dynamics/cloth/cloth_mesh.h +++ b/include/bounce/dynamics/cloth/cloth_mesh.h @@ -22,8 +22,6 @@ #include #include -class b3Particle; - struct b3ClothMeshTriangle { u32 v1, v2, v3; @@ -47,7 +45,6 @@ struct b3ClothMesh { u32 vertexCount; b3Vec3* vertices; - b3Particle** particles; u32 triangleCount; b3ClothMeshTriangle* triangles; u32 meshCount; diff --git a/src/bounce/dynamics/cloth/cloth.cpp b/src/bounce/dynamics/cloth/cloth.cpp index 14992fb..904ebcb 100644 --- a/src/bounce/dynamics/cloth/cloth.cpp +++ b/src/bounce/dynamics/cloth/cloth.cpp @@ -157,8 +157,10 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) : m_particleBlocks(sizeo m_world = world; m_mesh = def.mesh; m_density = def.density; + + const b3ClothMesh* m = m_mesh; - b3ClothMesh* m = m_mesh; + m_vertexParticles = (b3Particle**)b3Alloc(m->vertexCount * sizeof(b3Particle*)); // Create particles for (u32 i = 0; i < m->vertexCount; ++i) @@ -170,7 +172,8 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) : m_particleBlocks(sizeo b3Particle* p = CreateParticle(pd); p->m_vertex = i; - m->particles[i] = p; + + m_vertexParticles[i] = p; } // Compute mass @@ -192,8 +195,8 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) : m_particleBlocks(sizeo { b3UniqueEdge* e = uniqueEdges + i; - b3Particle* p1 = m->particles[e->v1]; - b3Particle* p2 = m->particles[e->v2]; + b3Particle* p1 = m_vertexParticles[e->v1]; + b3Particle* p2 = m_vertexParticles[e->v2]; b3SpringForceDef fd; fd.Initialize(p1, p2, def.structural, def.damping); @@ -206,10 +209,10 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) : m_particleBlocks(sizeo { b3SharedEdge* e = sharedEdges + i; - b3Particle* p1 = m->particles[e->v1]; - b3Particle* p2 = m->particles[e->v2]; - b3Particle* p3 = m->particles[e->nsv1]; - b3Particle* p4 = m->particles[e->nsv2]; + b3Particle* p1 = m_vertexParticles[e->v1]; + b3Particle* p2 = m_vertexParticles[e->v2]; + b3Particle* p3 = m_vertexParticles[e->nsv1]; + b3Particle* p4 = m_vertexParticles[e->nsv2]; b3BendForceDef fd; fd.Initialize(p1, p2, p3, p4, def.bending, def.damping); @@ -225,8 +228,8 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) : m_particleBlocks(sizeo { b3ClothMeshSewingLine* line = m->sewingLines + i; - b3Particle* p1 = m->particles[line->v1]; - b3Particle* p2 = m->particles[line->v2]; + b3Particle* p1 = m_vertexParticles[line->v1]; + b3Particle* p2 = m_vertexParticles[line->v2]; b3SpringForceDef fd; fd.Initialize(p1, p2, def.structural, def.damping); @@ -245,6 +248,8 @@ b3Cloth::~b3Cloth() p0->~b3Particle(); } + b3Free(m_vertexParticles); + b3Force* f = m_forceList.m_head; while (f) { @@ -267,7 +272,7 @@ void b3Cloth::DestroyParticle(b3Particle* particle) { if (particle->m_vertex != ~0) { - m_mesh->particles[particle->m_vertex] = NULL; + m_vertexParticles[particle->m_vertex] = NULL; } m_particleList.Remove(particle); @@ -298,6 +303,12 @@ float32 b3Cloth::GetEnergy() const return 0.5f * E; } +b3Particle* b3Cloth::GetVertexParticle(u32 i) +{ + B3_ASSERT(i < m_mesh->vertexCount); + return m_vertexParticles[i]; +} + void b3Cloth::ComputeMass() { for (b3Particle* p = m_particleList.m_head; p; p = p->m_next) @@ -322,9 +333,9 @@ void b3Cloth::ComputeMass() float32 mass = rho * area; - b3Particle* p1 = m_mesh->particles[triangle->v1]; - b3Particle* p2 = m_mesh->particles[triangle->v2]; - b3Particle* p3 = m_mesh->particles[triangle->v3]; + b3Particle* p1 = m_vertexParticles[triangle->v1]; + b3Particle* p2 = m_vertexParticles[triangle->v2]; + b3Particle* p3 = m_vertexParticles[triangle->v3]; p1->m_mass += inv3 * mass; p2->m_mass += inv3 * mass; @@ -408,9 +419,9 @@ bool b3Cloth::RayCast(b3RayCastOutput* output, const b3RayCastInput* input, u32 B3_ASSERT(triangleIndex < m_mesh->triangleCount); b3ClothMeshTriangle* triangle = m_mesh->triangles + triangleIndex; - b3Vec3 v1 = m_mesh->particles[triangle->v1]->m_position; - b3Vec3 v2 = m_mesh->particles[triangle->v2]->m_position; - b3Vec3 v3 = m_mesh->particles[triangle->v3]->m_position; + b3Vec3 v1 = m_vertexParticles[triangle->v1]->m_position; + b3Vec3 v2 = m_vertexParticles[triangle->v2]->m_position; + b3Vec3 v3 = m_vertexParticles[triangle->v3]->m_position; b3Vec3 p1 = input->p1; b3Vec3 p2 = input->p2; @@ -773,8 +784,8 @@ void b3Cloth::Draw() const for (u32 i = 0; i < m->sewingLineCount; ++i) { b3ClothMeshSewingLine* s = m->sewingLines + i; - b3Particle* p1 = m->particles[s->v1]; - b3Particle* p2 = m->particles[s->v2]; + b3Particle* p1 = m_vertexParticles[s->v1]; + b3Particle* p2 = m_vertexParticles[s->v2]; b3Draw_draw->DrawSegment(p1->m_position, p2->m_position, b3Color_white); } @@ -783,9 +794,9 @@ void b3Cloth::Draw() const { b3ClothMeshTriangle* t = m->triangles + i; - b3Particle* p1 = m->particles[t->v1]; - b3Particle* p2 = m->particles[t->v2]; - b3Particle* p3 = m->particles[t->v3]; + b3Particle* p1 = m_vertexParticles[t->v1]; + b3Particle* p2 = m_vertexParticles[t->v2]; + b3Particle* p3 = m_vertexParticles[t->v3]; b3Vec3 v1 = p1->m_position; b3Vec3 v2 = p2->m_position; diff --git a/src/bounce/dynamics/cloth/cloth_mesh.cpp b/src/bounce/dynamics/cloth/cloth_mesh.cpp index f15b3e8..f51c408 100644 --- a/src/bounce/dynamics/cloth/cloth_mesh.cpp +++ b/src/bounce/dynamics/cloth/cloth_mesh.cpp @@ -25,7 +25,6 @@ b3GarmentClothMesh::b3GarmentClothMesh() { vertexCount = 0; vertices = nullptr; - particles = nullptr; triangleCount = 0; triangles = nullptr; meshCount = 0; @@ -37,7 +36,6 @@ b3GarmentClothMesh::b3GarmentClothMesh() b3GarmentClothMesh::~b3GarmentClothMesh() { b3Free(vertices); - b3Free(particles); b3Free(triangles); b3Free(meshes); b3Free(sewingLines); @@ -51,9 +49,7 @@ void b3GarmentClothMesh::Set(const b3GarmentMesh* garment) vertexCount += garment->meshes[i].vertexCount; } vertices = (b3Vec3*)b3Alloc(vertexCount * sizeof(b3Vec3)); - particles = (b3Particle**)b3Alloc(vertexCount * sizeof(b3Particle*)); - memset(particles, 0, vertexCount * sizeof(b3Particle*)); - + B3_ASSERT(triangleCount == 0); for (u32 i = 0; i < garment->meshCount; ++i) {