diff --git a/include/bounce/softbody/softbody_mesh.h b/include/bounce/softbody/softbody_mesh.h index e7f392a..4ffc686 100644 --- a/include/bounce/softbody/softbody_mesh.h +++ b/include/bounce/softbody/softbody_mesh.h @@ -40,6 +40,8 @@ struct b3QSoftBodyMesh : public b3SoftBodyMesh ~b3QSoftBodyMesh(); void SetAsSphere(float32 radius, u32 subdivisions); + + void SetAsCylinder(float32 radius, float32 ey, u32 segments); }; #endif \ No newline at end of file diff --git a/src/bounce/softbody/softbody_mesh.cpp b/src/bounce/softbody/softbody_mesh.cpp index 2dd3135..995e12a 100644 --- a/src/bounce/softbody/softbody_mesh.cpp +++ b/src/bounce/softbody/softbody_mesh.cpp @@ -18,6 +18,7 @@ #include #include +#include b3QSoftBodyMesh::b3QSoftBodyMesh() { @@ -38,6 +39,7 @@ void b3QSoftBodyMesh::SetAsSphere(float32 radius, u32 subdivisions) smMesh mesh; smCreateMesh(mesh, subdivisions); + B3_ASSERT(vertexCount == 0); vertexCount = 1 + mesh.vertexCount; vertices = (b3Vec3*)b3Alloc(vertexCount * sizeof(b3Vec3)); vertices[0].SetZero(); @@ -46,6 +48,7 @@ void b3QSoftBodyMesh::SetAsSphere(float32 radius, u32 subdivisions) vertices[1 + i] = mesh.vertices[i]; } + B3_ASSERT(tetrahedronCount == 0); tetrahedronCount = mesh.indexCount / 3; tetrahedrons = (b3SoftBodyMeshTetrahedron*)b3Alloc(tetrahedronCount * sizeof(b3SoftBodyMeshTetrahedron)); for (u32 i = 0; i < mesh.indexCount / 3; ++i) @@ -66,4 +69,45 @@ void b3QSoftBodyMesh::SetAsSphere(float32 radius, u32 subdivisions) { vertices[i] *= radius; } +} + +void b3QSoftBodyMesh::SetAsCylinder(float32 radius, float32 ey, u32 segments) +{ + cymMesh mesh; + cymCreateMesh(mesh, segments); + + B3_ASSERT(vertexCount == 0); + vertexCount = 1 + mesh.vertexCount; + vertices = (b3Vec3*)b3Alloc(vertexCount * sizeof(b3Vec3)); + vertices[0].SetZero(); + for (u32 i = 0; i < mesh.vertexCount; ++i) + { + vertices[1 + i] = mesh.vertices[i]; + } + + B3_ASSERT(tetrahedronCount == 0); + tetrahedronCount = mesh.indexCount / 3; + tetrahedrons = (b3SoftBodyMeshTetrahedron*)b3Alloc(tetrahedronCount * sizeof(b3SoftBodyMeshTetrahedron)); + for (u32 i = 0; i < mesh.indexCount / 3; ++i) + { + u32 v1 = mesh.indices[3 * i + 0]; + u32 v2 = mesh.indices[3 * i + 1]; + u32 v3 = mesh.indices[3 * i + 2]; + + b3SoftBodyMeshTetrahedron* t = tetrahedrons + i; + + t->v1 = 1 + v3; + t->v2 = 1 + v2; + t->v3 = 1 + v1; + t->v4 = 0; + } + + float32 height = 2.0f * ey; + + for (u32 i = 0; i < vertexCount; ++i) + { + vertices[i].x *= radius; + vertices[i].y *= height; + vertices[i].z *= radius; + } } \ No newline at end of file