New feature: soft bodies!
This commit is contained in:
128
examples/testbed/framework/softbody_dragger.cpp
Normal file
128
examples/testbed/framework/softbody_dragger.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <testbed/framework/softbody_dragger.h>
|
||||
|
||||
b3SoftBodyDragger::b3SoftBodyDragger(b3Ray3* ray, b3SoftBody* body)
|
||||
{
|
||||
m_ray = ray;
|
||||
m_body = body;
|
||||
m_tetrahedron = nullptr;
|
||||
}
|
||||
|
||||
b3SoftBodyDragger::~b3SoftBodyDragger()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool b3SoftBodyDragger::StartDragging()
|
||||
{
|
||||
B3_ASSERT(IsDragging() == false);
|
||||
|
||||
b3SoftBodyRayCastSingleOutput rayOut;
|
||||
if (m_body->RayCastSingle(&rayOut, m_ray->A(), m_ray->B()) == false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_mesh = m_body->GetMesh();
|
||||
m_tetrahedron = m_mesh->tetrahedrons + rayOut.tetrahedron;
|
||||
m_v1 = m_tetrahedron->v1;
|
||||
m_v2 = m_tetrahedron->v2;
|
||||
m_v3 = m_tetrahedron->v3;
|
||||
m_v4 = m_tetrahedron->v4;
|
||||
m_x = rayOut.fraction;
|
||||
|
||||
b3SoftBodyNode* n1 = m_body->GetVertexNode(m_v1);
|
||||
b3SoftBodyNode* n2 = m_body->GetVertexNode(m_v2);
|
||||
b3SoftBodyNode* n3 = m_body->GetVertexNode(m_v3);
|
||||
b3SoftBodyNode* n4 = m_body->GetVertexNode(m_v4);
|
||||
|
||||
b3Vec3 v1 = n1->GetPosition();
|
||||
b3Vec3 v2 = n2->GetPosition();
|
||||
b3Vec3 v3 = n3->GetPosition();
|
||||
b3Vec3 v4 = n4->GetPosition();
|
||||
|
||||
b3Vec3 B = GetPointB();
|
||||
|
||||
float32 wABCD[5];
|
||||
b3BarycentricCoordinates(wABCD, v1, v2, v3, v4, B);
|
||||
|
||||
if (wABCD[4] > B3_EPSILON)
|
||||
{
|
||||
m_tu = wABCD[0] / wABCD[4];
|
||||
m_tv = wABCD[1] / wABCD[4];
|
||||
m_tw = wABCD[2] / wABCD[4];
|
||||
m_tx = wABCD[3] / wABCD[4];
|
||||
}
|
||||
else
|
||||
{
|
||||
m_tu = m_tv = m_tw = m_tx = 0.0f;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void b3SoftBodyDragger::Drag()
|
||||
{
|
||||
B3_ASSERT(IsDragging() == true);
|
||||
|
||||
b3Vec3 A = GetPointA();
|
||||
b3Vec3 B = GetPointB();
|
||||
|
||||
b3Vec3 dx = B - A;
|
||||
|
||||
const float32 k = 100.0f;
|
||||
|
||||
b3Vec3 f = k * dx;
|
||||
|
||||
b3Vec3 f1 = m_tu * f;
|
||||
b3Vec3 f2 = m_tv * f;
|
||||
b3Vec3 f3 = m_tw * f;
|
||||
b3Vec3 f4 = m_tx * f;
|
||||
|
||||
m_body->GetVertexNode(m_v1)->ApplyForce(f1);
|
||||
m_body->GetVertexNode(m_v2)->ApplyForce(f2);
|
||||
m_body->GetVertexNode(m_v3)->ApplyForce(f3);
|
||||
m_body->GetVertexNode(m_v4)->ApplyForce(f4);
|
||||
}
|
||||
|
||||
void b3SoftBodyDragger::StopDragging()
|
||||
{
|
||||
B3_ASSERT(IsDragging() == true);
|
||||
|
||||
m_tetrahedron = nullptr;
|
||||
}
|
||||
|
||||
b3Vec3 b3SoftBodyDragger::GetPointA() const
|
||||
{
|
||||
B3_ASSERT(IsDragging() == true);
|
||||
|
||||
b3Vec3 A = m_body->GetVertexNode(m_v1)->GetPosition();
|
||||
b3Vec3 B = m_body->GetVertexNode(m_v2)->GetPosition();
|
||||
b3Vec3 C = m_body->GetVertexNode(m_v3)->GetPosition();
|
||||
b3Vec3 D = m_body->GetVertexNode(m_v4)->GetPosition();
|
||||
|
||||
return m_tu * A + m_tv * B + m_tw * C + m_tx * D;
|
||||
}
|
||||
|
||||
b3Vec3 b3SoftBodyDragger::GetPointB() const
|
||||
{
|
||||
B3_ASSERT(IsDragging() == true);
|
||||
return (1.0f - m_x) * m_ray->A() + m_x * m_ray->B();
|
||||
}
|
61
examples/testbed/framework/softbody_dragger.h
Normal file
61
examples/testbed/framework/softbody_dragger.h
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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_SOFTBODY_DRAGGER_H
|
||||
#define B3_SOFTBODY_DRAGGER_H
|
||||
|
||||
#include <bounce/common/geometry.h>
|
||||
#include <bounce/softbody/softbody.h>
|
||||
#include <bounce/softbody/softbody_mesh.h>
|
||||
#include <bounce/softbody/softbody_node.h>
|
||||
|
||||
// A soft body triangle dragger.
|
||||
class b3SoftBodyDragger
|
||||
{
|
||||
public:
|
||||
b3SoftBodyDragger(b3Ray3* ray, b3SoftBody* body);
|
||||
~b3SoftBodyDragger();
|
||||
|
||||
bool IsDragging() const;
|
||||
|
||||
bool StartDragging();
|
||||
|
||||
void Drag();
|
||||
|
||||
void StopDragging();
|
||||
|
||||
b3Vec3 GetPointA() const;
|
||||
|
||||
b3Vec3 GetPointB() const;
|
||||
private:
|
||||
b3Ray3* m_ray;
|
||||
float32 m_x;
|
||||
|
||||
b3SoftBody* m_body;
|
||||
const b3SoftBodyMesh* m_mesh;
|
||||
const b3SoftBodyMeshTetrahedron* m_tetrahedron;
|
||||
u32 m_v1, m_v2, m_v3, m_v4;
|
||||
float32 m_tu, m_tv, m_tw, m_tx;
|
||||
};
|
||||
|
||||
inline bool b3SoftBodyDragger::IsDragging() const
|
||||
{
|
||||
return m_tetrahedron != nullptr;
|
||||
}
|
||||
|
||||
#endif
|
@ -65,8 +65,10 @@
|
||||
#include <testbed/tests/particle_types.h>
|
||||
#include <testbed/tests/tension_mapping.h>
|
||||
#include <testbed/tests/self_collision.h>
|
||||
#include <testbed/tests/soft_body.h>
|
||||
#include <testbed/tests/rope_test.h>
|
||||
#include <testbed/tests/softbody.h>
|
||||
#include <testbed/tests/pinned_softbody.h>
|
||||
#include <testbed/tests/smash_softbody.h>
|
||||
|
||||
TestEntry g_tests[] =
|
||||
{
|
||||
@ -118,6 +120,8 @@ TestEntry g_tests[] =
|
||||
{ "Tension Mapping", &TensionMapping::Create },
|
||||
{ "Self-Collision", &SelfCollision::Create },
|
||||
{ "Soft Body", &SoftBody::Create },
|
||||
{ "Pinned Soft Body", &PinnedSoftBody::Create },
|
||||
{ "Smash Soft Body", &SmashSoftBody::Create },
|
||||
{ "Rope", &Rope::Create },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
135
examples/testbed/tests/pinned_softbody.h
Normal file
135
examples/testbed/tests/pinned_softbody.h
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* 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 PINNED_SOFTBODY_H
|
||||
#define PINNED_SOFTBODY_H
|
||||
|
||||
#include <testbed/framework/softbody_dragger.h>
|
||||
|
||||
class PinnedSoftBody : public Test
|
||||
{
|
||||
public:
|
||||
PinnedSoftBody()
|
||||
{
|
||||
m_mesh.SetAsSphere(5.0f, 0);
|
||||
|
||||
// Create soft body
|
||||
b3SoftBodyDef def;
|
||||
def.mesh = &m_mesh;
|
||||
def.density = 0.2f;
|
||||
def.E = 100.0f;
|
||||
def.nu = 0.33f;
|
||||
|
||||
m_body = new b3SoftBody(def);
|
||||
|
||||
u32 pinIndex = ~0;
|
||||
float32 pinDot = -B3_MAX_FLOAT;
|
||||
for (u32 i = 0; i < m_mesh.vertexCount; ++i)
|
||||
{
|
||||
float32 dot = b3Dot(m_mesh.vertices[i], b3Vec3_y);
|
||||
if (dot > pinDot)
|
||||
{
|
||||
pinDot = dot;
|
||||
pinIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
b3SoftBodyNode* pinNode = m_body->GetVertexNode(pinIndex);
|
||||
pinNode->SetType(e_staticSoftBodyNode);
|
||||
|
||||
b3Vec3 gravity(0.0f, -9.8f, 0.0f);
|
||||
m_body->SetGravity(gravity);
|
||||
|
||||
m_bodyDragger = new b3SoftBodyDragger(&m_ray, m_body);
|
||||
}
|
||||
|
||||
~PinnedSoftBody()
|
||||
{
|
||||
delete m_bodyDragger;
|
||||
delete m_body;
|
||||
}
|
||||
|
||||
void Step()
|
||||
{
|
||||
Test::Step();
|
||||
|
||||
if (m_bodyDragger->IsDragging())
|
||||
{
|
||||
m_bodyDragger->Drag();
|
||||
}
|
||||
|
||||
m_body->Step(g_testSettings->inv_hertz, g_testSettings->velocityIterations, g_testSettings->positionIterations);
|
||||
|
||||
m_body->Draw();
|
||||
|
||||
if (m_bodyDragger->IsDragging())
|
||||
{
|
||||
b3Vec3 pA = m_bodyDragger->GetPointA();
|
||||
b3Vec3 pB = m_bodyDragger->GetPointB();
|
||||
|
||||
g_draw->DrawPoint(pA, 2.0f, b3Color_green);
|
||||
|
||||
g_draw->DrawPoint(pB, 2.0f, b3Color_green);
|
||||
|
||||
g_draw->DrawSegment(pA, pB, b3Color_white);
|
||||
}
|
||||
|
||||
extern u32 b3_softBodySolverIterations;
|
||||
g_draw->DrawString(b3Color_white, "Iterations = %d", b3_softBodySolverIterations);
|
||||
|
||||
float32 E = m_body->GetEnergy();
|
||||
g_draw->DrawString(b3Color_white, "E = %f", E);
|
||||
}
|
||||
|
||||
void MouseMove(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseMove(pw);
|
||||
}
|
||||
|
||||
void MouseLeftDown(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftDown(pw);
|
||||
|
||||
if (m_bodyDragger->IsDragging() == false)
|
||||
{
|
||||
m_bodyDragger->StartDragging();
|
||||
}
|
||||
}
|
||||
|
||||
void MouseLeftUp(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftUp(pw);
|
||||
|
||||
if (m_bodyDragger->IsDragging() == true)
|
||||
{
|
||||
m_bodyDragger->StopDragging();
|
||||
}
|
||||
}
|
||||
|
||||
static Test* Create()
|
||||
{
|
||||
return new PinnedSoftBody();
|
||||
}
|
||||
|
||||
b3QSoftBodyMesh m_mesh;
|
||||
|
||||
b3SoftBody* m_body;
|
||||
b3SoftBodyDragger* m_bodyDragger;
|
||||
};
|
||||
|
||||
#endif
|
172
examples/testbed/tests/smash_softbody.h
Normal file
172
examples/testbed/tests/smash_softbody.h
Normal file
@ -0,0 +1,172 @@
|
||||
/*
|
||||
* 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 SMASH_SOFTBODY_H
|
||||
#define SMASH_SOFTBODY_H
|
||||
|
||||
#include <testbed/framework/softbody_dragger.h>
|
||||
|
||||
class SmashSoftBody : public Test
|
||||
{
|
||||
public:
|
||||
SmashSoftBody()
|
||||
{
|
||||
m_mesh.SetAsSphere(2.0f, 1);
|
||||
|
||||
for (u32 i = 0; i < m_mesh.vertexCount; ++i)
|
||||
{
|
||||
m_mesh.vertices[i].y += 3.0f;
|
||||
}
|
||||
|
||||
// Create soft body
|
||||
b3SoftBodyDef def;
|
||||
def.mesh = &m_mesh;
|
||||
def.density = 0.2f;
|
||||
def.E = 100.0f;
|
||||
def.nu = 0.33f;
|
||||
|
||||
m_body = new b3SoftBody(def);
|
||||
|
||||
b3Vec3 gravity(0.0f, -9.8f, 0.0f);
|
||||
m_body->SetGravity(gravity);
|
||||
m_body->SetWorld(&m_world);
|
||||
|
||||
for (u32 i = 0; i < m_mesh.vertexCount; ++i)
|
||||
{
|
||||
b3SoftBodyNode* n = m_body->GetVertexNode(i);
|
||||
|
||||
n->SetRadius(0.05f);
|
||||
n->SetFriction(0.2f);
|
||||
}
|
||||
|
||||
// Create ground
|
||||
{
|
||||
b3BodyDef bd;
|
||||
bd.type = e_staticBody;
|
||||
|
||||
b3Body* b = m_world.CreateBody(bd);
|
||||
|
||||
b3HullShape groundShape;
|
||||
groundShape.m_hull = &m_groundHull;
|
||||
|
||||
b3ShapeDef sd;
|
||||
sd.shape = &groundShape;
|
||||
sd.friction = 0.3f;
|
||||
|
||||
b->CreateShape(sd);
|
||||
}
|
||||
|
||||
// Create body
|
||||
{
|
||||
b3BodyDef bd;
|
||||
bd.type = e_dynamicBody;
|
||||
bd.position.y = 10.0f;
|
||||
|
||||
b3Body* b = m_world.CreateBody(bd);
|
||||
|
||||
static b3BoxHull boxHull(5.0f, 1.0f, 5.0f);
|
||||
|
||||
b3HullShape boxShape;
|
||||
boxShape.m_hull = &boxHull;
|
||||
|
||||
b3ShapeDef sd;
|
||||
sd.shape = &boxShape;
|
||||
sd.density = 0.1f;
|
||||
sd.friction = 0.3f;
|
||||
|
||||
b->CreateShape(sd);
|
||||
}
|
||||
|
||||
m_bodyDragger = new b3SoftBodyDragger(&m_ray, m_body);
|
||||
}
|
||||
|
||||
~SmashSoftBody()
|
||||
{
|
||||
delete m_bodyDragger;
|
||||
delete m_body;
|
||||
}
|
||||
|
||||
void Step()
|
||||
{
|
||||
Test::Step();
|
||||
|
||||
if (m_bodyDragger->IsDragging())
|
||||
{
|
||||
m_bodyDragger->Drag();
|
||||
}
|
||||
|
||||
m_body->Step(g_testSettings->inv_hertz, g_testSettings->velocityIterations, g_testSettings->positionIterations);
|
||||
|
||||
m_body->Draw();
|
||||
|
||||
if (m_bodyDragger->IsDragging())
|
||||
{
|
||||
b3Vec3 pA = m_bodyDragger->GetPointA();
|
||||
b3Vec3 pB = m_bodyDragger->GetPointB();
|
||||
|
||||
g_draw->DrawPoint(pA, 2.0f, b3Color_green);
|
||||
|
||||
g_draw->DrawPoint(pB, 2.0f, b3Color_green);
|
||||
|
||||
g_draw->DrawSegment(pA, pB, b3Color_white);
|
||||
}
|
||||
|
||||
extern u32 b3_softBodySolverIterations;
|
||||
g_draw->DrawString(b3Color_white, "Iterations = %d", b3_softBodySolverIterations);
|
||||
|
||||
float32 E = m_body->GetEnergy();
|
||||
g_draw->DrawString(b3Color_white, "E = %f", E);
|
||||
}
|
||||
|
||||
void MouseMove(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseMove(pw);
|
||||
}
|
||||
|
||||
void MouseLeftDown(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftDown(pw);
|
||||
|
||||
if (m_bodyDragger->IsDragging() == false)
|
||||
{
|
||||
m_bodyDragger->StartDragging();
|
||||
}
|
||||
}
|
||||
|
||||
void MouseLeftUp(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftUp(pw);
|
||||
|
||||
if (m_bodyDragger->IsDragging() == true)
|
||||
{
|
||||
m_bodyDragger->StopDragging();
|
||||
}
|
||||
}
|
||||
|
||||
static Test* Create()
|
||||
{
|
||||
return new SmashSoftBody();
|
||||
}
|
||||
|
||||
b3QSoftBodyMesh m_mesh;
|
||||
|
||||
b3SoftBody* m_body;
|
||||
b3SoftBodyDragger* m_bodyDragger;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,293 +0,0 @@
|
||||
/*
|
||||
* 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 SOFT_BODY_H
|
||||
#define SOFT_BODY_H
|
||||
|
||||
#include <bounce/meshgen/sphere_mesh.h>
|
||||
|
||||
struct b3QClothMesh : public b3ClothMesh
|
||||
{
|
||||
b3StackArray<b3Vec3, 256> clothVertices;
|
||||
b3StackArray<b3ClothMeshTriangle, 256> clothTriangles;
|
||||
b3ClothMeshMesh clothMesh;
|
||||
|
||||
b3QClothMesh()
|
||||
{
|
||||
vertices = nullptr;
|
||||
vertexCount = 0;
|
||||
triangles = nullptr;
|
||||
triangleCount = 0;
|
||||
meshCount = 0;
|
||||
meshes = nullptr;
|
||||
sewingLineCount = 0;
|
||||
sewingLines = nullptr;
|
||||
}
|
||||
|
||||
void SetAsSphere(float32 radius = 1.0f)
|
||||
{
|
||||
smMesh mesh;
|
||||
smCreateMesh(mesh, 2);
|
||||
|
||||
clothVertices.Resize(mesh.vertexCount);
|
||||
for (u32 i = 0; i < mesh.vertexCount; ++i)
|
||||
{
|
||||
clothVertices[i] = radius * mesh.vertices[i];
|
||||
}
|
||||
|
||||
clothTriangles.Resize(mesh.indexCount / 3);
|
||||
for (u32 i = 0; i < mesh.indexCount / 3; ++i)
|
||||
{
|
||||
clothTriangles[i].v1 = mesh.indices[3 * i + 0];
|
||||
clothTriangles[i].v2 = mesh.indices[3 * i + 1];
|
||||
clothTriangles[i].v3 = mesh.indices[3 * i + 2];
|
||||
}
|
||||
|
||||
clothMesh.startTriangle = 0;
|
||||
clothMesh.triangleCount = clothTriangles.Count();
|
||||
clothMesh.startVertex = 0;
|
||||
clothMesh.vertexCount = clothVertices.Count();
|
||||
|
||||
vertices = clothVertices.Begin();
|
||||
vertexCount = clothVertices.Count();
|
||||
triangles = clothTriangles.Begin();
|
||||
triangleCount = clothTriangles.Count();
|
||||
meshCount = 1;
|
||||
meshes = &clothMesh;
|
||||
}
|
||||
};
|
||||
|
||||
class SoftBody : public Test
|
||||
{
|
||||
public:
|
||||
SoftBody()
|
||||
{
|
||||
m_mesh.SetAsSphere(2.0f);
|
||||
|
||||
// Translate the cloth mesh upwards
|
||||
for (u32 i = 0; i < m_mesh.vertexCount; ++i)
|
||||
{
|
||||
m_mesh.vertices[i].y += 10.0f;
|
||||
}
|
||||
|
||||
// Create cloth
|
||||
b3ClothDef def;
|
||||
def.mesh = &m_mesh;
|
||||
def.density = 0.2f;
|
||||
def.structural = 10000.0f;
|
||||
def.bending = 0.0f;
|
||||
def.damping = 0.0f;
|
||||
|
||||
m_cloth = new b3Cloth(def);
|
||||
|
||||
for (b3Particle* p = m_cloth->GetParticleList().m_head; p; p = p->GetNext())
|
||||
{
|
||||
p->SetRadius(0.05f);
|
||||
p->SetFriction(0.5f);
|
||||
}
|
||||
|
||||
b3Vec3 gravity(0.0f, -9.8f, 0.0f);
|
||||
|
||||
m_cloth->SetGravity(gravity);
|
||||
|
||||
// Attach a world to the cloth
|
||||
m_cloth->SetWorld(&m_world);
|
||||
|
||||
// Create static shapes
|
||||
{
|
||||
b3BodyDef bd;
|
||||
bd.type = e_staticBody;
|
||||
|
||||
b3Body* b = m_world.CreateBody(bd);
|
||||
|
||||
static b3BoxHull boxHull(10.0f, 1.0f, 10.0f);
|
||||
|
||||
b3HullShape boxShape;
|
||||
boxShape.m_hull = &boxHull;
|
||||
|
||||
b3ShapeDef sd;
|
||||
sd.shape = &boxShape;
|
||||
sd.friction = 0.5f;
|
||||
|
||||
b->CreateShape(sd);
|
||||
}
|
||||
|
||||
m_clothDragger = new b3ClothDragger(&m_ray, m_cloth);
|
||||
}
|
||||
|
||||
~SoftBody()
|
||||
{
|
||||
delete m_clothDragger;
|
||||
delete m_cloth;
|
||||
}
|
||||
|
||||
// Compute the volume of soft body
|
||||
float32 ComputeVolume() const
|
||||
{
|
||||
const b3ClothMesh* mesh = m_cloth->GetMesh();
|
||||
|
||||
b3StackArray<b3Vec3, 256> positions;
|
||||
positions.Resize(mesh->vertexCount);
|
||||
|
||||
for (u32 i = 0; i < mesh->vertexCount; ++i)
|
||||
{
|
||||
b3Particle* p = m_cloth->GetVertexParticle(i);
|
||||
positions[i] = p->GetPosition();
|
||||
}
|
||||
|
||||
b3AABB3 aabb;
|
||||
aabb.Compute(positions.Begin(), positions.Count());
|
||||
|
||||
return aabb.Volume();
|
||||
}
|
||||
|
||||
// Apply pressure forces
|
||||
// Explanation available in the paper
|
||||
// "Pressure Model of Soft Body Simulation"
|
||||
void ApplyPressureForces()
|
||||
{
|
||||
const b3ClothMesh* mesh = m_cloth->GetMesh();
|
||||
|
||||
// Volume in m^3
|
||||
float32 V = ComputeVolume();
|
||||
|
||||
// Inverse volume
|
||||
float32 invV = V > 0.0f ? 1.0f / V : 0.0f;
|
||||
|
||||
// Apply pressure forces on particles
|
||||
for (u32 i = 0; i < m_mesh.triangleCount; ++i)
|
||||
{
|
||||
u32 i1 = m_mesh.triangles[i].v1;
|
||||
u32 i2 = m_mesh.triangles[i].v2;
|
||||
u32 i3 = m_mesh.triangles[i].v3;
|
||||
|
||||
b3Particle* p1 = m_cloth->GetVertexParticle(i1);
|
||||
b3Particle* p2 = m_cloth->GetVertexParticle(i2);
|
||||
b3Particle* p3 = m_cloth->GetVertexParticle(i3);
|
||||
|
||||
b3Vec3 v1 = p1->GetPosition();
|
||||
b3Vec3 v2 = p2->GetPosition();
|
||||
b3Vec3 v3 = p3->GetPosition();
|
||||
|
||||
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
|
||||
|
||||
// Triangle area
|
||||
float32 A = n.Normalize();
|
||||
A *= 0.5f;
|
||||
|
||||
// Ideal Gas Approximation
|
||||
|
||||
// Number of gas moles
|
||||
const float32 k_n = 1.0f;
|
||||
|
||||
// Ideal Gas Constant [J / K mol]
|
||||
const float32 k_R = 8.31f;
|
||||
|
||||
// Gas temperature in Kelvin
|
||||
const float32 k_T = 100.0f;
|
||||
|
||||
// Pressure in Pascals
|
||||
float32 P = invV * k_n * k_R * k_T;
|
||||
|
||||
// Pressure vector
|
||||
b3Vec3 Pn = P * n;
|
||||
|
||||
// Pressure force
|
||||
b3Vec3 FP = Pn * A;
|
||||
|
||||
const float32 inv3 = 1.0f / 3.0f;
|
||||
|
||||
b3Vec3 f = inv3 * FP;
|
||||
|
||||
// Distribute
|
||||
p1->ApplyForce(f);
|
||||
p2->ApplyForce(f);
|
||||
p3->ApplyForce(f);
|
||||
}
|
||||
}
|
||||
|
||||
void Step()
|
||||
{
|
||||
Test::Step();
|
||||
|
||||
ApplyPressureForces();
|
||||
|
||||
m_cloth->Step(g_testSettings->inv_hertz);
|
||||
|
||||
m_cloth->Draw();
|
||||
|
||||
if (m_clothDragger->IsDragging())
|
||||
{
|
||||
b3Vec3 pA = m_clothDragger->GetPointA();
|
||||
b3Vec3 pB = m_clothDragger->GetPointB();
|
||||
|
||||
g_draw->DrawPoint(pA, 2.0f, b3Color_green);
|
||||
|
||||
g_draw->DrawPoint(pB, 2.0f, b3Color_green);
|
||||
|
||||
g_draw->DrawSegment(pA, pB, b3Color_white);
|
||||
}
|
||||
|
||||
extern u32 b3_clothSolverIterations;
|
||||
g_draw->DrawString(b3Color_white, "Iterations = %d", b3_clothSolverIterations);
|
||||
|
||||
float32 E = m_cloth->GetEnergy();
|
||||
g_draw->DrawString(b3Color_white, "E = %f", E);
|
||||
}
|
||||
|
||||
void MouseMove(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseMove(pw);
|
||||
|
||||
if (m_clothDragger->IsDragging() == true)
|
||||
{
|
||||
m_clothDragger->Drag();
|
||||
}
|
||||
}
|
||||
|
||||
void MouseLeftDown(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftDown(pw);
|
||||
|
||||
if (m_clothDragger->IsDragging() == false)
|
||||
{
|
||||
m_clothDragger->StartDragging();
|
||||
}
|
||||
}
|
||||
|
||||
void MouseLeftUp(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftUp(pw);
|
||||
|
||||
if (m_clothDragger->IsDragging() == true)
|
||||
{
|
||||
m_clothDragger->StopDragging();
|
||||
}
|
||||
}
|
||||
|
||||
static Test* Create()
|
||||
{
|
||||
return new SoftBody();
|
||||
}
|
||||
|
||||
b3QClothMesh m_mesh;
|
||||
b3Cloth* m_cloth;
|
||||
b3ClothDragger* m_clothDragger;
|
||||
};
|
||||
|
||||
#endif
|
155
examples/testbed/tests/softbody.h
Normal file
155
examples/testbed/tests/softbody.h
Normal file
@ -0,0 +1,155 @@
|
||||
/*
|
||||
* 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 SOFTBODY_H
|
||||
#define SOFTBODY_H
|
||||
|
||||
#include <testbed/framework/softbody_dragger.h>
|
||||
|
||||
class SoftBody : public Test
|
||||
{
|
||||
public:
|
||||
SoftBody()
|
||||
{
|
||||
m_mesh.SetAsSphere(5.0f, 1);
|
||||
|
||||
for (u32 i = 0; i < m_mesh.vertexCount; ++i)
|
||||
{
|
||||
m_mesh.vertices[i].y += 10.0f;
|
||||
}
|
||||
|
||||
// Create soft body
|
||||
b3SoftBodyDef def;
|
||||
def.mesh = &m_mesh;
|
||||
def.density = 0.2f;
|
||||
def.E = 100.0f;
|
||||
def.nu = 0.33f;
|
||||
|
||||
m_body = new b3SoftBody(def);
|
||||
|
||||
b3Vec3 gravity(0.0f, -9.8f, 0.0f);
|
||||
m_body->SetGravity(gravity);
|
||||
m_body->SetWorld(&m_world);
|
||||
|
||||
for (u32 i = 0; i < m_mesh.vertexCount; ++i)
|
||||
{
|
||||
b3SoftBodyNode* n = m_body->GetVertexNode(i);
|
||||
|
||||
n->SetRadius(0.05f);
|
||||
n->SetFriction(0.2f);
|
||||
}
|
||||
|
||||
// Create ground
|
||||
{
|
||||
b3BodyDef bd;
|
||||
bd.type = e_staticBody;
|
||||
|
||||
b3Body* b = m_world.CreateBody(bd);
|
||||
|
||||
m_groundHull.SetAsCylinder(20.0f, 2.0f);
|
||||
|
||||
b3HullShape groundShape;
|
||||
groundShape.m_hull = &m_groundHull;
|
||||
|
||||
b3ShapeDef sd;
|
||||
sd.shape = &groundShape;
|
||||
sd.friction = 0.3f;
|
||||
|
||||
b->CreateShape(sd);
|
||||
}
|
||||
|
||||
m_bodyDragger = new b3SoftBodyDragger(&m_ray, m_body);
|
||||
}
|
||||
|
||||
~SoftBody()
|
||||
{
|
||||
delete m_bodyDragger;
|
||||
delete m_body;
|
||||
}
|
||||
|
||||
void Step()
|
||||
{
|
||||
Test::Step();
|
||||
|
||||
if (m_bodyDragger->IsDragging())
|
||||
{
|
||||
m_bodyDragger->Drag();
|
||||
}
|
||||
|
||||
m_body->Step(g_testSettings->inv_hertz, g_testSettings->velocityIterations, g_testSettings->positionIterations);
|
||||
|
||||
m_body->Draw();
|
||||
|
||||
if (m_bodyDragger->IsDragging())
|
||||
{
|
||||
b3Vec3 pA = m_bodyDragger->GetPointA();
|
||||
b3Vec3 pB = m_bodyDragger->GetPointB();
|
||||
|
||||
g_draw->DrawPoint(pA, 2.0f, b3Color_green);
|
||||
|
||||
g_draw->DrawPoint(pB, 2.0f, b3Color_green);
|
||||
|
||||
g_draw->DrawSegment(pA, pB, b3Color_white);
|
||||
}
|
||||
|
||||
extern u32 b3_softBodySolverIterations;
|
||||
g_draw->DrawString(b3Color_white, "Iterations = %d", b3_softBodySolverIterations);
|
||||
|
||||
float32 E = m_body->GetEnergy();
|
||||
g_draw->DrawString(b3Color_white, "E = %f", E);
|
||||
}
|
||||
|
||||
void MouseMove(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseMove(pw);
|
||||
}
|
||||
|
||||
void MouseLeftDown(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftDown(pw);
|
||||
|
||||
if (m_bodyDragger->IsDragging() == false)
|
||||
{
|
||||
m_bodyDragger->StartDragging();
|
||||
}
|
||||
}
|
||||
|
||||
void MouseLeftUp(const b3Ray3& pw)
|
||||
{
|
||||
Test::MouseLeftUp(pw);
|
||||
|
||||
if (m_bodyDragger->IsDragging() == true)
|
||||
{
|
||||
m_bodyDragger->StopDragging();
|
||||
}
|
||||
}
|
||||
|
||||
static Test* Create()
|
||||
{
|
||||
return new SoftBody();
|
||||
}
|
||||
|
||||
b3QSoftBodyMesh m_mesh;
|
||||
|
||||
b3SoftBody* m_body;
|
||||
b3SoftBodyDragger* m_bodyDragger;
|
||||
|
||||
b3QHull m_groundHull;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user