maintain the upper triangle of A, external particle/force creation/destruction, particle force abstraction, testbed update

This commit is contained in:
Irlan
2018-05-30 11:34:41 -03:00
parent dba5ffbe06
commit caef3fede8
21 changed files with 1657 additions and 1088 deletions

View File

@ -25,6 +25,7 @@ public:
ClothDragger(Ray3* ray, b3Cloth*& cloth) : m_ray(ray), m_cloth(cloth)
{
m_isSelected = false;
m_spring = false;
}
~ClothDragger()
@ -61,31 +62,32 @@ public:
{
B3_ASSERT(m_isSelected == false);
if (Select(m_selection, m_x) == false)
b3RayCastInput rayIn;
rayIn.p1 = m_ray->A();
rayIn.p2 = m_ray->B();
rayIn.maxFraction = B3_MAX_FLOAT;
b3ClothRayCastOutput rayOut;
if (m_cloth->RayCast(&rayOut, &rayIn) == false)
{
return false;
}
m_isSelected = true;
m_selection = rayOut.triangle;
m_x = rayOut.fraction;
b3ClothMesh* m = m_cloth->GetMesh();
b3ClothMeshTriangle* t = m->triangles + m_selection;
b3Particle* p1 = m_cloth->GetParticle(t->v1);
m_t1 = p1->type;
m_cloth->SetType(p1, e_staticParticle);
b3Particle* p1 = m->particles[t->v1];
b3Particle* p2 = m->particles[t->v2];
b3Particle* p3 = m->particles[t->v3];
b3Particle* p2 = m_cloth->GetParticle(t->v2);
m_t2 = p2->type;
m_cloth->SetType(p2, e_staticParticle);
b3Particle* p3 = m_cloth->GetParticle(t->v3);
m_t3 = p3->type;
m_cloth->SetType(p3, e_staticParticle);
b3Vec3 v1 = p1->position;
b3Vec3 v2 = p2->position;
b3Vec3 v3 = p3->position;
b3Vec3 v1 = p1->GetPosition();
b3Vec3 v2 = p2->GetPosition();
b3Vec3 v3 = p3->GetPosition();
b3Vec3 B = GetPointB();
@ -102,6 +104,53 @@ public:
m_u = m_v = 0.0f;
}
if (m_spring)
{
b3ParticleDef pd;
pd.type = e_staticParticle;
pd.position = B;
m_particle = m_cloth->CreateParticle(pd);
{
b3SpringForceDef sfd;
sfd.p1 = m_particle;
sfd.p2 = p1;
sfd.restLength = 0.0f;
sfd.structural = 10000.0f;
m_s1 = (b3SpringForce*)m_cloth->CreateForce(sfd);
}
{
b3SpringForceDef sfd;
sfd.p1 = m_particle;
sfd.p2 = p2;
sfd.restLength = 0.0f;
sfd.structural = 10000.0f;
m_s2 = (b3SpringForce*)m_cloth->CreateForce(sfd);
}
{
b3SpringForceDef sfd;
sfd.p1 = m_particle;
sfd.p2 = p3;
sfd.restLength = 0.0f;
sfd.structural = 10000.0f;
m_s3 = (b3SpringForce*)m_cloth->CreateForce(sfd);
}
}
else
{
m_t1 = p1->GetType();
p1->SetType(e_staticParticle);
m_t2 = p2->GetType();
p2->SetType(e_staticParticle);
m_t3 = p3->GetType();
p3->SetType(e_staticParticle);
}
return true;
}
@ -117,14 +166,21 @@ public:
b3Vec3 dx = B - A;
b3Particle* p1 = m_cloth->GetParticle(t->v1);
m_cloth->ApplyTranslation(p1, dx);
b3Particle* p2 = m_cloth->GetParticle(t->v2);
m_cloth->ApplyTranslation(p2, dx);
b3Particle* p3 = m_cloth->GetParticle(t->v3);
m_cloth->ApplyTranslation(p3, dx);
if (m_spring)
{
m_particle->ApplyTranslation(dx);
}
else
{
b3Particle* p1 = m->particles[t->v1];
p1->ApplyTranslation(dx);
b3Particle* p2 = m->particles[t->v2];
p2->ApplyTranslation(dx);
b3Particle* p3 = m->particles[t->v3];
p3->ApplyTranslation(dx);
}
}
void StopDragging()
@ -133,141 +189,30 @@ public:
m_isSelected = false;
b3ClothMesh* m = m_cloth->GetMesh();
b3ClothMeshTriangle* t = m->triangles + m_selection;
if (m_spring)
{
m_cloth->DestroyForce(m_s1);
m_cloth->DestroyForce(m_s2);
m_cloth->DestroyForce(m_s3);
m_cloth->DestroyParticle(m_particle);
}
else
{
b3ClothMesh* m = m_cloth->GetMesh();
b3ClothMeshTriangle* t = m->triangles + m_selection;
b3Particle* p1 = m_cloth->GetParticle(t->v1);
m_cloth->SetType(p1, m_t1);
b3Particle* p2 = m_cloth->GetParticle(t->v2);
m_cloth->SetType(p2, m_t2);
b3Particle* p3 = m_cloth->GetParticle(t->v3);
m_cloth->SetType(p3, m_t3);
b3Particle* p1 = m->particles[t->v1];
p1->SetType(m_t1);
b3Particle* p2 = m->particles[t->v2];
p2->SetType(m_t2);
b3Particle* p3 = m->particles[t->v3];
p3->SetType(m_t3);
}
}
private:
bool RayCast(b3RayCastOutput* output, u32 triangleIndex) const
{
b3ClothMesh* mesh = m_cloth->GetMesh();
B3_ASSERT(triangleIndex < mesh->triangleCount);
b3ClothMeshTriangle* triangle = mesh->triangles + triangleIndex;
b3Vec3 v1 = mesh->vertices[triangle->v1];
b3Vec3 v2 = mesh->vertices[triangle->v2];
b3Vec3 v3 = mesh->vertices[triangle->v3];
b3Vec3 p1 = m_ray->A();
b3Vec3 p2 = m_ray->B();
b3Vec3 d = p2 - p1;
float32 maxFraction = b3Length(d);
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
n.Normalize();
float32 numerator = b3Dot(n, v1 - p1);
float32 denominator = b3Dot(n, d);
if (denominator == 0.0f)
{
return false;
}
float32 t = numerator / denominator;
// Is the intersection not on the segment?
if (t < 0.0f || maxFraction < t)
{
return false;
}
b3Vec3 q = p1 + t * d;
// Barycentric coordinates for q
b3Vec3 Q = q;
b3Vec3 A = v1;
b3Vec3 B = v2;
b3Vec3 C = v3;
b3Vec3 AB = B - A;
b3Vec3 AC = C - A;
b3Vec3 QA = A - Q;
b3Vec3 QB = B - Q;
b3Vec3 QC = C - Q;
b3Vec3 QB_x_QC = b3Cross(QB, QC);
b3Vec3 QC_x_QA = b3Cross(QC, QA);
b3Vec3 QA_x_QB = b3Cross(QA, QB);
b3Vec3 AB_x_AC = b3Cross(AB, AC);
float32 u = b3Dot(QB_x_QC, AB_x_AC);
float32 v = b3Dot(QC_x_QA, AB_x_AC);
float32 w = b3Dot(QA_x_QB, AB_x_AC);
// This tolerance helps intersections lying on
// shared edges to not be missed.
const float32 kTol = -B3_EPSILON;
// Is the intersection on the triangle?
if (u > kTol && v > kTol && w > kTol)
{
output->fraction = t;
// Does the ray start from below or above the triangle?
if (numerator > 0.0f)
{
output->normal = -n;
}
else
{
output->normal = n;
}
return true;
}
return false;
}
bool Select(u32& selection, float32& fraction) const
{
b3ClothMesh* m = m_cloth->GetMesh();
b3RayCastInput input;
input.p1 = m_ray->A();
input.p2 = m_ray->B();
input.maxFraction = m_ray->fraction;
float32 minFraction = B3_MAX_FLOAT;
b3Vec3 minNormal(0.0f, 0.0f, 0.0f);
u32 minIndex = ~0;
for (u32 i = 0; i < m->triangleCount; ++i)
{
b3RayCastOutput subOutput;
if (RayCast(&subOutput, i) == true)
{
if (subOutput.fraction < minFraction)
{
minFraction = subOutput.fraction;
minNormal = subOutput.normal;
minIndex = i;
}
}
}
if (minIndex != ~0)
{
selection = minIndex;
fraction = minFraction;
return true;
}
return false;
}
bool m_isSelected;
Ray3* m_ray;
@ -276,6 +221,14 @@ private:
b3Cloth*& m_cloth;
u32 m_selection;
float32 m_u, m_v;
bool m_spring;
b3Particle* m_particle;
b3SpringForce* m_s1;
b3SpringForce* m_s2;
b3SpringForce* m_s3;
b3ParticleType m_t1, m_t2, m_t3;
};

