Add b3GridClothMesh. Set particle friction to global cloth friction. Update the tests. Also simplified some code.
This commit is contained in:
@ -64,6 +64,7 @@
|
||||
#include <bounce/rope/rope.h>
|
||||
|
||||
#include <bounce/cloth/cloth_mesh.h>
|
||||
#include <bounce/cloth/grid_cloth_mesh.h>
|
||||
#include <bounce/cloth/cloth.h>
|
||||
#include <bounce/cloth/particle.h>
|
||||
#include <bounce/cloth/cloth_triangle.h>
|
||||
|
96
include/bounce/cloth/grid_cloth_mesh.h
Normal file
96
include/bounce/cloth/grid_cloth_mesh.h
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2019 Irlan Robson https://irlanrobson.github.io
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B3_GRID_CLOTH_MESH_H
|
||||
#define B3_GRID_CLOTH_MESH_H
|
||||
|
||||
#include <bounce/cloth/cloth_mesh.h>
|
||||
|
||||
// A (H + 1) x (W + 1) grid mesh stored in row-major order.
|
||||
// v(i, j) = i + (W + 1) + j
|
||||
template<u32 W = 1, u32 H = 1>
|
||||
struct b3GridClothMesh : public b3ClothMesh
|
||||
{
|
||||
b3Vec3 gridVertices[(H + 1) * (W + 1)];
|
||||
b3ClothMeshTriangle gridTriangles[2 * H * W];
|
||||
b3ClothMeshMesh gridMesh;
|
||||
|
||||
// Set this grid to a W (width) per H (height) dimensioned grid centered at the origin and aligned
|
||||
// with the world x-z axes.
|
||||
b3GridClothMesh()
|
||||
{
|
||||
vertexCount = 0;
|
||||
for (u32 i = 0; i <= H; ++i)
|
||||
{
|
||||
for (u32 j = 0; j <= W; ++j)
|
||||
{
|
||||
gridVertices[vertexCount++].Set(float32(j), 0.0f, float32(i));
|
||||
}
|
||||
}
|
||||
|
||||
B3_ASSERT(vertexCount == (W + 1) * (H + 1));
|
||||
|
||||
b3Vec3 translation;
|
||||
translation.x = -0.5f * float32(W);
|
||||
translation.y = 0.0f;
|
||||
translation.z = -0.5f * float32(H);
|
||||
|
||||
for (u32 i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
gridVertices[i] += translation;
|
||||
}
|
||||
|
||||
triangleCount = 0;
|
||||
for (u32 i = 0; i < H; ++i)
|
||||
{
|
||||
for (u32 j = 0; j < W; ++j)
|
||||
{
|
||||
u32 v1 = i * (W + 1) + j;
|
||||
u32 v2 = (i + 1) * (W + 1) + j;
|
||||
u32 v3 = (i + 1) * (W + 1) + (j + 1);
|
||||
u32 v4 = i * (W + 1) + (j + 1);
|
||||
|
||||
b3ClothMeshTriangle* t1 = gridTriangles + triangleCount++;
|
||||
t1->v1 = v3;
|
||||
t1->v2 = v2;
|
||||
t1->v3 = v1;
|
||||
|
||||
b3ClothMeshTriangle* t2 = gridTriangles + triangleCount++;
|
||||
t2->v1 = v1;
|
||||
t2->v2 = v4;
|
||||
t2->v3 = v3;
|
||||
}
|
||||
}
|
||||
|
||||
B3_ASSERT(triangleCount == 2 * H * W);
|
||||
|
||||
gridMesh.startTriangle = 0;
|
||||
gridMesh.triangleCount = triangleCount;
|
||||
gridMesh.startVertex = 0;
|
||||
gridMesh.vertexCount = vertexCount;
|
||||
|
||||
vertices = gridVertices;
|
||||
triangles = gridTriangles;
|
||||
meshCount = 1;
|
||||
meshes = &gridMesh;
|
||||
sewingLineCount = 0;
|
||||
sewingLines = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
@ -22,6 +22,7 @@
|
||||
#include <bounce/collision/shapes/mesh.h>
|
||||
|
||||
// A (H + 1) x (W + 1) grid mesh stored in row-major order.
|
||||
// v(i, j) = i * (W + 1) + j
|
||||
template<u32 H = 1, u32 W = 1>
|
||||
struct b3GridMesh : public b3Mesh
|
||||
{
|
||||
@ -32,61 +33,53 @@ struct b3GridMesh : public b3Mesh
|
||||
// with the world x-z axes.
|
||||
b3GridMesh()
|
||||
{
|
||||
u32 h = H + 1;
|
||||
u32 w = W + 1;
|
||||
|
||||
b3Vec3 t;
|
||||
t.x = -0.5f * float32(w) + 0.5f;
|
||||
t.y = 0.0f;
|
||||
t.z = -0.5f * float32(h) + 0.5f;
|
||||
|
||||
for (u32 i = 0; i < h; ++i)
|
||||
vertexCount = 0;
|
||||
for (u32 i = 0; i <= H; ++i)
|
||||
{
|
||||
for (u32 j = 0; j < w; ++j)
|
||||
for (u32 j = 0; j <= W; ++j)
|
||||
{
|
||||
u32 v1 = i * w + j;
|
||||
|
||||
b3Vec3 v;
|
||||
v.x = float32(j);
|
||||
v.y = 0.0f;
|
||||
v.z = float32(i);
|
||||
|
||||
v += t;
|
||||
|
||||
gridVertices[v1] = v;
|
||||
gridVertices[vertexCount++].Set(float32(j), 0.0f, float32(i));
|
||||
}
|
||||
}
|
||||
|
||||
u32 triangleIndex = 0;
|
||||
for (u32 i = 0; i < h - 1; ++i)
|
||||
B3_ASSERT(vertexCount == (H + 1) * (W + 1));
|
||||
|
||||
b3Vec3 translation;
|
||||
translation.x = -0.5f * float32(W);
|
||||
translation.y = 0.0f;
|
||||
translation.z = -0.5f * float32(H);
|
||||
|
||||
for (u32 i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
for (u32 j = 0; j < w - 1; ++j)
|
||||
gridVertices[i] += translation;
|
||||
}
|
||||
|
||||
triangleCount = 0;
|
||||
for (u32 i = 0; i < H; ++i)
|
||||
{
|
||||
for (u32 j = 0; j < W; ++j)
|
||||
{
|
||||
u32 v1 = i * w + j;
|
||||
u32 v2 = (i + 1) * w + j;
|
||||
u32 v3 = (i + 1) * w + (j + 1);
|
||||
u32 v4 = i * w + (j + 1);
|
||||
|
||||
b3Triangle* t1 = gridTriangles + triangleIndex;
|
||||
++triangleIndex;
|
||||
u32 v1 = i * (W + 1) + j;
|
||||
u32 v2 = (i + 1) * (W + 1) + j;
|
||||
u32 v3 = (i + 1) * (W + 1) + (j + 1);
|
||||
u32 v4 = i * (W + 1) + (j + 1);
|
||||
|
||||
b3Triangle* t1 = gridTriangles + triangleCount++;
|
||||
t1->v1 = v3;
|
||||
t1->v2 = v2;
|
||||
t1->v3 = v1;
|
||||
|
||||
b3Triangle* t2 = gridTriangles + triangleIndex;
|
||||
++triangleIndex;
|
||||
|
||||
b3Triangle* t2 = gridTriangles + triangleCount++;
|
||||
t2->v1 = v1;
|
||||
t2->v2 = v4;
|
||||
t2->v3 = v3;
|
||||
}
|
||||
}
|
||||
|
||||
B3_ASSERT(triangleCount == 2 * H * W);
|
||||
|
||||
vertices = gridVertices;
|
||||
vertexCount = (H + 1) * (W + 1);
|
||||
triangles = gridTriangles;
|
||||
triangleCount = 2 * H * W;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -36,7 +36,7 @@ struct b3BlockSoftBodyMesh : public b3SoftBodyMesh
|
||||
{
|
||||
for (u32 z = 0; z <= D; ++z)
|
||||
{
|
||||
blockVertices[vertexCount++].Set(x, y, z);
|
||||
blockVertices[vertexCount++].Set(float32(x), float32(y), float32(z));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -141,7 +141,7 @@ public:
|
||||
// Debug draw the body using the associated mesh.
|
||||
void Draw() const;
|
||||
private:
|
||||
friend class b3SoftBodyNode;
|
||||
friend struct b3SoftBodyNode;
|
||||
friend class b3SoftBodySolver;
|
||||
friend class b3SoftBodyForceSolver;
|
||||
|
||||
|
Reference in New Issue
Block a user