From 9765e72ab9cc469fe459c3ad83eb1c9fbbbf8c0e Mon Sep 17 00:00:00 2001 From: Irlan Date: Tue, 18 Jun 2019 21:00:03 -0300 Subject: [PATCH] Better API naming. Enable self-collision by default. --- examples/testbed/framework/cloth_dragger.cpp | 24 +++---- examples/testbed/tests/tension_mapping.h | 23 +++--- include/bounce/cloth/cloth.h | 8 +-- include/bounce/cloth/cloth_triangle.h | 3 + include/bounce/cloth/particle.h | 6 +- src/bounce/cloth/cloth.cpp | 74 +++++++++----------- src/bounce/cloth/cloth_contact_manager.cpp | 14 ++-- src/bounce/cloth/cloth_triangle.cpp | 8 +-- src/bounce/cloth/particle.cpp | 4 +- 9 files changed, 85 insertions(+), 79 deletions(-) diff --git a/examples/testbed/framework/cloth_dragger.cpp b/examples/testbed/framework/cloth_dragger.cpp index ad18319..fb208c2 100644 --- a/examples/testbed/framework/cloth_dragger.cpp +++ b/examples/testbed/framework/cloth_dragger.cpp @@ -45,9 +45,9 @@ bool b3ClothDragger::StartDragging() m_triangle = m_mesh->triangles + rayOut.triangle; m_x = rayOut.fraction; - b3Particle* p1 = m_cloth->GetVertexParticle(m_triangle->v1); - b3Particle* p2 = m_cloth->GetVertexParticle(m_triangle->v2); - b3Particle* p3 = m_cloth->GetVertexParticle(m_triangle->v3); + b3Particle* p1 = m_cloth->GetParticle(m_triangle->v1); + b3Particle* p2 = m_cloth->GetParticle(m_triangle->v2); + b3Particle* p3 = m_cloth->GetParticle(m_triangle->v3); b3Vec3 v1 = p1->GetPosition(); b3Vec3 v2 = p2->GetPosition(); @@ -133,13 +133,13 @@ void b3ClothDragger::Drag() } else { - b3Particle* p1 = m_cloth->GetVertexParticle(m_triangle->v1); + b3Particle* p1 = m_cloth->GetParticle(m_triangle->v1); p1->ApplyTranslation(dx); - b3Particle* p2 = m_cloth->GetVertexParticle(m_triangle->v2); + b3Particle* p2 = m_cloth->GetParticle(m_triangle->v2); p2->ApplyTranslation(dx); - b3Particle* p3 = m_cloth->GetVertexParticle(m_triangle->v3); + b3Particle* p3 = m_cloth->GetParticle(m_triangle->v3); p3->ApplyTranslation(dx); } } @@ -172,9 +172,9 @@ void b3ClothDragger::StopDragging() } else { - 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->GetParticle(m_triangle->v1)->SetType(m_t1); + m_cloth->GetParticle(m_triangle->v2)->SetType(m_t2); + m_cloth->GetParticle(m_triangle->v3)->SetType(m_t3); } m_triangle = nullptr; @@ -184,9 +184,9 @@ b3Vec3 b3ClothDragger::GetPointA() const { B3_ASSERT(IsDragging() == true); - 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(); + b3Vec3 A = m_cloth->GetParticle(m_triangle->v1)->GetPosition(); + b3Vec3 B = m_cloth->GetParticle(m_triangle->v2)->GetPosition(); + b3Vec3 C = m_cloth->GetParticle(m_triangle->v3)->GetPosition(); return m_u * A + m_v * B + (1.0f - m_u - m_v) * C; } diff --git a/examples/testbed/tests/tension_mapping.h b/examples/testbed/tests/tension_mapping.h index 1da11b2..3b4f89b 100644 --- a/examples/testbed/tests/tension_mapping.h +++ b/examples/testbed/tests/tension_mapping.h @@ -130,8 +130,15 @@ public: u32 v1 = s->GetParticle1()->GetVertex(); u32 v2 = s->GetParticle2()->GetVertex(); - tension[v1] += s->GetActionForce(); - tension[v2] -= s->GetActionForce(); + if (v1 != ~0) + { + tension[v1] += s->GetActionForce(); + } + + if (v2 != ~0) + { + tension[v2] -= s->GetActionForce(); + } } } @@ -139,13 +146,13 @@ public: { b3ClothMeshTriangle* t = mesh->triangles + i; - b3Vec3 v1 = m_cloth->GetVertexParticle(t->v1)->GetPosition(); - b3Vec3 v2 = m_cloth->GetVertexParticle(t->v2)->GetPosition(); - b3Vec3 v3 = m_cloth->GetVertexParticle(t->v3)->GetPosition(); + b3Vec3 v1 = m_cloth->GetParticle(t->v1)->GetPosition(); + b3Vec3 v2 = m_cloth->GetParticle(t->v2)->GetPosition(); + b3Vec3 v3 = m_cloth->GetParticle(t->v3)->GetPosition(); - b3Draw_draw->DrawSegment(v1, v2, b3Color_black); - b3Draw_draw->DrawSegment(v2, v3, b3Color_black); - b3Draw_draw->DrawSegment(v3, v1, b3Color_black); + g_draw->DrawSegment(v1, v2, b3Color_black); + g_draw->DrawSegment(v2, v3, b3Color_black); + g_draw->DrawSegment(v3, v1, b3Color_black); b3Vec3 f1 = tension[t->v1]; float32 L1 = b3Length(f1); diff --git a/include/bounce/cloth/cloth.h b/include/bounce/cloth/cloth.h index 98109b2..1f8e365 100644 --- a/include/bounce/cloth/cloth.h +++ b/include/bounce/cloth/cloth.h @@ -133,8 +133,8 @@ public: // Return the cloth mesh proxy. const b3ClothMesh* GetMesh() const; - // Return the particle associated with the given vertex. - b3Particle* GetVertexParticle(u32 i); + // Return the cloth particle given vertex index. + b3Particle* GetParticle(u32 i); // Return the cloth triangle given the triangle index. b3ClothTriangle* GetTriangle(u32 i); @@ -179,8 +179,8 @@ private: // Proxy mesh const b3ClothMesh* m_mesh; - // Vertex particles - b3Particle** m_vertexParticles; + // Particles + b3Particle** m_particles; // Triangles b3ClothTriangle* m_triangles; diff --git a/include/bounce/cloth/cloth_triangle.h b/include/bounce/cloth/cloth_triangle.h index 6d4fcc3..ca51ccc 100644 --- a/include/bounce/cloth/cloth_triangle.h +++ b/include/bounce/cloth/cloth_triangle.h @@ -67,6 +67,9 @@ private: // AABB Proxy b3ClothAABBProxy m_aabbProxy; + + // Broadphase ID + u32 m_broadPhaseId; }; inline u32 b3ClothTriangle::GetTriangle() const diff --git a/include/bounce/cloth/particle.h b/include/bounce/cloth/particle.h index 9adbf2c..ce909e2 100644 --- a/include/bounce/cloth/particle.h +++ b/include/bounce/cloth/particle.h @@ -102,8 +102,7 @@ enum b3ClothAABBProxyType struct b3ClothAABBProxy { b3ClothAABBProxyType type; - void* data; - u32 broadPhaseId; + void* owner; }; // A cloth particle. @@ -229,6 +228,9 @@ private: // AABB Proxy b3ClothAABBProxy m_aabbProxy; + // Broadphase ID + u32 m_broadPhaseId; + // Links to the cloth particle list. b3Particle* m_prev; b3Particle* m_next; diff --git a/src/bounce/cloth/cloth.cpp b/src/bounce/cloth/cloth.cpp index 98da7f1..936df66 100644 --- a/src/bounce/cloth/cloth.cpp +++ b/src/bounce/cloth/cloth.cpp @@ -33,8 +33,6 @@ #include -#define B3_ENABLE_SELF_COLLISION 0 - static B3_FORCE_INLINE u32 b3NextIndex(u32 i) { return i + 1 < 3 ? i + 1 : 0; @@ -166,7 +164,7 @@ b3Cloth::b3Cloth(const b3ClothDef& def) : const b3ClothMesh* m = m_mesh; // Initialize particles - m_vertexParticles = (b3Particle**)b3Alloc(m->vertexCount * sizeof(b3Particle*)); + m_particles = (b3Particle**)b3Alloc(m->vertexCount * sizeof(b3Particle*)); for (u32 i = 0; i < m->vertexCount; ++i) { b3ParticleDef pd; @@ -178,7 +176,7 @@ b3Cloth::b3Cloth(const b3ClothDef& def) : b3Particle* p = CreateParticle(pd); p->m_vertex = i; - m_vertexParticles[i] = p; + m_particles[i] = p; } // Compute mass @@ -205,8 +203,8 @@ b3Cloth::b3Cloth(const b3ClothDef& def) : aabb.Extend(triangle->m_radius); triangle->m_aabbProxy.type = e_triangleProxy; - triangle->m_aabbProxy.data = triangle; - triangle->m_aabbProxy.broadPhaseId = m_contactManager.m_broadPhase.CreateProxy(aabb, &triangle->m_aabbProxy); + triangle->m_aabbProxy.owner = triangle; + triangle->m_broadPhaseId = m_contactManager.m_broadPhase.CreateProxy(aabb, &triangle->m_aabbProxy); } // Initialize forces @@ -225,8 +223,8 @@ b3Cloth::b3Cloth(const b3ClothDef& def) : { b3UniqueEdge* e = uniqueEdges + i; - b3Particle* p1 = m_vertexParticles[e->v1]; - b3Particle* p2 = m_vertexParticles[e->v2]; + b3Particle* p1 = m_particles[e->v1]; + b3Particle* p2 = m_particles[e->v2]; b3SpringForceDef fd; fd.Initialize(p1, p2, def.structural, def.damping); @@ -242,10 +240,10 @@ b3Cloth::b3Cloth(const b3ClothDef& def) : { b3SharedEdge* e = sharedEdges + i; - b3Particle* p1 = m_vertexParticles[e->v1]; - b3Particle* p2 = m_vertexParticles[e->v2]; - b3Particle* p3 = m_vertexParticles[e->nsv1]; - b3Particle* p4 = m_vertexParticles[e->nsv2]; + b3Particle* p1 = m_particles[e->v1]; + b3Particle* p2 = m_particles[e->v2]; + b3Particle* p3 = m_particles[e->nsv1]; + b3Particle* p4 = m_particles[e->nsv2]; b3SpringForceDef fd; fd.Initialize(p3, p4, def.bending, def.damping); @@ -264,8 +262,8 @@ b3Cloth::b3Cloth(const b3ClothDef& def) : { b3ClothMeshSewingLine* line = m->sewingLines + i; - b3Particle* p1 = m_vertexParticles[line->v1]; - b3Particle* p2 = m_vertexParticles[line->v2]; + b3Particle* p1 = m_particles[line->v1]; + b3Particle* p2 = m_particles[line->v2]; b3SpringForceDef fd; fd.Initialize(p1, p2, def.structural, def.damping); @@ -282,7 +280,7 @@ b3Cloth::b3Cloth(const b3ClothDef& def) : b3Cloth::~b3Cloth() { - b3Free(m_vertexParticles); + b3Free(m_particles); b3Free(m_triangles); b3Particle* p = m_particleList.m_head; @@ -311,8 +309,8 @@ b3Particle* b3Cloth::CreateParticle(const b3ParticleDef& def) aabb.Set(p->m_position, p->m_radius); p->m_aabbProxy.type = e_particleProxy; - p->m_aabbProxy.data = p; - p->m_aabbProxy.broadPhaseId = m_contactManager.m_broadPhase.CreateProxy(aabb, &p->m_aabbProxy); + p->m_aabbProxy.owner = p; + p->m_broadPhaseId = m_contactManager.m_broadPhase.CreateProxy(aabb, &p->m_aabbProxy); m_particleList.PushFront(p); @@ -341,7 +339,7 @@ void b3Cloth::DestroyParticle(b3Particle* particle) particle->DestroyContacts(); // Destroy AABB proxy - m_contactManager.m_broadPhase.DestroyProxy(particle->m_aabbProxy.broadPhaseId); + m_contactManager.m_broadPhase.DestroyProxy(particle->m_broadPhaseId); m_particleList.Remove(particle); particle->~b3Particle(); @@ -371,10 +369,10 @@ float32 b3Cloth::GetEnergy() const return 0.5f * E; } -b3Particle* b3Cloth::GetVertexParticle(u32 i) +b3Particle* b3Cloth::GetParticle(u32 i) { B3_ASSERT(i < m_mesh->vertexCount); - return m_vertexParticles[i]; + return m_particles[i]; } b3ClothTriangle* b3Cloth::GetTriangle(u32 i) @@ -407,9 +405,9 @@ void b3Cloth::ComputeMass() float32 mass = rho * area; - b3Particle* p1 = m_vertexParticles[triangle->v1]; - b3Particle* p2 = m_vertexParticles[triangle->v2]; - b3Particle* p3 = m_vertexParticles[triangle->v3]; + b3Particle* p1 = m_particles[triangle->v1]; + b3Particle* p2 = m_particles[triangle->v2]; + b3Particle* p3 = m_particles[triangle->v3]; p1->m_mass += inv3 * mass; p2->m_mass += inv3 * mass; @@ -438,7 +436,7 @@ struct b3ClothRayCastSingleCallback return input.maxFraction; } - b3ClothTriangle* triangle = (b3ClothTriangle*)proxy->data; + b3ClothTriangle* triangle = (b3ClothTriangle*)proxy->owner; u32 triangleIndex = triangle->GetTriangle(); b3RayCastOutput subOutput; @@ -495,9 +493,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_vertexParticles[triangle->v1]->m_position; - b3Vec3 v2 = m_vertexParticles[triangle->v2]->m_position; - b3Vec3 v3 = m_vertexParticles[triangle->v3]->m_position; + b3Vec3 v1 = m_particles[triangle->v1]->m_position; + b3Vec3 v2 = m_particles[triangle->v2]->m_position; + b3Vec3 v3 = m_particles[triangle->v3]->m_position; return b3RayCast(output, input, v1, v2, v3); } @@ -556,7 +554,7 @@ void b3Cloth::UpdateParticleBodyContacts() continue; } - b3AABB3 aabb = m_contactManager.m_broadPhase.GetAABB(p->m_aabbProxy.broadPhaseId); + b3AABB3 aabb = m_contactManager.m_broadPhase.GetAABB(p->m_broadPhaseId); b3ClothUpdateContactsQueryListener listener; listener.sphere.vertex = p->m_position; @@ -681,9 +679,9 @@ void b3Cloth::Step(float32 dt, u32 velocityIterations, u32 positionIterations) { b3ClothMeshTriangle* triangle = m_mesh->triangles + i; - b3Particle* p1 = m_vertexParticles[triangle->v1]; - b3Particle* p2 = m_vertexParticles[triangle->v2]; - b3Particle* p3 = m_vertexParticles[triangle->v3]; + b3Particle* p1 = m_particles[triangle->v1]; + b3Particle* p2 = m_particles[triangle->v2]; + b3Particle* p3 = m_particles[triangle->v3]; b3Vec3 v1 = p1->m_velocity; b3Vec3 v2 = p2->m_velocity; @@ -696,12 +694,8 @@ void b3Cloth::Step(float32 dt, u32 velocityIterations, u32 positionIterations) m_triangles[i].Synchronize(displacement); } -#if B3_ENABLE_SELF_COLLISION - // Find new self-contacts m_contactManager.FindNewContacts(); - -#endif } void b3Cloth::Draw() const @@ -741,8 +735,8 @@ void b3Cloth::Draw() const for (u32 i = 0; i < m->sewingLineCount; ++i) { b3ClothMeshSewingLine* s = m->sewingLines + i; - b3Particle* p1 = m_vertexParticles[s->v1]; - b3Particle* p2 = m_vertexParticles[s->v2]; + b3Particle* p1 = m_particles[s->v1]; + b3Particle* p2 = m_particles[s->v2]; b3Draw_draw->DrawSegment(p1->m_position, p2->m_position, b3Color_white); } @@ -751,9 +745,9 @@ void b3Cloth::Draw() const { b3ClothMeshTriangle* t = m->triangles + i; - b3Particle* p1 = m_vertexParticles[t->v1]; - b3Particle* p2 = m_vertexParticles[t->v2]; - b3Particle* p3 = m_vertexParticles[t->v3]; + b3Particle* p1 = m_particles[t->v1]; + b3Particle* p2 = m_particles[t->v2]; + b3Particle* p3 = m_particles[t->v3]; b3Vec3 v1 = p1->m_position; b3Vec3 v2 = p2->m_position; diff --git a/src/bounce/cloth/cloth_contact_manager.cpp b/src/bounce/cloth/cloth_contact_manager.cpp index eef9ae1..d41b4a8 100644 --- a/src/bounce/cloth/cloth_contact_manager.cpp +++ b/src/bounce/cloth/cloth_contact_manager.cpp @@ -61,13 +61,13 @@ void b3ClothContactManager::AddPair(void* data1, void* data2) B3_ASSERT(proxy1->type == e_particleProxy); B3_ASSERT(proxy2->type == e_triangleProxy); - b3Particle* p1 = (b3Particle*)proxy1->data; + b3Particle* p1 = (b3Particle*)proxy1->owner; - b3ClothTriangle* t2 = (b3ClothTriangle*)proxy2->data; + b3ClothTriangle* t2 = (b3ClothTriangle*)proxy2->owner; b3ClothMeshTriangle* triangle = m_cloth->m_mesh->triangles + t2->m_triangle; - b3Particle* p2 = m_cloth->m_vertexParticles[triangle->v1]; - b3Particle* p3 = m_cloth->m_vertexParticles[triangle->v2]; - b3Particle* p4 = m_cloth->m_vertexParticles[triangle->v3]; + b3Particle* p2 = m_cloth->m_particles[triangle->v1]; + b3Particle* p3 = m_cloth->m_particles[triangle->v2]; + b3Particle* p4 = m_cloth->m_particles[triangle->v3]; // Check if there is a contact between the two entities. for (b3ParticleTriangleContact* c = m_particleTriangleContactList.m_head; c; c = c->m_next) @@ -146,8 +146,8 @@ void b3ClothContactManager::UpdateContacts() continue; } - u32 proxy1 = c->m_p1->m_aabbProxy.broadPhaseId; - u32 proxy2 = c->m_t2->m_aabbProxy.broadPhaseId; + u32 proxy1 = c->m_p1->m_broadPhaseId; + u32 proxy2 = c->m_t2->m_broadPhaseId; // Destroy the contact if primitive AABBs are not overlapping. bool overlap = m_broadPhase.TestOverlap(proxy1, proxy2); diff --git a/src/bounce/cloth/cloth_triangle.cpp b/src/bounce/cloth/cloth_triangle.cpp index f4776f5..1fc5477 100644 --- a/src/bounce/cloth/cloth_triangle.cpp +++ b/src/bounce/cloth/cloth_triangle.cpp @@ -24,9 +24,9 @@ void b3ClothTriangle::Synchronize(const b3Vec3& displacement) { b3ClothMeshTriangle* triangle = m_cloth->m_mesh->triangles + m_triangle; - b3Particle* p1 = m_cloth->m_vertexParticles[triangle->v1]; - b3Particle* p2 = m_cloth->m_vertexParticles[triangle->v2]; - b3Particle* p3 = m_cloth->m_vertexParticles[triangle->v3]; + b3Particle* p1 = m_cloth->m_particles[triangle->v1]; + b3Particle* p2 = m_cloth->m_particles[triangle->v2]; + b3Particle* p3 = m_cloth->m_particles[triangle->v3]; b3Vec3 x1 = p1->m_position; b3Vec3 x2 = p2->m_position; @@ -36,5 +36,5 @@ void b3ClothTriangle::Synchronize(const b3Vec3& displacement) aabb.Set(x1, x2, x3); aabb.Extend(m_radius); - m_cloth->m_contactManager.m_broadPhase.MoveProxy(m_aabbProxy.broadPhaseId, aabb, displacement); + m_cloth->m_contactManager.m_broadPhase.MoveProxy(m_broadPhaseId, aabb, displacement); } \ No newline at end of file diff --git a/src/bounce/cloth/particle.cpp b/src/bounce/cloth/particle.cpp index b6caf0b..308188c 100644 --- a/src/bounce/cloth/particle.cpp +++ b/src/bounce/cloth/particle.cpp @@ -76,7 +76,7 @@ void b3Particle::Synchronize(const b3Vec3& displacement) b3AABB3 aabb; aabb.Set(m_position, m_radius); - m_cloth->m_contactManager.m_broadPhase.MoveProxy(m_aabbProxy.broadPhaseId, aabb, displacement); + m_cloth->m_contactManager.m_broadPhase.MoveProxy(m_broadPhaseId, aabb, displacement); } void b3Particle::SynchronizeTriangles() @@ -140,5 +140,5 @@ void b3Particle::SetType(b3ParticleType type) DestroyContacts(); // Move the proxy so new contacts can be created. - m_cloth->m_contactManager.m_broadPhase.TouchProxy(m_aabbProxy.broadPhaseId); + m_cloth->m_contactManager.m_broadPhase.TouchProxy(m_broadPhaseId); } \ No newline at end of file