View File

@ -38,10 +38,9 @@ public:
void SetClothType(b3ParticleType type)
{
for (u32 i = 0; i < m_cloth->GetParticleCount(); ++i)
for (b3Particle* p = m_cloth->GetParticleList().m_head; p; p = p->GetNext())
{
b3Particle* p = m_cloth->GetParticle(i);
m_cloth->SetType(p, type);
p->SetType(type);
}
}
@ -62,10 +61,8 @@ public:
SetClothType(e_dynamicParticle);
}
for (u32 i = 0; i < m_cloth->GetParticleCount(); ++i)
for (b3Particle* p = m_cloth->GetParticleList().m_head; p; p = p->GetNext())
{
b3Particle* p = m_cloth->GetParticle(i);
b3Vec3 d;
d.SetZero();
@ -94,25 +91,25 @@ public:
button == GLFW_KEY_UP ||
button == GLFW_KEY_DOWN)
{
if (p->type == e_staticParticle)
if (p->GetType() == e_staticParticle)
{
m_cloth->ApplyTranslation(p, d);
p->ApplyTranslation(d);
}
if (p->type == e_kinematicParticle)
if (p->GetType() == e_kinematicParticle)
{
b3Vec3 v = p->velocity;
b3Vec3 v = p->GetVelocity();
v += 5.0f * d;
m_cloth->SetVelocity(p, d);
p->SetVelocity(v);
}
if (p->type == e_dynamicParticle)
if (p->GetType() == e_dynamicParticle)
{
b3Vec3 f = 100.0f * d;
m_cloth->ApplyForce(p, f);
p->ApplyForce(f);
}
}
}

View File

@ -22,32 +22,27 @@
class PinnedCloth : public ClothTest
{
public:
PinnedCloth()
PinnedCloth() : m_rectangleGarment(5.0f, 5.0f)
{
m_gridClothMesh.vertexCount = m_gridMesh.vertexCount;
m_gridClothMesh.vertices = m_gridMesh.vertices;
// Generate 2D mesh
m_rectangleGarmentMesh.Set(&m_rectangleGarment, 1.0f);
m_gridClothMesh.triangleCount = m_gridMesh.triangleCount;
m_gridClothMesh.triangles = (b3ClothMeshTriangle*)m_gridMesh.triangles;
m_gridClothMeshMesh.vertexCount = m_gridClothMesh.vertexCount;
m_gridClothMeshMesh.startVertex = 0;
m_gridClothMeshMesh.triangleCount = m_gridClothMesh.triangleCount;
m_gridClothMeshMesh.startTriangle = 0;
m_gridClothMesh.meshCount = 1;
m_gridClothMesh.meshes = &m_gridClothMeshMesh;
m_gridClothMesh.sewingLineCount = 0;
m_gridClothMesh.sewingLines = nullptr;
// Create 3D mesh
m_rectangleClothMesh.Set(&m_rectangleGarmentMesh);
//
b3Mat33 dq = b3Mat33RotationX(0.5f * B3_PI);
for (u32 i = 0; i < m_rectangleClothMesh.vertexCount; ++i)
{
m_rectangleClothMesh.vertices[i] = dq * m_rectangleClothMesh.vertices[i];
}
b3ClothDef def;
def.mesh = &m_gridClothMesh;
def.mesh = &m_rectangleClothMesh;
def.radius = 0.05f;
def.density = 0.2f;
def.ks = 10000.0f;
def.kd = 0.0f;
def.r = 0.05f;
def.structural = 10000.0f;
def.damping = 0.0f;
m_cloth = m_world.CreateCloth(def);
@ -55,12 +50,11 @@ public:
aabb.m_lower.Set(-5.0f, -1.0f, -6.0f);
aabb.m_upper.Set(5.0f, 1.0f, -4.0f);
for (u32 i = 0; i < m_cloth->GetParticleCount(); ++i)
for (b3Particle* p = m_cloth->GetParticleList().m_head; p; p = p->GetNext())
{
b3Particle* p = m_cloth->GetParticle(i);
if (aabb.Contains(p->position))
if (aabb.Contains(p->GetPosition()))
{
m_cloth->SetType(p, e_staticParticle);
p->SetType(e_staticParticle);
}
}
}
@ -69,10 +63,10 @@ public:
{
return new PinnedCloth();
}
b3GridMesh<10, 10> m_gridMesh;
b3ClothMeshMesh m_gridClothMeshMesh;
b3ClothMesh m_gridClothMesh;
b3RectangleGarment m_rectangleGarment;
b3GarmentMesh m_rectangleGarmentMesh;
b3GarmentClothMesh m_rectangleClothMesh;
};
#endif

View File

@ -19,10 +19,6 @@
#ifndef SHIRT_H
#define SHIRT_H
#include <bounce/garment/garment.h>
#include <bounce/garment/sewing_pattern.h>
#include <bounce/garment/garment_mesh.h>
class Shirt : public ClothTest
{
public:
@ -57,10 +53,10 @@ public:
// Create cloth
b3ClothDef def;
def.radius = 0.2f;
def.mesh = &m_shirtClothMesh;
def.density = 0.2f;
def.r = 0.2f;
def.ks = 10000.0f;
def.structural = 10000.0f;
m_cloth = m_world.CreateCloth(def);
}

View File

@ -22,38 +22,28 @@
class TableCloth : public ClothTest
{
public:
TableCloth()
TableCloth() : m_rectangleGarment(5.0f, 5.0f)
{
// Shift vertices up
for (u32 i = 0; i < m_gridMesh.vertexCount; ++i)
// Generate 2D mesh
m_rectangleGarmentMesh.Set(&m_rectangleGarment, 1.0f);
// Create 3D mesh
m_rectangleClothMesh.Set(&m_rectangleGarmentMesh);
//
b3Mat33 dq = b3Mat33RotationX(0.5f * B3_PI);
for (u32 i = 0; i < m_rectangleClothMesh.vertexCount; ++i)
{
m_gridMesh.vertices[i].y = 5.0f;
m_rectangleClothMesh.vertices[i] = dq * m_rectangleClothMesh.vertices[i];
m_rectangleClothMesh.vertices[i].y += 5.0f;
}
m_gridClothMesh.vertexCount = m_gridMesh.vertexCount;
m_gridClothMesh.vertices = m_gridMesh.vertices;
m_gridClothMesh.triangleCount = m_gridMesh.triangleCount;
m_gridClothMesh.triangles = (b3ClothMeshTriangle*)m_gridMesh.triangles;
m_gridClothMeshMesh.vertexCount = m_gridClothMesh.vertexCount;
m_gridClothMeshMesh.startVertex = 0;
m_gridClothMeshMesh.triangleCount = m_gridClothMesh.triangleCount;
m_gridClothMeshMesh.startTriangle = 0;
m_gridClothMesh.meshCount = 1;
m_gridClothMesh.meshes = &m_gridClothMeshMesh;
m_gridClothMesh.sewingLineCount = 0;
m_gridClothMesh.sewingLines = nullptr;
b3ClothDef def;
def.mesh = &m_gridClothMesh;
def.mesh = &m_rectangleClothMesh;
def.radius = 0.05f;
def.density = 0.2f;
def.ks = 10000.0f;
def.kd = 0.0f;
def.r = 0.05f;
def.structural = 10000.0f;
def.damping = 0.0f;
m_cloth = m_world.CreateCloth(def);
@ -87,10 +77,9 @@ public:
return new TableCloth();
}
//b3GridMesh<2, 2> m_gridMesh;
b3GridMesh<10, 10> m_gridMesh;
b3ClothMeshMesh m_gridClothMeshMesh;
b3ClothMesh m_gridClothMesh;
b3RectangleGarment m_rectangleGarment;
b3GarmentMesh m_rectangleGarmentMesh;
b3GarmentClothMesh m_rectangleClothMesh;
b3QHull m_tableHull;
};

View File

@ -58,32 +58,26 @@ static inline b3Color Color(float32 x, float32 a, float32 b)
class TensionMapping : public ClothTest
{
public:
TensionMapping()
TensionMapping() : m_rectangleGarment(5.0f, 5.0f)
{
m_gridClothMesh.vertexCount = m_gridMesh.vertexCount;
m_gridClothMesh.vertices = m_gridMesh.vertices;
// Generate 2D mesh
m_rectangleGarmentMesh.Set(&m_rectangleGarment, 1.0f);
m_gridClothMesh.triangleCount = m_gridMesh.triangleCount;
m_gridClothMesh.triangles = (b3ClothMeshTriangle*)m_gridMesh.triangles;
m_gridClothMeshMesh.vertexCount = m_gridClothMesh.vertexCount;
m_gridClothMeshMesh.startVertex = 0;
// Create 3D mesh
m_rectangleClothMesh.Set(&m_rectangleGarmentMesh);
m_gridClothMeshMesh.triangleCount = m_gridClothMesh.triangleCount;
m_gridClothMeshMesh.startTriangle = 0;
//
b3Mat33 dq = b3Mat33RotationX(0.5f * B3_PI);
for (u32 i = 0; i < m_rectangleClothMesh.vertexCount; ++i)
{
m_rectangleClothMesh.vertices[i] = dq * m_rectangleClothMesh.vertices[i];
}
m_gridClothMesh.meshCount = 1;
m_gridClothMesh.meshes = &m_gridClothMeshMesh;
m_gridClothMesh.sewingLineCount = 0;
m_gridClothMesh.sewingLines = nullptr;
b3ClothDef def;
def.mesh = &m_gridClothMesh;
def.mesh = &m_rectangleClothMesh;
def.radius = 0.2f;
def.density = 0.2f;
def.ks = 10000.0f;
def.kd = 0.0f;
def.r = 0.2f;
def.structural = 10000.0f;
m_cloth = m_world.CreateCloth(def);
@ -91,12 +85,11 @@ public:
aabb.m_lower.Set(-5.0f, -1.0f, -6.0f);
aabb.m_upper.Set(5.0f, 1.0f, -4.0f);
for (u32 i = 0; i < m_cloth->GetParticleCount(); ++i)
for (b3Particle* p = m_cloth->GetParticleList().m_head; p; p = p->GetNext())
{
b3Particle* p = m_cloth->GetParticle(i);
if (aabb.Contains(p->position))
if (aabb.Contains(p->GetPosition()))
{
m_cloth->SetType(p, e_staticParticle);
p->SetType(e_staticParticle);
}
}
}
@ -107,38 +100,36 @@ public:
m_cloth->Apply();
b3ClothMesh* mesh = m_cloth->GetMesh();
b3StackArray<b3Vec3, 256> tension;
tension.Resize(m_cloth->GetParticleCount());
for (u32 i = 0; i < tension.Count(); ++i)
tension.Resize(mesh->vertexCount);
for (u32 i = 0; i < mesh->vertexCount; ++i)
{
tension[i].SetZero();
}
for (u32 i = 0; i < m_cloth->GetSpringCount(); ++i)
for (b3Force* f = m_cloth->GetForceList().m_head; f; f = f->GetNext())
{
b3Spring* s = m_cloth->GetSpring(i);
if (f->GetType() == e_springForce)
{
b3SpringForce* s = (b3SpringForce*)f;
b3Particle* p1 = s->p1;
b3Particle* p2 = s->p2;
u32 v1 = s->GetParticle1()->GetVertex();
u32 v2 = s->GetParticle2()->GetVertex();
u32 i1 = m_cloth->GetParticleIndex(p1);
u32 i2 = m_cloth->GetParticleIndex(p2);
tension[i1] += s->f;
tension[i2] -= s->f;
tension[v1] += s->GetActionForce();
tension[v2] -= s->GetActionForce();
}
}
for (u32 i = 0; i < m_gridClothMesh.triangleCount; ++i)
for (u32 i = 0; i < m_rectangleClothMesh.triangleCount; ++i)
{
b3ClothMeshTriangle* t = m_gridClothMesh.triangles + i;
b3ClothMeshTriangle* t = m_rectangleClothMesh.triangles + i;
b3Particle* p1 = m_cloth->GetParticle(t->v1);
b3Particle* p2 = m_cloth->GetParticle(t->v2);
b3Particle* p3 = m_cloth->GetParticle(t->v3);
b3Vec3 v1 = p1->position;
b3Vec3 v2 = p2->position;
b3Vec3 v3 = p3->position;
b3Vec3 v1 = mesh->vertices[t->v1];
b3Vec3 v2 = mesh->vertices[t->v2];
b3Vec3 v3 = mesh->vertices[t->v3];
b3Draw_draw->DrawSegment(v1, v2, b3Color_black);
b3Draw_draw->DrawSegment(v2, v3, b3Color_black);
@ -183,9 +174,9 @@ public:
return new TensionMapping();
}
b3GridMesh<10, 10> m_gridMesh;
b3ClothMeshMesh m_gridClothMeshMesh;
b3ClothMesh m_gridClothMesh;
b3RectangleGarment m_rectangleGarment;
b3GarmentMesh m_rectangleGarmentMesh;
b3GarmentClothMesh m_rectangleClothMesh;
};
#endif