Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Luke Benstead 2019-07-08 19:42:44 +01:00
commit d728d45d70
281 changed files with 11638 additions and 5756 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -0,0 +1,98 @@
/*
* 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/body_dragger.h>
b3BodyDragger::b3BodyDragger(b3Ray3* ray, b3World* world)
{
m_ray = ray;
m_world = world;
m_shape = nullptr;
m_mouseJoint = nullptr;
}
b3BodyDragger::~b3BodyDragger()
{
}
bool b3BodyDragger::StartDragging()
{
B3_ASSERT(IsDragging() == false);
b3RayCastSingleOutput out;
if (m_world->RayCastSingle(&out, m_ray->A(), m_ray->B()) == false)
{
return false;
}
m_x = out.fraction;
m_shape = out.shape;
b3BodyDef bd;
b3Body* groundBody = m_world->CreateBody(bd);
b3Body* body = m_shape->GetBody();
body->SetAwake(true);
b3MouseJointDef jd;
jd.bodyA = groundBody;
jd.bodyB = body;
jd.target = out.point;
jd.maxForce = 2000.0f * body->GetMass();
m_mouseJoint = (b3MouseJoint*)m_world->CreateJoint(jd);
m_p = body->GetLocalPoint(out.point);
return true;
}
void b3BodyDragger::Drag()
{
B3_ASSERT(IsDragging() == true);
m_mouseJoint->SetTarget(GetPointB());
}
void b3BodyDragger::StopDragging()
{
B3_ASSERT(IsDragging() == true);
b3Body* groundBody = m_mouseJoint->GetBodyA();
m_world->DestroyJoint(m_mouseJoint);
m_mouseJoint = nullptr;
m_world->DestroyBody(groundBody);
m_shape = nullptr;
}
b3Body* b3BodyDragger::GetBody() const
{
B3_ASSERT(IsDragging() == true);
return m_shape->GetBody();
}
b3Vec3 b3BodyDragger::GetPointA() const
{
B3_ASSERT(IsDragging() == true);
return m_shape->GetBody()->GetWorldPoint(m_p);
}
b3Vec3 b3BodyDragger::GetPointB() const
{
B3_ASSERT(IsDragging() == true);
return (1.0f - m_x) * m_ray->A() + m_x * m_ray->B();
}

View File

@ -0,0 +1,72 @@
/*
* 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_BODY_DRAGGER_H
#define B3_BODY_DRAGGER_H
#include <bounce/common/geometry.h>
#include <bounce/common/geometry.h>
#include <bounce/dynamics/shapes/shape.h>
#include <bounce/dynamics/body.h>
#include <bounce/dynamics/world.h>
#include <bounce/dynamics/joints/mouse_joint.h>
// A body shape dragger.
class b3BodyDragger
{
public:
b3BodyDragger(b3Ray3* ray, b3World* world);
~b3BodyDragger();
bool StartDragging();
void Drag();
void StopDragging();
bool IsDragging() const;
b3Ray3* GetRay() const;
b3Body* GetBody() const;
b3Vec3 GetPointA() const;
b3Vec3 GetPointB() const;
private:
b3Ray3 * m_ray;
float32 m_x;
b3World* m_world;
b3Shape* m_shape;
b3Vec3 m_p;
b3MouseJoint* m_mouseJoint;
};
inline bool b3BodyDragger::IsDragging() const
{
return m_shape != nullptr;
}
inline b3Ray3* b3BodyDragger::GetRay() const
{
return m_ray;
}
#endif

View File

@ -0,0 +1,185 @@
/*
* 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/cloth_dragger.h>
b3ClothDragger::b3ClothDragger(b3Ray3* ray, b3Cloth* cloth)
{
m_staticDrag = true;
m_ray = ray;
m_cloth = cloth;
m_triangle = nullptr;
m_km = 10000.0f;
m_kd = 0.0f;
}
b3ClothDragger::~b3ClothDragger()
{
}
bool b3ClothDragger::StartDragging()
{
B3_ASSERT(IsDragging() == false);
b3ClothRayCastSingleOutput rayOut;
if (m_cloth->RayCastSingle(&rayOut, m_ray->A(), m_ray->B()) == false)
{
return false;
}
m_mesh = m_cloth->GetMesh();
m_triangleIndex = rayOut.triangle;
m_triangle = m_mesh->triangles + m_triangleIndex;
m_x = rayOut.fraction;
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();
b3Vec3 v3 = p3->GetPosition();
b3Vec3 B = GetPointB();
float32 wABC[4];
b3BarycentricCoordinates(wABC, v1, v2, v3, B);
if (wABC[3] > B3_EPSILON)
{
m_u = wABC[0] / wABC[3];
m_v = wABC[1] / wABC[3];
}
else
{
m_u = m_v = 0.0f;
}
if (m_staticDrag)
{
m_t1 = p1->GetType();
p1->SetType(e_staticParticle);
m_t2 = p2->GetType();
p2->SetType(e_staticParticle);
m_t3 = p3->GetType();
p3->SetType(e_staticParticle);
}
else
{
b3ParticleDef pd;
pd.type = e_staticParticle;
pd.position = GetPointA();
m_particle = m_cloth->CreateParticle(pd);
b3ClothTriangle* triangle = m_cloth->GetTriangle(m_triangleIndex);
b3MouseForceDef def;
def.particle = m_particle;
def.triangle = triangle;
def.w2 = m_u;
def.w3 = m_v;
def.w4 = (1.0f - m_u - m_v);
def.mouse = m_km;
def.damping = m_kd;
m_mf = (b3MouseForce*)m_cloth->CreateForce(def);
}
return true;
}
void b3ClothDragger::Drag()
{
B3_ASSERT(IsDragging() == true);
b3Vec3 A = GetPointA();
b3Vec3 B = GetPointB();
b3Vec3 dx = B - A;
if (m_staticDrag)
{
b3Particle* p1 = m_cloth->GetParticle(m_triangle->v1);
p1->ApplyTranslation(dx);
b3Particle* p2 = m_cloth->GetParticle(m_triangle->v2);
p2->ApplyTranslation(dx);
b3Particle* p3 = m_cloth->GetParticle(m_triangle->v3);
p3->ApplyTranslation(dx);
}
else
{
m_particle->SetPosition(B);
}
}
void b3ClothDragger::SetStaticDrag(bool bit)
{
if (bit == m_staticDrag)
{
return;
}
if (IsDragging())
{
StopDragging();
}
m_staticDrag = bit;
}
void b3ClothDragger::StopDragging()
{
B3_ASSERT(IsDragging() == true);
if (m_staticDrag)
{
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);
}
else
{
m_cloth->DestroyForce(m_mf);
m_cloth->DestroyParticle(m_particle);
}
m_triangle = nullptr;
}
b3Vec3 b3ClothDragger::GetPointA() const
{
B3_ASSERT(IsDragging() == true);
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;
}
b3Vec3 b3ClothDragger::GetPointB() const
{
B3_ASSERT(IsDragging() == true);
return (1.0f - m_x) * m_ray->A() + m_x * m_ray->B();
}

View File

@ -0,0 +1,108 @@
/*
* 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_CLOTH_DRAGGER_H
#define B3_CLOTH_DRAGGER_H
#include <bounce/common/geometry.h>
#include <bounce/cloth/cloth.h>
#include <bounce/cloth/cloth_mesh.h>
#include <bounce/cloth/particle.h>
#include <bounce/cloth/cloth_triangle.h>
#include <bounce/cloth/forces/mouse_force.h>
// A cloth triangle dragger.
class b3ClothDragger
{
public:
b3ClothDragger(b3Ray3* ray, b3Cloth* cloth);
~b3ClothDragger();
void SetStaticDrag(bool bit);
bool GetStaticDrag() const;
void SetMouseStiffness(float32 k);
float32 GetMouseStiffness();
void SetMouseDamping(float32 k);
float32 GetMouseDamping();
bool IsDragging() const;
bool StartDragging();
void Drag();
void StopDragging();
b3Vec3 GetPointA() const;
b3Vec3 GetPointB() const;
private:
b3Ray3* m_ray;
float32 m_x;
b3Cloth* m_cloth;
const b3ClothMesh* m_mesh;
u32 m_triangleIndex;
b3ClothMeshTriangle* m_triangle;
float32 m_u, m_v;
float32 m_km;
float32 m_kd;
b3Particle* m_particle;
b3MouseForce* m_mf;
bool m_staticDrag;
b3ParticleType m_t1, m_t2, m_t3;
};
inline bool b3ClothDragger::GetStaticDrag() const
{
return m_staticDrag;
}
inline void b3ClothDragger::SetMouseStiffness(float32 k)
{
m_km = k;
}
inline float32 b3ClothDragger::GetMouseStiffness()
{
return m_km;
}
inline void b3ClothDragger::SetMouseDamping(float32 k)
{
m_kd = k;
}
inline float32 b3ClothDragger::GetMouseDamping()
{
return m_kd;
}
inline bool b3ClothDragger::IsDragging() const
{
return m_triangle != nullptr;
}
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -251,24 +251,9 @@ void Draw::DrawSolidPolygon(const b3Vec3& normal, const b3Vec3* vertices, u32 co
void Draw::DrawCircle(const b3Vec3& normal, const b3Vec3& center, float32 radius, const b3Color& color)
{
// Build a tangent vector to normal.
b3Vec3 u = b3Cross(normal, b3Vec3(1.0f, 0.0f, 0.0f));
b3Vec3 v = b3Cross(normal, b3Vec3(0.0f, 1.0f, 0.0f));
b3Vec3 n1, n3;
b3ComputeBasis(normal, n1, n3);
// Handle edge cases (zero cross product).
b3Vec3 n1;
if (b3LengthSquared(u) > b3LengthSquared(v))
{
n1 = u;
}
else
{
n1 = v;
}
n1.Normalize();
// Build a quaternion to rotate the tangent about the normal.
u32 kEdgeCount = 20;
float32 kAngleInc = 2.0f * B3_PI / float32(kEdgeCount);
b3Quat q(normal, kAngleInc);
@ -292,24 +277,9 @@ void Draw::DrawSolidCircle(const b3Vec3& normal, const b3Vec3& center, float32 r
b3Color fillColor(color.r, color.g, color.b, color.a);
b3Color frameColor(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 1.0f);
// Build a tangent vector to normal.
b3Vec3 u = b3Cross(normal, b3Vec3(1.0f, 0.0f, 0.0f));
b3Vec3 v = b3Cross(normal, b3Vec3(0.0f, 1.0f, 0.0f));
b3Vec3 n1, n3;
b3ComputeBasis(normal, n1, n3);
// Handle edge cases (zero cross product).
b3Vec3 n1;
if (b3LengthSquared(u) > b3LengthSquared(v))
{
n1 = u;
}
else
{
n1 = v;
}
n1.Normalize();
// Build a quaternion to rotate the tangent about the normal.
const u32 kEdgeCount = 20;
const float32 kAngleInc = 2.0f * B3_PI / float32(kEdgeCount);
b3Quat q(normal, kAngleInc);
@ -332,16 +302,16 @@ void Draw::DrawSolidCircle(const b3Vec3& normal, const b3Vec3& center, float32 r
void Draw::DrawSphere(const b3Vec3& center, float32 radius, const b3Color& color)
{
b3Transform xf;
xf.SetIdentity();
xf.rotation.SetIdentity();
xf.position = center;
m_wire->DrawSphere(radius, color, xf);
}
void Draw::DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Color& color)
void Draw::DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Mat33& rotation, const b3Color& color)
{
b3Transform xf;
xf.SetIdentity();
xf.rotation = rotation;
xf.position = center;
m_solid->DrawSphere(radius, color, xf);
@ -352,10 +322,10 @@ void Draw::DrawCapsule(const b3Vec3& c1, const b3Vec3& c2, float32 radius, const
float32 height = b3Length(c1 - c2);
{
b3Transform xfc;
xfc.rotation.SetIdentity();
xfc.position = c1;
m_wire->DrawSphere(radius, color, xfc);
b3Transform xf;
xf.rotation.SetIdentity();
xf.position = c1;
m_wire->DrawSphere(radius, color, xf);
}
if (height > 0.0f)
@ -363,23 +333,23 @@ void Draw::DrawCapsule(const b3Vec3& c1, const b3Vec3& c2, float32 radius, const
DrawSegment(c1, c2, color);
{
b3Transform xfc;
xfc.rotation.SetIdentity();
xfc.position = c2;
m_wire->DrawSphere(radius, color, xfc);
b3Transform xf;
xf.rotation.SetIdentity();
xf.position = c2;
m_wire->DrawSphere(radius, color, xf);
}
}
}
void Draw::DrawSolidCapsule(const b3Vec3& c1, const b3Vec3& c2, float32 radius, const b3Color& c)
void Draw::DrawSolidCapsule(const b3Vec3& c1, const b3Vec3& c2, float32 radius, const b3Mat33& rotation, const b3Color& c)
{
float32 height = b3Length(c1 - c2);
{
b3Transform xfc;
xfc.rotation.SetIdentity();
xfc.position = c1;
m_solid->DrawSphere(radius, c, xfc);
b3Transform xf;
xf.rotation = rotation;
xf.position = c1;
m_solid->DrawSphere(radius, c, xf);
}
if (height > 0.0f)
@ -387,21 +357,20 @@ void Draw::DrawSolidCapsule(const b3Vec3& c1, const b3Vec3& c2, float32 radius,
{
b3Mat33 R;
R.y = (1.0f / height) * (c1 - c2);
R.z = b3Perp(R.y);
R.x = b3Cross(R.y, R.z);
b3ComputeBasis(R.y, R.z, R.x);
b3Transform xfc;
xfc.position = 0.5f * (c1 + c2);
xfc.rotation = R;
b3Transform xf;
xf.position = 0.5f * (c1 + c2);
xf.rotation = R;
m_solid->DrawCylinder(radius, height, c, xfc);
m_solid->DrawCylinder(radius, height, c, xf);
}
{
b3Transform xfc;
xfc.rotation.SetIdentity();
xfc.position = c2;
m_solid->DrawSphere(radius, c, xfc);
b3Transform xf;
xf.rotation = rotation;
xf.position = c2;
m_solid->DrawSphere(radius, c, xf);
}
}
}
@ -410,10 +379,6 @@ void Draw::DrawTransform(const b3Transform& xf)
{
float32 lenght = 1.0f;
b3Color red(1.0f, 0.0f, 0.0f, 1.0f);
b3Color green(0.0f, 1.0f, 0.0f, 1.0f);
b3Color blue(0.0f, 0.0f, 1.0f, 1.0f);
b3Vec3 position = xf.position;
b3Mat33 rotation = xf.rotation;
@ -421,9 +386,9 @@ void Draw::DrawTransform(const b3Transform& xf)
b3Vec3 B = position + lenght * rotation.y;
b3Vec3 C = position + lenght * rotation.z;
DrawSegment(position, A, red);
DrawSegment(position, B, green);
DrawSegment(position, C, blue);
DrawSegment(position, A, b3Color_red);
DrawSegment(position, B, b3Color_green);
DrawSegment(position, C, b3Color_blue);
}
void Draw::DrawAABB(const b3AABB3& aabb, const b3Color& color)
@ -460,6 +425,54 @@ void Draw::DrawAABB(const b3AABB3& aabb, const b3Color& color)
DrawSegment(vs[1], vs[7], color);
}
void Draw::DrawPlane(const b3Vec3& normal, const b3Vec3& center, float32 radius, const b3Color& color)
{
b3Vec3 n1, n2;
b3ComputeBasis(normal, n1, n2);
float32 scale = 2.0f * radius;
// v1__v4
// | |
// v2__v3
b3Vec3 v1 = center - scale * n1 - scale * n2;
b3Vec3 v2 = center + scale * n1 - scale * n2;
b3Vec3 v3 = center + scale * n1 + scale * n2;
b3Vec3 v4 = center - scale * n1 + scale * n2;
DrawSegment(v1, v2, color);
DrawSegment(v2, v3, color);
DrawSegment(v3, v4, color);
DrawSegment(v4, v1, color);
DrawSegment(center, center + normal, color);
}
void Draw::DrawSolidPlane(const b3Vec3& normal, const b3Vec3& center, float32 radius, const b3Color& color)
{
b3Color frameColor(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 1.0f);
b3Vec3 n1, n2;
b3ComputeBasis(normal, n1, n2);
float32 scale = 2.0f * radius;
b3Vec3 v1 = center - scale * n1 - scale * n2;
b3Vec3 v2 = center + scale * n1 - scale * n2;
b3Vec3 v3 = center + scale * n1 + scale * n2;
b3Vec3 v4 = center - scale * n1 + scale * n2;
DrawSegment(v1, v2, frameColor);
DrawSegment(v2, v3, frameColor);
DrawSegment(v3, v4, frameColor);
DrawSegment(v4, v1, frameColor);
DrawSegment(center, center + normal, frameColor);
DrawSolidTriangle(normal, v1, v2, v3, color);
DrawSolidTriangle(normal, v3, v4, v1, color);
}
void Draw::DrawString(const b3Color& color, const b3Vec2& ps, const char* text, ...)
{
va_list args;
@ -480,13 +493,17 @@ void Draw::DrawString(const b3Color& color, const b3Vec3& pw, const char* text,
va_list args;
va_start(args, text);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::SetNextWindowBgAlpha(0.0f);
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowSize(ImVec2(g_camera->m_width, g_camera->m_height));
ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f));
ImGui::Begin("Superlay", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar);
ImGui::SetCursorPos(ImVec2(ps.x, ps.y));
ImGui::TextColoredV(ImVec4(color.r, color.g, color.b, color.a), text, args);
ImGui::End();
ImGui::PopStyleVar();
va_end(args);
}
@ -495,181 +512,18 @@ void Draw::DrawString(const b3Color& color, const char* text, ...)
va_list args;
va_start(args, text);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::SetNextWindowBgAlpha(0.0f);
ImGui::SetNextWindowPos(ImVec2(0.0f, 40.0f));
ImGui::SetNextWindowSize(ImVec2(g_camera->m_width, g_camera->m_height));
ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f));
ImGui::Begin("Overlay", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar);
ImGui::TextColoredV(ImVec4(color.r, color.g, color.b, color.a), text, args);
ImGui::End();
ImGui::PopStyleVar();
va_end(args);
}
void Draw::DrawSolidSphere(const b3SphereShape* s, const b3Color& c, const b3Transform& xf)
{
b3Transform xfc;
xfc.rotation = xf.rotation;
xfc.position = xf * s->m_center;
m_solid->DrawSphere(s->m_radius, c, xfc);
}
void Draw::DrawSolidCapsule(const b3CapsuleShape* s, const b3Color& c, const b3Transform& xf)
{
b3Vec3 c1 = s->m_centers[0];
b3Vec3 c2 = s->m_centers[1];
float32 height = b3Length(c1 - c2);
float32 radius = s->m_radius;
{
b3Transform xfc;
xfc.rotation = xf.rotation;
xfc.position = xf * c1;
m_solid->DrawSphere(radius, c, xfc);
}
if (height > 0.0f)
{
{
b3Mat33 R;
R.y = (1.0f / height) * (c1 - c2);
R.z = b3Perp(R.y);
R.x = b3Cross(R.y, R.z);
b3Transform xfc;
xfc.position = xf * (0.5f * (c1 + c2));
xfc.rotation = xf.rotation * R;
m_solid->DrawCylinder(radius, height, c, xfc);
}
{
b3Transform xfc;
xfc.rotation = xf.rotation;
xfc.position = xf * c2;
m_solid->DrawSphere(radius, c, xfc);
}
}
}
void Draw::DrawSolidHull(const b3HullShape* s, const b3Color& c, const b3Transform& xf)
{
const b3Hull* hull = s->m_hull;
for (u32 i = 0; i < hull->faceCount; ++i)
{
const b3Face* face = hull->GetFace(i);
const b3HalfEdge* begin = hull->GetEdge(face->edge);
b3Vec3 n = xf.rotation * hull->planes[i].normal;
const b3HalfEdge* edge = hull->GetEdge(begin->next);
do
{
u32 i1 = begin->origin;
u32 i2 = edge->origin;
const b3HalfEdge* next = hull->GetEdge(edge->next);
u32 i3 = next->origin;
b3Vec3 p1 = xf * hull->vertices[i1];
b3Vec3 p2 = xf * hull->vertices[i2];
b3Vec3 p3 = xf * hull->vertices[i3];
m_triangles->Vertex(p1, c, n);
m_triangles->Vertex(p2, c, n);
m_triangles->Vertex(p3, c, n);
edge = next;
} while (hull->GetEdge(edge->next) != begin);
}
}
void Draw::DrawSolidMesh(const b3MeshShape* s, const b3Color& c, const b3Transform& xf)
{
const b3Mesh* mesh = s->m_mesh;
for (u32 i = 0; i < mesh->triangleCount; ++i)
{
const b3Triangle* t = mesh->triangles + i;
b3Vec3 p1 = xf * mesh->vertices[t->v1];
b3Vec3 p2 = xf * mesh->vertices[t->v2];
b3Vec3 p3 = xf * mesh->vertices[t->v3];
b3Vec3 n1 = b3Cross(p2 - p1, p3 - p1);
n1.Normalize();
m_triangles->Vertex(p1, c, n1);
m_triangles->Vertex(p2, c, n1);
m_triangles->Vertex(p3, c, n1);
b3Vec3 n2 = -n1;
m_triangles->Vertex(p1, c, n2);
m_triangles->Vertex(p3, c, n2);
m_triangles->Vertex(p2, c, n2);
}
}
void Draw::DrawSolidShape(const b3Shape* s, const b3Color& c, const b3Transform& xf)
{
switch (s->GetType())
{
case e_sphereShape:
{
DrawSolidSphere((b3SphereShape*)s, c, xf);
break;
}
case e_capsuleShape:
{
DrawSolidCapsule((b3CapsuleShape*)s, c, xf);
break;
}
case e_hullShape:
{
DrawSolidHull((b3HullShape*)s, c, xf);
break;
}
case e_meshShape:
{
DrawSolidMesh((b3MeshShape*)s, c, xf);
break;
}
default:
{
break;
}
}
}
void Draw::DrawSolidShapes(const b3World& world)
{
for (b3Body* b = world.GetBodyList().m_head; b; b = b->GetNext())
{
b3Color c;
if (b->IsAwake() == false)
{
c = b3Color(0.5f, 0.25f, 0.25f, 1.0f);
}
else if (b->GetType() == e_staticBody)
{
c = b3Color(0.5f, 0.5f, 0.5f, 1.0f);
}
else if (b->GetType() == e_dynamicBody)
{
c = b3Color(1.0f, 0.5f, 0.5f, 1.0f);
}
else
{
c = b3Color(0.5f, 0.5f, 1.0f, 1.0f);
}
b3Transform xf = b->GetTransform();
for (b3Shape* s = b->GetShapeList().m_head; s; s = s->GetNext())
{
DrawSolidShape(s, c, xf);
}
}
}
void Draw::Flush()
{
m_triangles->Flush();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -85,11 +85,15 @@ public:
void DrawSphere(const b3Vec3& center, float32 radius, const b3Color& color);
void DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Color& color);
void DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Mat33& rotation, const b3Color& color);
void DrawCapsule(const b3Vec3& p1, const b3Vec3& p2, float32 radius, const b3Color& color);
void DrawSolidCapsule(const b3Vec3& p1, const b3Vec3& p2, float32 radius, const b3Color& color);
void DrawSolidCapsule(const b3Vec3& p1, const b3Vec3& p2, float32 radius, const b3Mat33& rotation, const b3Color& color);
void DrawPlane(const b3Vec3& normal, const b3Vec3& center, float32 radius, const b3Color& color);
void DrawSolidPlane(const b3Vec3& normal, const b3Vec3& center, float32 radius, const b3Color& color);
void DrawAABB(const b3AABB3& aabb, const b3Color& color);
@ -101,18 +105,6 @@ public:
void DrawString(const b3Color& color, const char* string, ...);
void DrawSolidSphere(const b3SphereShape* s, const b3Color& c, const b3Transform& xf);
void DrawSolidCapsule(const b3CapsuleShape* s, const b3Color& c, const b3Transform& xf);
void DrawSolidHull(const b3HullShape* s, const b3Color& c, const b3Transform& xf);
void DrawSolidMesh(const b3MeshShape* s, const b3Color& c, const b3Transform& xf);
void DrawSolidShape(const b3Shape* s, const b3Color& c, const b3Transform& xf);
void DrawSolidShapes(const b3World& world);
void Flush();
private:
friend struct DrawPoints;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -28,6 +28,9 @@
#include <bounce/common/math/mat44.h>
#include <bounce/common/draw.h>
#include <bounce/meshgen/sphere_mesh.h>
#include <bounce/meshgen/cylinder_mesh.h>
#define BUFFER_OFFSET(i) ((char*)NULL + (i))
extern bool g_glDrawPoints;
@ -535,72 +538,22 @@ struct DrawTriangles
struct DrawWireSphere
{
enum
{
e_rings = 12,
e_sectors = 12,
e_vertexCount = e_rings * e_sectors,
e_indexCount = (e_rings - 1) * (e_sectors - 1) * 8
};
DrawWireSphere()
{
float32 R = 1.0f / float32(e_rings - 1);
float32 S = 1.0f / float32(e_sectors - 1);
smMesh mesh;
smCreateMesh(mesh, 2);
b3Vec3 vs[e_vertexCount];
b3Vec3 ns[e_vertexCount];
b3Color cs[e_vertexCount];
u32 vc = 0;
for (u32 r = 0; r < e_rings; r++)
{
for (u32 s = 0; s < e_sectors; s++)
{
float32 y = sin(-0.5f * B3_PI + B3_PI * r * R);
float32 x = cos(2.0f * B3_PI * s * S) * sin(B3_PI * r * R);
float32 z = sin(2.0f * B3_PI * s * S) * sin(B3_PI * r * R);
vs[vc].Set(x, y, z);
cs[vc] = b3Color(1.0f, 1.0f, 1.0f, 1.0f);
++vc;
}
}
u32 is[e_indexCount];
u32 ic = 0;
for (u32 r = 0; r < e_rings - 1; r++)
{
for (u32 s = 0; s < e_sectors - 1; s++)
{
u32 i1 = r * e_sectors + s;
u32 i2 = (r + 1) * e_sectors + s;
u32 i3 = (r + 1) * e_sectors + (s + 1);
u32 i4 = r * e_sectors + (s + 1);
is[ic++] = i1;
is[ic++] = i2;
is[ic++] = i2;
is[ic++] = i3;
is[ic++] = i3;
is[ic++] = i4;
is[ic++] = i4;
is[ic++] = i1;
}
}
m_vertexCount = mesh.vertexCount;
m_indexCount = mesh.indexCount;
glGenBuffers(1, &m_vboId);
glGenBuffers(1, &m_iboId);
glBindBuffer(GL_ARRAY_BUFFER, m_vboId);
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), vs, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ic * sizeof(u32), is, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount * sizeof(u32), mesh.indices, GL_STATIC_DRAW);
AssertGL();
@ -616,6 +569,8 @@ struct DrawWireSphere
GLuint m_vboId;
GLuint m_iboId;
u32 m_vertexCount;
u32 m_indexCount;
};
struct DrawWire
@ -670,6 +625,7 @@ struct DrawWire
b3Mat44 m3 = g_glProjectionMatrix;
b3Mat44 m = m3 * m2 * m1;
glUniform4fv(m_colorUniform, 1, &c.r);
glUniformMatrix4fv(m_projectionUniform, 1, GL_FALSE, &m.x.x);
glBindBuffer(GL_ARRAY_BUFFER, m_sphere.m_vboId);
@ -677,7 +633,7 @@ struct DrawWire
glEnableVertexAttribArray(m_vertexAttribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphere.m_iboId);
glDrawElements(GL_LINES, m_sphere.e_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDrawElements(GL_LINES, m_sphere.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDisableVertexAttribArray(m_vertexAttribute);
@ -697,84 +653,25 @@ struct DrawWire
struct DrawSolidSphere
{
enum
{
e_rings = 18,
e_sectors = 18,
e_vertexCount = e_rings * e_sectors,
e_indexCount = (e_rings - 1) * (e_sectors - 1) * 6,
e_faceCount = e_indexCount / 3
};
DrawSolidSphere()
{
float32 R = 1.0f / float32(e_rings - 1);
float32 S = 1.0f / float32(e_sectors - 1);
smMesh mesh;
smCreateMesh(mesh, 2);
b3Vec3 vs[e_vertexCount];
b3Vec3 ns[e_vertexCount];
u32 vc = 0;
for (u32 r = 0; r < e_rings; r++)
{
for (u32 s = 0; s < e_sectors; s++)
{
float32 a1 = 2.0f * B3_PI * float32(s) * S;
float32 c1 = cos(a1);
float32 s1 = sin(a1);
float32 a2 = -0.5f * B3_PI + B3_PI * float32(r) * R;
float32 s2 = sin(a2);
float32 a3 = B3_PI * float32(r) * R;
float32 s3 = sin(a3);
float32 x = c1 * s3;
float32 y = s2;
float32 z = s1 * s3;
b3Vec3 v(x, y, z);
v.Normalize();
vs[vc] = v;
ns[vc] = v;
++vc;
}
}
u32 is[e_indexCount];
u32 ic = 0;
for (u32 r = 0; r < e_rings - 1; r++)
{
for (u32 s = 0; s < e_sectors - 1; s++)
{
u32 i1 = r * e_sectors + s;
u32 i2 = (r + 1) * e_sectors + s;
u32 i3 = (r + 1) * e_sectors + (s + 1);
u32 i4 = r * e_sectors + (s + 1);
is[ic++] = i1;
is[ic++] = i2;
is[ic++] = i3;
is[ic++] = i1;
is[ic++] = i3;
is[ic++] = i4;
}
}
m_vertexCount = mesh.vertexCount;
m_indexCount = mesh.indexCount;
glGenBuffers(3, m_vboIds);
glGenBuffers(1, &m_iboId);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), vs, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[1]);
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), ns, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.normals, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ic * sizeof(u32), is, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount * sizeof(u32), mesh.indices, GL_STATIC_DRAW);
AssertGL();
@ -790,100 +687,31 @@ struct DrawSolidSphere
GLuint m_vboIds[2];
GLuint m_iboId;
u32 m_vertexCount;
u32 m_indexCount;
};
struct DrawSolidCylinder
{
enum
{
e_segments = 64,
e_vertexCount = e_segments * 6,
};
DrawSolidCylinder()
{
b3Vec3 vs[e_vertexCount];
b3Vec3 ns[e_vertexCount];
cymMesh mesh;
cymCreateMesh(mesh, 20);
u32 vc = 0;
for (u32 i = 0; i < e_segments; ++i)
{
float32 t0 = 2.0f * B3_PI * float32(i) / float32(e_segments);
float32 t1 = 2.0f * B3_PI * float32(i + 1) / float32(e_segments);
float32 c0 = cos(t0);
float32 s0 = sin(t0);
float32 c1 = cos(t1);
float32 s1 = sin(t1);
b3Vec3 v1;
v1.x = s0;
v1.y = -0.5f;
v1.z = c0;
b3Vec3 v2;
v2.x = s1;
v2.y = -0.5f;
v2.z = c1;
b3Vec3 v3;
v3.x = s1;
v3.y = 0.5f;
v3.z = c1;
b3Vec3 v4;
v4.x = s0;
v4.y = 0.5f;
v4.z = c0;
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
n.Normalize();
vs[vc] = v1;
ns[vc] = n;
++vc;
vs[vc] = v2;
ns[vc] = n;
++vc;
vs[vc] = v3;
ns[vc] = n;
++vc;
vs[vc] = v1;
ns[vc] = n;
++vc;
vs[vc] = v3;
ns[vc] = n;
++vc;
vs[vc] = v4;
ns[vc] = n;
++vc;
}
u32 is[e_vertexCount];
u32 ic = vc;
for (u32 i = 0; i < vc; ++i)
{
is[i] = i;
}
m_vertexCount = mesh.vertexCount;
m_indexCount = mesh.indexCount;
glGenBuffers(2, m_vboIds);
glGenBuffers(1, &m_iboId);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), vs, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[1]);
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), ns, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.normals, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ic * sizeof(u32), is, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount * sizeof(u32), mesh.indices, GL_STATIC_DRAW);
AssertGL();
@ -899,6 +727,8 @@ struct DrawSolidCylinder
GLuint m_vboIds[2];
GLuint m_iboId;
u32 m_vertexCount;
u32 m_indexCount;
};
struct DrawSolid
@ -978,7 +808,7 @@ struct DrawSolid
glEnableVertexAttribArray(m_normalAttribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_cylinder.m_iboId);
glDrawElements(GL_TRIANGLES, m_cylinder.e_vertexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, m_cylinder.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDisableVertexAttribArray(m_normalAttribute);
@ -1021,7 +851,7 @@ struct DrawSolid
glEnableVertexAttribArray(m_normalAttribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphere.m_iboId);
glDrawElements(GL_TRIANGLES, m_sphere.e_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, m_sphere.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDisableVertexAttribArray(m_normalAttribute);

View File

@ -28,6 +28,9 @@
#include <bounce/common/math/mat44.h>
#include <bounce/common/draw.h>
#include <bounce/meshgen/sphere_mesh.h>
#include <bounce/meshgen/cylinder_mesh.h>
#define BUFFER_OFFSET(i) ((char*)NULL + (i))
extern bool g_glDrawPoints;
@ -551,72 +554,22 @@ struct DrawTriangles
struct DrawWireSphere
{
enum
{
e_rings = 12,
e_sectors = 12,
e_vertexCount = e_rings * e_sectors,
e_indexCount = (e_rings - 1) * (e_sectors - 1) * 8
};
DrawWireSphere()
{
float32 R = 1.0f / float32(e_rings - 1);
float32 S = 1.0f / float32(e_sectors - 1);
smMesh mesh;
smCreateMesh(mesh, 2);
b3Vec3 vs[e_vertexCount];
b3Vec3 ns[e_vertexCount];
b3Color cs[e_vertexCount];
u32 vc = 0;
for (u32 r = 0; r < e_rings; r++)
{
for (u32 s = 0; s < e_sectors; s++)
{
float32 y = sin(-0.5f * B3_PI + B3_PI * r * R);
float32 x = cos(2.0f * B3_PI * s * S) * sin(B3_PI * r * R);
float32 z = sin(2.0f * B3_PI * s * S) * sin(B3_PI * r * R);
vs[vc].Set(x, y, z);
cs[vc] = b3Color(1.0f, 1.0f, 1.0f, 1.0f);
++vc;
}
}
u32 is[e_indexCount];
u32 ic = 0;
for (u32 r = 0; r < e_rings - 1; r++)
{
for (u32 s = 0; s < e_sectors - 1; s++)
{
u32 i1 = r * e_sectors + s;
u32 i2 = (r + 1) * e_sectors + s;
u32 i3 = (r + 1) * e_sectors + (s + 1);
u32 i4 = r * e_sectors + (s + 1);
is[ic++] = i1;
is[ic++] = i2;
is[ic++] = i2;
is[ic++] = i3;
is[ic++] = i3;
is[ic++] = i4;
is[ic++] = i4;
is[ic++] = i1;
}
}
m_vertexCount = mesh.vertexCount;
m_indexCount = mesh.indexCount;
glGenBuffers(1, &m_vboId);
glGenBuffers(1, &m_iboId);
glBindBuffer(GL_ARRAY_BUFFER, m_vboId);
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), vs, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ic * sizeof(u32), is, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount * sizeof(u32), mesh.indices, GL_STATIC_DRAW);
AssertGL();
@ -632,6 +585,8 @@ struct DrawWireSphere
GLuint m_vboId;
GLuint m_iboId;
u32 m_vertexCount;
u32 m_indexCount;
};
struct DrawWire
@ -694,7 +649,7 @@ struct DrawWire
glEnableVertexAttribArray(m_vertexAttribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphere.m_iboId);
glDrawElements(GL_LINES, m_sphere.e_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDrawElements(GL_LINES, m_sphere.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -712,84 +667,25 @@ struct DrawWire
struct DrawSolidSphere
{
enum
{
e_rings = 18,
e_sectors = 18,
e_vertexCount = e_rings * e_sectors,
e_indexCount = (e_rings - 1) * (e_sectors - 1) * 6,
e_faceCount = e_indexCount / 3
};
DrawSolidSphere()
{
float32 R = 1.0f / float32(e_rings - 1);
float32 S = 1.0f / float32(e_sectors - 1);
smMesh mesh;
smCreateMesh(mesh, 2);
b3Vec3 vs[e_vertexCount];
b3Vec3 ns[e_vertexCount];
u32 vc = 0;
for (u32 r = 0; r < e_rings; r++)
{
for (u32 s = 0; s < e_sectors; s++)
{
float32 a1 = 2.0f * B3_PI * float32(s) * S;
float32 c1 = cos(a1);
float32 s1 = sin(a1);
float32 a2 = -0.5f * B3_PI + B3_PI * float32(r) * R;
float32 s2 = sin(a2);
float32 a3 = B3_PI * float32(r) * R;
float32 s3 = sin(a3);
float32 x = c1 * s3;
float32 y = s2;
float32 z = s1 * s3;
b3Vec3 v(x, y, z);
v.Normalize();
vs[vc] = v;
ns[vc] = v;
++vc;
}
}
u32 is[e_indexCount];
u32 ic = 0;
for (u32 r = 0; r < e_rings - 1; r++)
{
for (u32 s = 0; s < e_sectors - 1; s++)
{
u32 i1 = r * e_sectors + s;
u32 i2 = (r + 1) * e_sectors + s;
u32 i3 = (r + 1) * e_sectors + (s + 1);
u32 i4 = r * e_sectors + (s + 1);
is[ic++] = i1;
is[ic++] = i2;
is[ic++] = i3;
is[ic++] = i1;
is[ic++] = i3;
is[ic++] = i4;
}
}
m_vertexCount = mesh.vertexCount;
m_indexCount = mesh.indexCount;
glGenBuffers(3, m_vboIds);
glGenBuffers(1, &m_iboId);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), vs, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[1]);
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), ns, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ic * sizeof(u32), is, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount * sizeof(u32), mesh.indices, GL_STATIC_DRAW);
AssertGL();
@ -805,100 +701,31 @@ struct DrawSolidSphere
GLuint m_vboIds[2];
GLuint m_iboId;
u32 m_vertexCount;
u32 m_indexCount;
};
struct DrawSolidCylinder
{
enum
{
e_segments = 64,
e_vertexCount = e_segments * 6,
};
DrawSolidCylinder()
{
b3Vec3 vs[e_vertexCount];
b3Vec3 ns[e_vertexCount];
cymMesh mesh;
cymCreateMesh(mesh, 20);
u32 vc = 0;
for (u32 i = 0; i < e_segments; ++i)
{
float32 t0 = 2.0f * B3_PI * float32(i) / float32(e_segments);
float32 t1 = 2.0f * B3_PI * float32(i + 1) / float32(e_segments);
float32 c0 = cos(t0);
float32 s0 = sin(t0);
float32 c1 = cos(t1);
float32 s1 = sin(t1);
b3Vec3 v1;
v1.x = s0;
v1.y = -0.5f;
v1.z = c0;
b3Vec3 v2;
v2.x = s1;
v2.y = -0.5f;
v2.z = c1;
b3Vec3 v3;
v3.x = s1;
v3.y = 0.5f;
v3.z = c1;
b3Vec3 v4;
v4.x = s0;
v4.y = 0.5f;
v4.z = c0;
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
n.Normalize();
vs[vc] = v1;
ns[vc] = n;
++vc;
vs[vc] = v2;
ns[vc] = n;
++vc;
vs[vc] = v3;
ns[vc] = n;
++vc;
vs[vc] = v1;
ns[vc] = n;
++vc;
vs[vc] = v3;
ns[vc] = n;
++vc;
vs[vc] = v4;
ns[vc] = n;
++vc;
}
u32 is[e_vertexCount];
u32 ic = vc;
for (u32 i = 0; i < vc; ++i)
{
is[i] = i;
}
m_vertexCount = mesh.vertexCount;
m_indexCount = mesh.indexCount;
glGenBuffers(2, m_vboIds);
glGenBuffers(1, &m_iboId);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), vs, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[1]);
glBufferData(GL_ARRAY_BUFFER, vc * sizeof(b3Vec3), ns, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(b3Vec3), mesh.normals, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ic * sizeof(u32), is, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount * sizeof(u32), mesh.indices, GL_STATIC_DRAW);
AssertGL();
@ -914,6 +741,8 @@ struct DrawSolidCylinder
GLuint m_vboIds[2];
GLuint m_iboId;
u32 m_vertexCount;
u32 m_indexCount;
};
struct DrawSolid
@ -994,7 +823,7 @@ struct DrawSolid
glEnableVertexAttribArray(m_normalAttribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_cylinder.m_iboId);
glDrawElements(GL_TRIANGLES, m_cylinder.e_vertexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, m_cylinder.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -1033,7 +862,7 @@ struct DrawSolid
glEnableVertexAttribArray(m_normalAttribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphere.m_iboId);
glDrawElements(GL_TRIANGLES, m_sphere.e_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, m_sphere.m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -18,6 +18,8 @@
#include <testbed/framework/json_profiler.h>
JsonProfiler* g_jsonProfiler = nullptr;
#define STRING(x) String(x, sizeof(x) - 1)
JsonProfiler::JsonProfiler()
@ -75,7 +77,7 @@ void JsonProfiler::EndEvents()
m_file = nullptr;
}
void JsonProfiler::BeginEvent(i32 tid, i32 pid, const char* name, float64 t)
void JsonProfiler::BeginEvent(const char* name, float64 t)
{
if (!m_writer)
{
@ -87,8 +89,8 @@ void JsonProfiler::BeginEvent(i32 tid, i32 pid, const char* name, float64 t)
float64 scale = 1000.0;
m_writer->StartObject();
m_writer->STRING("pid"); m_writer->Int(pid);
m_writer->STRING("tid"); m_writer->Int(tid);
m_writer->STRING("pid"); m_writer->Int(0);
m_writer->STRING("tid"); m_writer->Int(0);
m_writer->STRING("ts"); m_writer->Int64((u64)(t * scale));
m_writer->STRING("ph"); m_writer->String(phase, 1);
m_writer->STRING("cat"); m_writer->STRING("physics");
@ -97,7 +99,7 @@ void JsonProfiler::BeginEvent(i32 tid, i32 pid, const char* name, float64 t)
m_writer->EndObject();
}
void JsonProfiler::EndEvent(i32 tid, i32 pid, const char* name, float64 t)
void JsonProfiler::EndEvent(const char* name, float64 t)
{
if (!m_writer)
{
@ -109,8 +111,8 @@ void JsonProfiler::EndEvent(i32 tid, i32 pid, const char* name, float64 t)
float64 scale = 1000.0;
m_writer->StartObject();
m_writer->STRING("pid"); m_writer->Int(pid);
m_writer->STRING("tid"); m_writer->Int(tid);
m_writer->STRING("pid"); m_writer->Int(0);
m_writer->STRING("tid"); m_writer->Int(0);
m_writer->STRING("ts"); m_writer->Int64((u64)(t * scale));
m_writer->STRING("ph"); m_writer->String(phase, 1);
m_writer->STRING("cat"); m_writer->STRING("physics");

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -26,7 +26,7 @@
using namespace rapidjson;
// The following profiler listener is notified by a profiler when events are initiated
// The following profiler is notified when events are initiated
// or terminated.
// When it receives the notification it immediately saves its data into a .json file format.
// The .json file can be read and interpreted by the Google Chrome Tracing.
@ -43,13 +43,15 @@ public:
void EndEvents();
void BeginEvent(i32 tid, i32 pid, const char* name, float64 time);
void BeginEvent(const char* name, float64 time);
void EndEvent(i32 tid, i32 pid, const char* name, float64 time);
void EndEvent(const char* name, float64 time);
private:
FILE * m_file;
FILE* m_file;
FileWriteStream* m_stream;
Writer<FileWriteStream>* m_writer;
};
extern JsonProfiler* g_jsonProfiler;
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -31,7 +31,7 @@
#include <testbed/framework/view_model.h>
//
static GLFWwindow* g_window;
GLFWwindow* g_window;
//
static Model* g_model;
@ -100,13 +100,17 @@ static void Run()
int w, h;
glfwGetWindowSize(g_window, &w, &h);
g_view->Event_SetWindowSize(u32(w), u32(h));
while (glfwWindowShouldClose(g_window) == 0)
{
g_profiler->Begin();
g_profiler->PushEvent("Frame");
g_profilerSt->Begin();
g_profiler->BeginScope("Frame");
g_profilerSt->BeginScope("Frame");
g_view->BeginInterface();
if (g_model->IsPaused())
@ -118,25 +122,33 @@ static void Run()
g_draw->DrawString(b3Color_white, "*PLAYING*");
}
if (g_settings->drawProfile)
{
const b3Array<ProfilerRecord>& records = g_profilerRecorder->GetRecords();
for (u32 i = 0; i < records.Count(); ++i)
{
const ProfilerRecord& r = records[i];
g_draw->DrawString(b3Color_white, "%s %.4f (%.4f) [ms]", r.name, r.elapsed, r.maxElapsed);
}
}
g_view->Interface();
g_model->Update();
g_view->EndInterface();
g_profiler->PopEvent();
g_profilerSt->EndScope();
g_profiler->End(g_profilerListener);
g_profiler->EndScope();
if (g_settings->drawProfileTree)
{
g_view->InterfaceProfileTree();
}
if (g_settings->drawProfileTreeStats)
{
g_view->InterfaceProfileTreeStats();
}
g_profilerSt->End();
#if PROFILE_JSON == 1
g_model->UpdateJson();
#endif
g_profiler->End();
g_view->EndInterface();
glfwSwapBuffers(g_window);
glfwPollEvents();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -26,8 +26,11 @@ Model::Model()
g_draw = &m_draw;
g_camera = &m_camera;
g_profiler = &m_profiler;
g_profilerRecorder = &m_profilerListener.m_recorderProfiler;
g_profilerListener = &m_profilerListener;
g_profilerSt = &m_profilerSt;
#if (PROFILE_JSON == 1)
g_jsonProfiler = &m_jsonProfiler;
#endif
m_test = nullptr;
@ -53,8 +56,11 @@ Model::~Model()
g_draw = nullptr;
g_camera = nullptr;
g_profiler = nullptr;
g_profilerRecorder = nullptr;
g_profilerListener = nullptr;
g_profilerSt = nullptr;
#if (PROFILE_JSON == 1)
g_jsonProfiler = nullptr;
#endif
delete m_test;
}
@ -223,4 +229,34 @@ void Model::Update()
m_test->Step();
m_draw.Flush();
}
}
#if (PROFILE_JSON == 1)
static inline void RecurseEvents(ProfilerNode* node)
{
g_jsonProfiler->BeginEvent(node->name, node->t0);
g_jsonProfiler->EndEvent(node->name, node->t1);
for (u32 i = 0; i < node->children.Count(); ++i)
{
RecurseEvents(node->children[i]);
}
}
void Model::UpdateJson()
{
m_jsonProfiler.BeginEvents();
ProfilerNode* root = m_profiler.GetRoot();
if (root)
{
RecurseEvents(root);
}
m_jsonProfiler.EndEvents();
}
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -20,7 +20,15 @@
#define MODEL_H
#include <testbed/framework/draw.h>
#include <testbed/framework/testbed_listener.h>
#include <testbed/framework/profiler.h>
#include <testbed/framework/profiler_st.h>
// Set to 1 to write profile events into a .json file. Set to 0 otherwise.
#define PROFILE_JSON 0
#if (PROFILE_JSON == 1)
#include <testbed/framework/json_profiler.h>
#endif
class Test;
@ -53,7 +61,11 @@ public:
void Command_ZoomCamera(float32 d);
void Update();
#if (PROFILE_JSON == 1)
void UpdateJson();
#endif
bool IsPaused() const { return m_pause; }
private:
friend class ViewModel;
@ -63,7 +75,12 @@ private:
Draw m_draw;
Camera m_camera;
Profiler m_profiler;
TestbedListener m_profilerListener;
ProfilerSt m_profilerSt;
#if (PROFILE_JSON == 1)
JsonProfiler m_jsonProfiler;
#endif
Test* m_test;
bool m_setTest;
bool m_pause;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -19,73 +19,92 @@
#include <testbed/framework/profiler.h>
Profiler* g_profiler = nullptr;
ProfilerListener* g_profilerListener = nullptr;
Profiler::Profiler()
Profiler::Profiler() : m_pool(sizeof(ProfilerNode))
{
m_root = nullptr;
m_top = nullptr;
}
Profiler::~Profiler()
{
assert(m_root == nullptr);
assert(m_top == nullptr);
}
bool Profiler::PushEvent(const char* name)
ProfilerNode* Profiler::CreateNode()
{
void* block = m_pool.Allocate();
ProfilerNode* n = new (block) ProfilerNode();
return n;
}
void Profiler::DestroyNode(ProfilerNode* node)
{
node->~ProfilerNode();
m_pool.Free(node);
}
void Profiler::BeginScope(const char* name)
{
m_time.Update();
ProfilerEvent e;
e.tid = -1;
e.pid = -1;
e.t0 = m_time.GetCurrentMilis();
e.t1 = 0.0;
e.name = name;
e.parent = m_top;
ProfilerNode* n = CreateNode();
n->name = name;
n->t0 = m_time.GetCurrentMilis();
n->t1 = 0.0;
n->parent = m_top;
ProfilerEvent* back = m_events.Push(e);
if (back)
if (m_root == nullptr)
{
m_top = back;
m_root = n;
m_top = n;
return;
}
return back != NULL;
if (m_top)
{
m_top->children.PushBack(n);
}
m_top = n;
}
void Profiler::PopEvent()
void Profiler::EndScope()
{
B3_ASSERT(m_top);
B3_ASSERT(m_top->t1 == 0.0);
m_time.Update();
assert(m_top != nullptr);
m_top->t1 = m_time.GetCurrentMilis();
B3_ASSERT(m_top->t1 != 0.0);
assert(m_top->t1 > m_top->t0);
m_top = m_top->parent;
}
void Profiler::Begin()
{
// If this assert is hit then it means Profiler::End hasn't been called.
B3_ASSERT(m_events.IsEmpty());
assert(m_root == nullptr);
assert(m_top == nullptr);
}
void Profiler::End(ProfilerListener* listener)
void Profiler::RecurseDestroyNode(ProfilerNode* node)
{
listener->BeginEvents();
while (m_events.IsEmpty() == false)
for (u32 i = 0; i < node->children.Count(); ++i)
{
const ProfilerEvent& e = m_events.Front();
m_events.Pop();
listener->BeginEvent(e.tid, e.pid, e.name, e.t0);
listener->EndEvent(e.tid, e.pid, e.name, e.t1);
listener->Duration(e.name, e.t1 - e.t0);
RecurseDestroyNode(node->children[i]);
}
B3_ASSERT(m_events.IsEmpty());
DestroyNode(node);
}
listener->EndEvents();
void Profiler::End()
{
assert(m_top == nullptr);
if (m_root)
{
RecurseDestroyNode(m_root);
m_root = nullptr;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -20,28 +20,21 @@
#define PROFILER_H
#include <bounce/common/math/math.h>
#include <bounce/common/time.h>
#include <bounce/common/template/queue.h>
#include <bounce/common/memory/block_pool.h>
#include <bounce/common/template/array.h>
#include <bounce/common/time.h>
// This defines the maximum number of profiler events that can be
// queued per frame until the function Profiler::Flush is called.
#define MAX_PROFILER_EVENTS 256
class ProfilerListener;
// A time-stamped profiler event.
struct ProfilerEvent
// Profiler node
struct ProfilerNode
{
i32 tid;
i32 pid;
const char* name;
float64 t0;
float64 t1;
ProfilerEvent* parent;
ProfilerNode* parent;
b3StackArray<ProfilerNode*, 32> children;
};
// A single-threaded event-based profiler.
// A single-threaded profiler.
class Profiler
{
public:
@ -53,67 +46,28 @@ public:
void Begin();
// Must be called after profiling.
// The function will report all events in this profiler
// to the given event listener in the correct calling order.
// This function also flushes the profiler.
void End(ProfilerListener* listener);
void End();
// Add a profiler event to the queue.
// Return true if the even has been added to the event queue
// or false if the queue is full.
// You can control the maximum number of profiler events using
// MAX_PROFILER_EVENTS.
bool PushEvent(const char* name);
// Begin a new scope.
void BeginScope(const char* name);
// Remove the top profiler event.
void PopEvent();
// End the top scope.
void EndScope();
// Get the root profiler node.
ProfilerNode* GetRoot() { return m_root; }
private:
b3Time m_time;
b3BoundedQueue<ProfilerEvent, MAX_PROFILER_EVENTS> m_events;
ProfilerEvent* m_top;
ProfilerNode* CreateNode();
void DestroyNode(ProfilerNode* node);
void RecurseDestroyNode(ProfilerNode* node);
b3BlockPool m_pool; // pool of nodes
b3Time m_time; // timer
ProfilerNode* m_root; // tree root node
ProfilerNode* m_top; // top node
};
extern Profiler* g_profiler;
// Any implementation of this interface passed to Profiler::End will listen to profile events.
class ProfilerListener
{
public:
virtual ~ProfilerListener() { }
// This function is called when profiling has began.
virtual void BeginEvents() { }
// This function is called when profiling has ended.
virtual void EndEvents() { }
// This function is called when a profiler event begins.
virtual void BeginEvent(i32 tid, i32 pid, const char* name, float64 time)
{
B3_NOT_USED(tid);
B3_NOT_USED(pid);
B3_NOT_USED(name);
B3_NOT_USED(time);
}
// This function is called when a profiler event ends.
virtual void EndEvent(i32 tid, i32 pid, const char* name, float64 time)
{
B3_NOT_USED(tid);
B3_NOT_USED(pid);
B3_NOT_USED(name);
B3_NOT_USED(time);
}
// This function is called when a profiler event ends.
// However it supplies the duration of the last begin and end events.
virtual void Duration(const char* name, float64 duration)
{
B3_NOT_USED(name);
B3_NOT_USED(duration);
}
};
extern ProfilerListener* g_profilerListener;
#endif

View File

@ -0,0 +1,197 @@
/*
* 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/profiler_st.h>
ProfilerSt* g_profilerSt = nullptr;
ProfilerSt::ProfilerSt() : m_pool(sizeof(ProfilerStNode))
{
m_root = nullptr;
m_top = nullptr;
}
ProfilerSt::~ProfilerSt()
{
assert(m_root == nullptr);
assert(m_top == nullptr);
}
ProfilerStNodeStat* ProfilerSt::FindStat(const char* name)
{
for (u32 i = 0; i < m_stats.Count(); ++i)
{
if (m_stats[i].name == name)
{
return &m_stats[i];
}
}
return nullptr;
}
ProfilerStNodeStat* ProfilerSt::CreateStat()
{
m_stats.PushBack(ProfilerStNodeStat());
return &m_stats.Back();
}
ProfilerStNode* ProfilerSt::CreateNode()
{
void* block = m_pool.Allocate();
return new (block) ProfilerStNode();
}
void ProfilerSt::DestroyNode(ProfilerStNode* node)
{
node->~ProfilerStNode();
m_pool.Free(node);
}
void ProfilerSt::RecurseDestroyNode(ProfilerStNode* node)
{
for (u32 i = 0; i < node->children.Count(); ++i)
{
return RecurseDestroyNode(node->children[i]);
}
DestroyNode(node);
}
static ProfilerStNode* RecurseFindNode(ProfilerStNode* node, const char* name)
{
if (node->name == name)
{
return node;
}
ProfilerStNode* result = nullptr;
for (u32 i = 0; result == nullptr && i < node->children.Count(); ++i)
{
result = RecurseFindNode(node->children[i], name);
}
return result;
}
ProfilerStNode* ProfilerSt::FindNode(const char* name)
{
if (m_top)
{
return RecurseFindNode(m_top, name);
}
if (m_root)
{
return RecurseFindNode(m_root, name);
}
return nullptr;
}
void ProfilerSt::BeginScope(const char* name)
{
ProfilerStNode* fn = FindNode(name);
if (fn)
{
m_time.Update();
fn->t0 = m_time.GetCurrentMilis();
++fn->callCount;
m_top = fn;
return;
}
m_time.Update();
ProfilerStNode* n = CreateNode();
n->name = name;
n->t0 = m_time.GetCurrentMilis();
n->elapsed = 0.0f;
n->callCount = 1;
n->parent = m_top;
n->stat = nullptr;
if (m_root == nullptr)
{
m_root = n;
m_top = n;
return;
}
if (m_top)
{
m_top->children.PushBack(n);
}
m_top = n;
}
void ProfilerSt::EndScope()
{
assert(m_top != nullptr);
m_time.Update();
m_top->t1 = m_time.GetCurrentMilis();
float64 elapsedTime = m_top->t1 - m_top->t0;
m_top->elapsed += elapsedTime;
ProfilerStNodeStat* stat = FindStat(m_top->name);
if (stat == nullptr)
{
stat = CreateStat();
stat->name = m_top->name;
stat->minElapsed = elapsedTime;
stat->maxElapsed = elapsedTime;
}
else
{
stat->minElapsed = b3Min(stat->minElapsed, elapsedTime);
stat->maxElapsed = b3Max(stat->maxElapsed, elapsedTime);
}
if (m_top->stat == nullptr)
{
m_top->stat = stat;
}
assert(m_top->stat == stat);
m_top = m_top->parent;
}
void ProfilerSt::Begin()
{
assert(m_top == nullptr);
}
void ProfilerSt::End()
{
assert(m_top == nullptr);
if (m_root)
{
RecurseDestroyNode(m_root);
m_root = nullptr;
}
}

View File

@ -0,0 +1,94 @@
/*
* 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 PROFILER_ST_H
#define PROFILER_ST_H
#include <bounce/common/math/math.h>
#include <bounce/common/memory/block_pool.h>
#include <bounce/common/template/array.h>
#include <bounce/common/time.h>
// Profiler tree node statistics
struct ProfilerStNodeStat
{
const char* name;
float64 minElapsed;
float64 maxElapsed;
};
// A profiler tree node
struct ProfilerStNode
{
const char* name;
float64 t0;
float64 t1;
float64 elapsed;
u32 callCount;
ProfilerStNode* parent;
b3StackArray<ProfilerStNode*, 32> children;
ProfilerStNodeStat* stat;
};
// A profiler tree
class ProfilerSt
{
public:
ProfilerSt();
~ProfilerSt();
// Must be called before profiling.
void Begin();
// Must be called after profiling.
void End();
// Begin a new scope.
void BeginScope(const char* name);
// End the top scope.
void EndScope();
ProfilerStNode* GetRoot() { return m_root; }
private:
ProfilerStNode* CreateNode();
void DestroyNode(ProfilerStNode* node);
void RecurseDestroyNode(ProfilerStNode* node);
ProfilerStNode* FindNode(const char* name);
ProfilerStNodeStat* CreateStat();
ProfilerStNodeStat* FindStat(const char* name);
b3BlockPool m_pool; // pool of nodes
b3Time m_time; // timer
ProfilerStNode* m_root; // tree root node
ProfilerStNode* m_top; // top node
b3StackArray<ProfilerStNodeStat, 256> m_stats; // node statistics
};
extern ProfilerSt* g_profilerSt;
#endif

View 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();
}

View 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -18,32 +18,27 @@
#include <testbed/framework/test.h>
#include <testbed/framework/profiler.h>
#include <imgui/imgui.h>
#include <testbed/framework/profiler_st.h>
extern u32 b3_allocCalls, b3_maxAllocCalls;
extern u32 b3_convexCalls, b3_convexCacheHits;
extern u32 b3_gjkCalls, b3_gjkIters, b3_gjkMaxIters;
extern bool b3_convexCache;
static bool push_ok = false;
void b3BeginProfileScope(const char* name)
{
push_ok = g_profiler->PushEvent(name);
g_profiler->BeginScope(name);
g_profilerSt->BeginScope(name);
}
void b3EndProfileScope()
{
if (push_ok)
{
g_profiler->PopEvent();
push_ok = false;
}
g_profiler->EndScope();
g_profilerSt->EndScope();
}
Test::Test() :
m_bodyDragger(&m_ray, &m_world),
m_clothDragger(&m_ray, &m_world)
m_bodyDragger(&m_ray, &m_world)
{
b3Draw_draw = g_draw;
b3_convexCache = g_testSettings->convexCache;
@ -93,7 +88,7 @@ void Test::Step()
if (g_settings->drawTriangles)
{
g_draw->DrawSolidShapes(m_world);
m_world.DrawSolid();
}
if (g_settings->drawStats)
@ -121,11 +116,6 @@ void Test::Step()
g_draw->DrawString(b3Color_white, "Convex Cache Hits %d (%f)", b3_convexCacheHits, convexCacheHitRatio);
g_draw->DrawString(b3Color_white, "Frame Allocations %d (%d)", b3_allocCalls, b3_maxAllocCalls);
}
if (m_clothDragger.IsDragging() == true)
{
g_draw->DrawSegment(m_clothDragger.GetPointA(), m_clothDragger.GetPointB(), b3Color_white);
}
}
void Test::MouseMove(const b3Ray3& pw)
@ -136,11 +126,6 @@ void Test::MouseMove(const b3Ray3& pw)
{
m_bodyDragger.Drag();
}
if (m_clothDragger.IsDragging() == true)
{
m_clothDragger.Drag();
}
}
void Test::MouseLeftDown(const b3Ray3& pw)
@ -152,14 +137,6 @@ void Test::MouseLeftDown(const b3Ray3& pw)
BeginDragging();
}
}
if (m_clothDragger.IsDragging() == false)
{
if (m_clothDragger.StartDragging() == true)
{
BeginDragging();
}
}
}
void Test::MouseLeftUp(const b3Ray3& pw)
@ -170,11 +147,4 @@ void Test::MouseLeftUp(const b3Ray3& pw)
EndDragging();
}
if (m_clothDragger.IsDragging() == true)
{
m_clothDragger.StopDragging();
EndDragging();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -22,6 +22,9 @@
#include <glfw/glfw3.h>
#include <bounce/bounce.h>
#include <testbed/framework/body_dragger.h>
#include <testbed/framework/cloth_dragger.h>
#include <testbed/framework/draw.h>
#include <testbed/framework/view_model.h>
@ -33,27 +36,6 @@ inline float32 RandomFloat(float32 a, float32 b)
return a + r;
}
class RayCastListener : public b3RayCastListener
{
public:
float32 ReportShape(b3Shape* shape, const b3Vec3& point, const b3Vec3& normal, float32 fraction)
{
hit.shape = shape;
hit.point = point;
hit.normal = normal;
hit.fraction = fraction;
return 1.0f;
}
float32 ReportCloth(b3Shape* shape, const b3Vec3& point, const b3Vec3& normal, float32 fraction)
{
B3_ASSERT(false);
return 1.0f;
}
b3RayCastSingleShapeOutput hit;
};
class Test : public b3ContactListener
{
public:
@ -77,10 +59,9 @@ public:
void EndContact(b3Contact* c) override { }
void PreSolve(b3Contact* c) override { }
b3World m_world;
b3Ray3 m_ray;
b3ClothDragger m_clothDragger;
b3World m_world;
b3BodyDragger m_bodyDragger;
b3BoxHull m_groundHull;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -20,6 +20,7 @@
#include <testbed/tests/convex_hull.h>
#include <testbed/tests/cluster.h>
#include <testbed/tests/distance_test.h>
#include <testbed/tests/shape_cast.h>
#include <testbed/tests/collide_test.h>
#include <testbed/tests/capsule_collision.h>
#include <testbed/tests/hull_collision.h>
@ -53,28 +54,27 @@
#include <testbed/tests/pyramids.h>
#include <testbed/tests/ray_cast.h>
#include <testbed/tests/sensor_test.h>
#include <testbed/tests/point_click.h>
#include <testbed/tests/body_types.h>
#include <testbed/tests/varying_friction.h>
#include <testbed/tests/varying_restitution.h>
#include <testbed/tests/tumbler.h>
#include <testbed/tests/multiple_pendulum.h>
#include <testbed/tests/cloth_test.h>
#include <testbed/tests/table_cloth.h>
#include <testbed/tests/pinned_cloth.h>
#include <testbed/tests/shirt.h>
#include <testbed/tests/particle_types.h>
#include <testbed/tests/tension_mapping.h>
#include <testbed/tests/self_collision.h>
#include <testbed/tests/single_pendulum.h>
#include <testbed/tests/cloth_self_collision.h>
#include <testbed/tests/rope_test.h>
#include <testbed/tests/mass_spring.h>
#include <testbed/tests/beam.h>
#include <testbed/tests/pinned_softbody.h>
#include <testbed/tests/smash_softbody.h>
TestEntry g_tests[] =
{
{ "Convex Hull", &ConvexHull::Create },
{ "Cluster", &Cluster::Create },
{ "Distance", &Distance::Create },
{ "Shape Cast", &ShapeCast::Create },
{ "Capsule Collision", &CapsuleCollision::Create },
{ "Hull Collision", &HullCollision::Create },
{ "Deep Capsule", &DeepCapsule::Create },
@ -106,7 +106,6 @@ TestEntry g_tests[] =
{ "Box Pyramid Rows", &Pyramids::Create },
{ "Ray Cast", &RayCast::Create },
{ "Sensor Test", &SensorTest::Create },
{ "Point & Click", &PointClick::Create },
{ "Body Types", &BodyTypes::Create },
{ "Varying Friction", &VaryingFriction::Create },
{ "Varying Restitution", &VaryingRestitution::Create },
@ -115,12 +114,12 @@ TestEntry g_tests[] =
{ "Multiple Pendulum", &MultiplePendulum::Create },
{ "Table Cloth", &TableCloth::Create },
{ "Pinned Cloth", &PinnedCloth::Create },
{ "Shirt", &Shirt::Create },
{ "Particle Types", &ParticleTypes::Create },
{ "Tension Mapping", &TensionMapping::Create },
{ "Self-Collision", &SelfCollision::Create },
{ "Mass-Spring System", &MassSpring::Create },
{ "Single Pendulum", &SinglePendulum::Create },
{ "Cloth Self-Collision", &ClothSelfCollision::Create },
{ "Beam", &Beam::Create },
{ "Pinned Soft Body", &PinnedSoftBody::Create },
{ "Smash Soft Body", &SmashSoftBody::Create },
{ "Rope", &Rope::Create },
{ NULL, NULL }
};

View File

@ -1,86 +0,0 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* 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 TESTBED_LISTENER_H
#define TESTBED_LISTENER_H
#include <testbed/framework/profiler.h>
#include <testbed/framework/recorder_profiler.h>
// Set to 1 then the testbed listener will write profile events into a .json file.
// Set to 0 otherwise.
#define PROFILE_JSON 0
#if (PROFILE_JSON == 1)
#include <testbed\framework\json_profiler.h>
#endif
class TestbedListener : public ProfilerListener
{
public:
void BeginEvents() override
{
m_recorderProfiler.BeginEvents();
#if (PROFILE_JSON == 1)
m_jsonListener.BeginEvents();
#endif
}
void EndEvents() override
{
m_recorderProfiler.EndEvents();
#if (PROFILE_JSON == 1)
m_jsonListener.EndEvents();
#endif
}
void BeginEvent(i32 tid, i32 pid, const char* name, float64 time) override
{
#if (PROFILE_JSON == 1)
m_jsonListener.BeginEvent(tid, pid, name, time);
#endif
}
void EndEvent(i32 tid, i32 pid, const char* name, float64 time) override
{
#if (PROFILE_JSON == 1)
m_jsonListener.EndEvent(tid, pid, name, time);
#endif
}
void Duration(const char* name, float64 time) override
{
m_recorderProfiler.Add(name, time);
}
RecorderProfiler m_recorderProfiler;
#if (PROFILE_JSON == 1)
JsonProfiler m_jsonListener;
#endif
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -19,6 +19,8 @@
#include <testbed/framework/view.h>
#include <testbed/framework/view_model.h>
#include <testbed/framework/test.h>
#include <testbed/framework/profiler.h>
#include <testbed/framework/profiler_st.h>
#include <imgui/imgui.h>
#if defined (U_OPENGL_2)
@ -217,7 +219,8 @@ void View::Interface()
if (ImGui::BeginMenu("View"))
{
ImGui::MenuItem("Profile", "", &settings.drawProfile);
ImGui::MenuItem("Profile Tree", "", &settings.drawProfileTree);
ImGui::MenuItem("Profile Tree Statistics", "", &settings.drawProfileTreeStats);
ImGui::MenuItem("Statistics", "", &settings.drawStats);
ImGui::Separator();
@ -400,6 +403,111 @@ void View::Interface()
ImGui::End();
}
static void TreeNode(ProfilerNode* node, u32& index)
{
ImGui::PushID(index);
++index;
if (ImGui::TreeNode(node->name))
{
float64 elapsedTime = node->t1 - node->t0;
ImGui::Text("%.4f [ms]", elapsedTime);
for (u32 i = 0; i < node->children.Count(); ++i)
{
TreeNode(node->children[i], index);
}
ImGui::TreePop();
}
ImGui::PopID();
}
void View::InterfaceProfileTree()
{
ImGui::Begin("Overlay", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar);
ImVec2 ws = ImGui::GetWindowSize();
ImVec2 wp = ImGui::GetWindowPos();
ImGui::End();
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::SetNextWindowBgAlpha(0.0f);
ImGui::SetNextWindowPos(ImVec2(0.0f, wp.y + ws.y));
ImGui::SetNextWindowSize(ImVec2(g_camera->m_width - 250.0f, 0.0f));
ImGui::Begin("Profile Tree", NULL, ImGuiWindowFlags_AlwaysAutoResize);
ProfilerNode* root = g_profiler->GetRoot();
if (root)
{
u32 index = 0;
TreeNode(root, index);
}
ImGui::End();
ImGui::PopStyleVar();
}
static void TreeNode(ProfilerStNode* node, u32& index)
{
ImGui::PushID(index);
++index;
if (ImGui::TreeNode(node->name))
{
ImGui::Text("%.4f (min = %.4f) (max = %.4f) (calls = %d) [ms]", node->elapsed, node->stat->minElapsed, node->stat->maxElapsed, node->callCount);
for (u32 i = 0; i < node->children.Count(); ++i)
{
TreeNode(node->children[i], index);
}
ImGui::TreePop();
}
ImGui::PopID();
}
void View::InterfaceProfileTreeStats()
{
ImGui::Begin("Overlay", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar);
ImVec2 wp = ImGui::GetWindowPos();
ImVec2 ws = ImGui::GetWindowSize();
ImGui::End();
wp.y = wp.y + ws.y;
if (g_settings->drawProfileTree)
{
ImGui::Begin("Profile Tree", NULL, ImGuiWindowFlags_AlwaysAutoResize);
ImVec2 ptwp = ImGui::GetWindowPos();
ImVec2 ptws = ImGui::GetWindowSize();
ImGui::End();
wp.y = ptwp.y + ptws.y;
}
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::SetNextWindowBgAlpha(0.0f);
ImGui::SetNextWindowPos(ImVec2(0.0f, wp.y));
ImGui::SetNextWindowSize(ImVec2(g_camera->m_width - 250.0f, 0.0f));
ImGui::Begin("Profile Tree Statistics", NULL, ImGuiWindowFlags_AlwaysAutoResize);
ProfilerStNode* root = g_profilerSt->GetRoot();
if (root)
{
u32 index = 0;
TreeNode(root, index);
}
ImGui::End();
ImGui::PopStyleVar();
}
void View::EndInterface()
{
ImGui::PopStyleVar();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -41,6 +41,8 @@ public:
void BeginInterface();
void Interface();
void InterfaceProfileTree();
void InterfaceProfileTreeStats();
void EndInterface();
private:
friend class ViewModel;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -32,7 +32,8 @@ struct Settings
drawLines = true;
drawTriangles = true;
drawGrid = true;
drawProfile = false;
drawProfileTree = false;
drawProfileTreeStats = false;
drawStats = false;
}
@ -42,7 +43,8 @@ struct Settings
bool drawLines;
bool drawTriangles;
bool drawGrid;
bool drawProfile;
bool drawProfileTree;
bool drawProfileTreeStats;
bool drawStats;
};

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -0,0 +1,162 @@
/*
* 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 BEAM_H
#define BEAM_H
#include <testbed/framework/softbody_dragger.h>
class Beam : public Test
{
public:
Beam()
{
// Create soft body
b3SoftBodyDef def;
def.mesh = &m_mesh;
def.density = 0.2f;
def.E = 1000.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 body
{
b3BodyDef bd;
bd.type = e_staticBody;
bd.position.x = -3.5f;
b3Body* b = m_world.CreateBody(bd);
m_wallHull.Set(1.0f, 5.0f, 5.0f);
b3HullShape wallShape;
wallShape.m_hull = &m_wallHull;
b3ShapeDef sd;
sd.shape = &wallShape;
b3Shape* wall = b->CreateShape(sd);
}
b3AABB3 aabb;
aabb.m_lower.Set(-3.0f, -5.0f, -5.0f);
aabb.m_upper.Set(-2.0f, 5.0f, 5.0f);
for (u32 i = 0; i < m_mesh.vertexCount; ++i)
{
b3SoftBodyNode* n = m_body->GetVertexNode(i);
b3Vec3 p = n->GetPosition();
if (aabb.Contains(p))
{
n->SetType(e_staticSoftBodyNode);
}
}
m_bodyDragger = new b3SoftBodyDragger(&m_ray, m_body);
}
~Beam()
{
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 Beam();
}
b3BlockSoftBodyMesh<5, 2, 2> m_mesh;
b3SoftBody* m_body;
b3SoftBodyDragger* m_bodyDragger;
b3BoxHull m_wallHull;
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -0,0 +1,140 @@
/*
* 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 CLOTH_SELF_COLLISION_H
#define CLOTH_SELF_COLLISION_H
class ClothSelfCollision : public Test
{
public:
ClothSelfCollision()
{
// Translate the mesh
for (u32 i = 0; i < m_clothMesh.vertexCount; ++i)
{
m_clothMesh.vertices[i].y += 5.0f;
}
// Create cloth
b3ClothDef def;
def.mesh = &m_clothMesh;
def.density = 1.0f;
def.streching = 100000.0f;
def.thickness = 0.2f;
def.friction = 0.3f;
m_cloth = new b3Cloth(def);
m_cloth->SetGravity(b3Vec3(0.0f, -9.8f, 0.0f));
m_cloth->SetWorld(&m_world);
{
b3BodyDef bd;
bd.type = e_staticBody;
b3Body* b = m_world.CreateBody(bd);
b3CapsuleShape capsuleShape;
capsuleShape.m_centers[0].Set(0.0f, 0.0f, -5.0f);
capsuleShape.m_centers[1].Set(0.0f, 0.0f, 5.0f);
capsuleShape.m_radius = 1.0f;;
b3ShapeDef sd;
sd.shape = &capsuleShape;
sd.friction = 1.0f;
b->CreateShape(sd);
}
m_clothDragger = new b3ClothDragger(&m_ray, m_cloth);
}
~ClothSelfCollision()
{
delete m_cloth;
delete m_clothDragger;
}
void Step()
{
Test::Step();
m_cloth->Step(g_testSettings->inv_hertz, g_testSettings->velocityIterations, g_testSettings->positionIterations);
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 ClothSelfCollision();
}
b3GridClothMesh<10, 10> m_clothMesh;
b3Cloth* m_cloth;
b3ClothDragger* m_clothDragger;
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -50,13 +50,13 @@ public:
g_draw->DrawSegment(pw, pw + wm.points[i].normal, b3Color_white);
}
m_world.DrawShape(m_xfA, m_shapeA);
m_world.DrawShape(m_xfB, m_shapeB);
m_world.DrawShape(m_xfA, m_shapeA, b3Color_black);
m_world.DrawShape(m_xfB, m_shapeB, b3Color_black);
g_draw->Flush();
g_draw->DrawSolidShape(m_shapeA, b3Color(1.0f, 1.0f, 1.0f, 0.25f), m_xfA);
g_draw->DrawSolidShape(m_shapeB, b3Color(1.0f, 1.0f, 1.0f, 0.25f), m_xfB);
m_world.DrawSolidShape(m_xfA, m_shapeA, b3Color(1.0f, 1.0f, 1.0f, 0.25f));
m_world.DrawSolidShape(m_xfB, m_shapeB, b3Color(1.0f, 1.0f, 1.0f, 0.25f));
g_draw->DrawString(b3Color_white, "Left/Right/Up/Down Arrow - Translate shape");
g_draw->DrawString(b3Color_white, "X/Y/Z - Rotate shape");

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -68,8 +68,8 @@ public:
g_draw->DrawTransform(m_xfA);
g_draw->DrawTransform(m_xfB);
m_world.DrawShape(m_xfA, &m_shapeA);
m_world.DrawShape(m_xfB, &m_shapeB);
m_world.DrawShape(m_xfA, &m_shapeA, b3Color_black);
m_world.DrawShape(m_xfB, &m_shapeB, b3Color_black);
g_draw->DrawString(b3Color_white, "Left/Right/Up/Down Arrow - Translate shape");
g_draw->DrawString(b3Color_white, "X/Y/Z - Rotate shape");

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,158 +0,0 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* 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 MASS_SPRING_H
#define MASS_SPRING_H
class MassSpring : public Test
{
public:
MassSpring()
{
m_x.Set(0.0f, 5.0f, 0.0f);
m_v.SetZero();
m_k = 100.0f;
m_iterations = 0;
}
void Solve(float32 h)
{
// ODE
// f(Y) = dY / dt = [v]
// [-k * x]
// 1. Apply Implicit Euler
// Y(t + h) = Y(t) + h * f( Y(t + h) )
// G( Y(t + h) ) = Y(t + h) - Y(t) - h * f( Y(t + h) ) = 0
// 2. Solve G = 0
// Newton-Raphson Iteration
//
// Y(t + h) =
// Y(t + h)_0 - G( Y(t + h)_0 ) / G'( Y(t + h)_0 ) =
// Y(t + h)_0 - G'( Y(t + h)_0 )^-1 * ( Y(t + h)_0 - Y(t) - h * f( Y(t + h)_0 )
// G'( Y ) = I - h * del_f / del_Y
// del_f / del_Y = [del_f1 / del_x del_f1 / del_v] = [0 I]
// [del_f2 / del_x del_f2 / del_v] [-k * I 0]
// G'( Y ) = [I 0] - [0 h * I] = [I -h * I]
// [0 I] [-h * k * I 0] [h * k * I I]
// Compute Jacobian
b3Mat33 I = b3Mat33_identity;
b3Mat33 A, B, C, D;
A = I;
B = -h * I;
C = h * m_k * I;
D = I;
// Invert
// Block matrix inversion
b3Mat33 invD = b3Inverse(D);
b3Mat33 B_invD = B * invD;
b3Mat33 invJ_A = b3Inverse(A - B_invD * C);
b3Mat33 invJ_B = -invJ_A * B_invD;
b3Mat33 invJ_C = -invD * C * invJ_A;
b3Mat33 invJ_D = invD + invD * C * invJ_A * B_invD;
// Initial guess
b3Vec3 f1 = m_v;
b3Vec3 f2 = -m_k * m_x;
b3Vec3 Y1 = m_x + h * f1;
b3Vec3 Y2 = m_v + h * f2;
const float32 kTol = 0.05f;
const u32 kMaxIterations = 20;
float32 eps0 = 0.0f;
float32 eps1 = B3_MAX_FLOAT;
m_iterations = 0;
while (m_iterations < kMaxIterations && eps1 > kTol * kTol * eps0)
{
// Evaluate f(Y_n-1)
f1 = Y2;
f2 = -m_k * Y1;
// Residual vector
b3Vec3 G1 = Y1 - m_x - h * f1;
b3Vec3 G2 = Y2 - m_v - h * f2;
eps1 = b3Dot(G1, G1) + b3Dot(G2, G2);
// Solve Ax = b
b3Vec3 x1 = invJ_A * G1 + invJ_B * G2;
b3Vec3 x2 = invJ_C * G1 + invJ_D * G2;
Y1 -= x1;
Y2 -= x2;
++m_iterations;
}
// Update state
m_x = Y1;
m_v = Y2;
}
void Step()
{
float32 h = g_testSettings->inv_hertz;
Solve(h);
g_draw->DrawSolidSphere(m_x, 0.25f, b3Color_white);
g_draw->DrawSegment(b3Vec3_zero, m_x, b3Color_white);
g_draw->DrawString(b3Color_white, "Iterations = %u", m_iterations);
float32 E = 0.5f * b3Dot(m_v, m_v);
g_draw->DrawString(b3Color_white, "E = %f", E);
}
static Test* Create()
{
return new MassSpring();
}
// State
b3Vec3 m_x, m_v;
// Stiffness
float32 m_k;
//
u32 m_iterations;
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -24,6 +24,8 @@ class MeshContactTest : public Test
public:
MeshContactTest()
{
m_gridMesh.BuildTree();
// Transform grid into a terrain
for (u32 i = 0; i < m_terrainMesh.vertexCount; ++i)
{
@ -37,7 +39,7 @@ public:
m_ground = m_world.CreateBody(bd);
b3MeshShape ms;
ms.m_mesh = &m_groundMesh;
ms.m_mesh = &m_gridMesh;
b3ShapeDef sd;
sd.shape = &ms;
@ -138,7 +140,7 @@ public:
if (key == GLFW_KEY_G)
{
b3MeshShape ms;
ms.m_mesh = &m_groundMesh;
ms.m_mesh = &m_gridMesh;
b3ShapeDef sd;
sd.shape = &ms;
@ -176,7 +178,8 @@ public:
}
b3GridMesh<25, 25> m_terrainMesh;
b3GridMesh<25, 25> m_gridMesh;
b3Body* m_ground;
b3Body* m_body;
};

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -19,32 +19,23 @@
#ifndef PINNED_CLOTH_H
#define PINNED_CLOTH_H
class PinnedCloth : public ClothTest
class PinnedCloth : public Test
{
public:
PinnedCloth() : m_rectangleGarment(5.0f, 5.0f)
PinnedCloth()
{
// 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_rectangleClothMesh.vertices[i] = dq * m_rectangleClothMesh.vertices[i];
}
// Create cloth
b3ClothDef def;
def.mesh = &m_rectangleClothMesh;
def.mesh = &m_clothMesh;
def.density = 0.2f;
def.structural = 100000.0f;
def.damping = 0.0f;
def.streching = 100000.0f;
m_cloth = m_world.CreateCloth(def);
m_cloth = new b3Cloth(def);
m_cloth->SetGravity(b3Vec3(0.0f, -9.8f, 0.0f));
m_cloth->SetWorld(&m_world);
// Freeze some particles
b3AABB3 aabb1;
aabb1.m_lower.Set(-5.0f, -1.0f, -6.0f);
aabb1.m_upper.Set(5.0f, 1.0f, -4.0f);
@ -65,23 +56,70 @@ public:
p->SetType(e_staticParticle);
}
}
m_clothDragger = new b3ClothDragger(&m_ray, m_cloth);
}
~PinnedCloth()
{
delete m_clothDragger;
delete m_cloth;
}
void Step()
{
Test::Step();
m_cloth->Step(g_testSettings->inv_hertz, g_testSettings->velocityIterations, g_testSettings->positionIterations);
m_cloth->Draw();
if (m_clothDragger->IsDragging())
{
b3BodyDef bd;
bd.type = e_dynamicBody;
bd.position.Set(0.0f, 5.0f, 0.0f);
b3Vec3 pA = m_clothDragger->GetPointA();
b3Vec3 pB = m_clothDragger->GetPointB();
b3Body* b = m_world.CreateBody(bd);
g_draw->DrawPoint(pA, 2.0f, b3Color_green);
b3SphereShape sphere;
sphere.m_center.SetZero();
sphere.m_radius = 2.0f;
g_draw->DrawPoint(pB, 2.0f, b3Color_green);
b3ShapeDef sd;
sd.shape = &sphere;
sd.density = 1.0f;
g_draw->DrawSegment(pA, pB, b3Color_white);
}
b3Shape* s = b->CreateShape(sd);
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();
}
}
@ -90,9 +128,9 @@ public:
return new PinnedCloth();
}
b3RectangleGarment m_rectangleGarment;
b3GarmentMesh m_rectangleGarmentMesh;
b3GarmentClothMesh m_rectangleClothMesh;
b3GridClothMesh<10, 10> m_clothMesh;
b3Cloth* m_cloth;
b3ClothDragger* m_clothDragger;
};
#endif

View File

@ -0,0 +1,144 @@
/*
* 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 = 1000.0f;
def.nu = 0.33f;
def.c_yield = 0.1f;
def.c_creep = 0.5f;
def.c_max = 1.0f;
m_body = new b3SoftBody(def);
for (u32 i = 0; i < m_mesh.vertexCount; ++i)
{
b3SoftBodyNode* n = m_body->GetVertexNode(i);
n->SetMassDamping(0.2f);
}
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

View File

@ -1,98 +0,0 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* 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 POINT_CLICK_H
#define POINT_CLICK_H
class PointClick : public Test
{
public:
PointClick()
{
{
b3BodyDef bdef;
b3Body* ground = m_world.CreateBody(bdef);
b3MeshShape ms;
ms.m_mesh = &m_groundMesh;
b3ShapeDef sd;
sd.shape = &ms;
ground->CreateShape(sd);
}
{
b3BodyDef bdef;
bdef.type = b3BodyType::e_dynamicBody;
bdef.fixedRotationY = true;
bdef.position.Set(0.0f, 5.0f, 0.0f);
m_character = m_world.CreateBody(bdef);
b3CapsuleShape cap;
cap.m_centers[0].Set(0.0f, 1.0f, 0.0f);
cap.m_centers[1].Set(0.0f, -1.0f, 0.0f);
cap.m_radius = 1.0f;
b3ShapeDef sdef;
sdef.shape = &cap;
sdef.density = 1.0f;
sdef.friction = 0.5f;
m_character->CreateShape(sdef);
}
}
void BeginDragging()
{
if (m_bodyDragger.GetBody() == m_character)
{
m_bodyDragger.StopDragging();
}
}
void Step()
{
if (m_bodyDragger.IsDragging())
{
if (m_bodyDragger.GetBody() != m_character)
{
b3Vec3 p1 = m_character->GetPosition();
b3Vec3 p2 = m_bodyDragger.GetPointA();
b3Vec3 n = b3Normalize(p2 - p1);
const float32 k = 1000.0f;
b3Vec3 f = k * n;
m_character->ApplyForceToCenter(f, true);
}
}
Test::Step();
}
static Test* Create()
{
return new PointClick();
}
b3Body* m_character;
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -178,8 +178,8 @@ public:
void CastRay(const b3Vec3 p1, const b3Vec3 p2) const
{
b3RayCastSingleShapeOutput out;
if (m_world.RayCastSingleShape(&out, p1, p2))
b3RayCastSingleOutput out;
if (m_world.RayCastSingle(&out, p1, p2))
{
g_draw->DrawSegment(p1, out.point, b3Color_green);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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 hebd liable for any damages

View File

@ -1,82 +0,0 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* 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 SELF_COLLISION_H
#define SELF_COLLISION_H
class SelfCollision : public ClothTest
{
public:
SelfCollision() : m_rectangleGarment(5.0f, 5.0f)
{
// Generate 2D mesh
m_rectangleGarmentMesh.Set(&m_rectangleGarment, 1.0f);
// Create 3D mesh
m_rectangleClothMesh.Set(&m_rectangleGarmentMesh);
b3Mat33 Rx = b3Mat33RotationX(0.5f * B3_PI);
for (u32 i = 0; i < m_rectangleClothMesh.vertexCount; ++i)
{
m_rectangleClothMesh.vertices[i] = Rx * m_rectangleClothMesh.vertices[i];
m_rectangleClothMesh.vertices[i].y += 5.0f;
}
b3ClothDef def;
def.mesh = &m_rectangleClothMesh;
def.density = 1.0f;
def.structural = 100000.0f;
m_cloth = m_world.CreateCloth(def);
for (b3Particle* p = m_cloth->GetParticleList().m_head; p; p = p->GetNext())
{
p->SetRadius(0.2f);
p->SetFriction(0.2f);
}
{
b3BodyDef bd;
bd.type = e_staticBody;
b3Body* b = m_world.CreateBody(bd);
b3CapsuleShape capsuleShape;
capsuleShape.m_centers[0].Set(0.0f, 0.0f, -5.0f);
capsuleShape.m_centers[1].Set(0.0f, 0.0f, 5.0f);
capsuleShape.m_radius = 1.0f;;
b3ShapeDef sd;
sd.shape = &capsuleShape;
sd.friction = 1.0f;
b->CreateShape(sd);
}
}
static Test* Create()
{
return new SelfCollision();
}
b3RectangleGarment m_rectangleGarment;
b3GarmentMesh m_rectangleGarmentMesh;
b3GarmentClothMesh m_rectangleClothMesh;
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -0,0 +1,121 @@
#ifndef SHAPE_CAST_H
#define SHAPE_CAST_H
class ShapeCast : public Test
{
public:
ShapeCast()
{
m_shapeA.m_hull = &b3BoxHull_identity;
m_shapeA.m_radius = 0.0f;
m_shapeB.m_hull = &b3BoxHull_identity;
m_shapeB.m_radius = 0.0f;
m_xfA.position.Set(-5.0f, 0.0f, 0.0f);
m_xfA.rotation.SetIdentity();
m_xfB.position.Set(10.0f, 0.0f, 0.0f);
m_xfB.rotation.SetIdentity();
m_proxyA.Set(&m_shapeA, 0);
m_proxyB.Set(&m_shapeB, 0);
}
void Step()
{
g_draw->DrawString(b3Color_white, "Left/Right/Up/Down Arrow - Translate shape");
g_draw->DrawString(b3Color_white, "X/Y/Z - Rotate shape");
g_draw->DrawTransform(m_xfA);
g_draw->DrawTransform(m_xfB);
m_world.DrawShape(m_xfA, &m_shapeA, b3Color_black);
m_world.DrawShape(m_xfB, &m_shapeB, b3Color_black);
m_world.DrawSolidShape(m_xfA, &m_shapeA, b3Color_white);
m_world.DrawSolidShape(m_xfB, &m_shapeB, b3Color_white);
b3Vec3 translationB = -100.0f * b3Vec3_x;
g_draw->DrawSegment(m_xfB.position, m_xfB.position + translationB, b3Color_white);
b3GJKShapeCastOutput out;
bool hit = b3GJKShapeCast(&out, m_xfA, m_proxyA, m_xfB, m_proxyB, translationB);
g_draw->DrawString(b3Color_white, "Iterations = %d", out.iterations);
if (hit)
{
g_draw->DrawPoint(out.point, 4.0f, b3Color_green);
g_draw->DrawSegment(out.point, out.point + out.normal, b3Color_green);
b3Transform xfB;
xfB.rotation = m_xfB.rotation;
xfB.position = m_xfB.position + out.t * translationB;
m_world.DrawShape(xfB, &m_shapeB, b3Color_black);
}
}
void KeyDown(int key)
{
if (key == GLFW_KEY_LEFT)
{
m_xfB.position.x -= 0.105f;
}
if (key == GLFW_KEY_RIGHT)
{
m_xfB.position.x += 0.105f;
}
if (key == GLFW_KEY_UP)
{
m_xfB.position.y += 0.105f;
}
if (key == GLFW_KEY_DOWN)
{
m_xfB.position.y -= 0.105f;
}
if (key == GLFW_KEY_X)
{
b3Quat qx(b3Vec3(1.0f, 0.0f, 0.0f), 0.05f * B3_PI);
b3Mat33 xfx = b3QuatMat33(qx);
m_xfB.rotation = m_xfB.rotation * xfx;
}
if (key == GLFW_KEY_Y)
{
b3Quat qy(b3Vec3(0.0f, 1.0f, 0.0f), 0.05f * B3_PI);
b3Mat33 xfy = b3QuatMat33(qy);
m_xfB.rotation = m_xfB.rotation * xfy;
}
if (key == GLFW_KEY_Z)
{
b3Quat qy(b3Vec3(0.0f, 0.0f, 1.0f), 0.05f * B3_PI);
b3Mat33 xfz = b3QuatMat33(qy);
m_xfB.rotation = m_xfB.rotation * xfz;
}
}
static Test* Create()
{
return new ShapeCast();
}
b3HullShape m_shapeA;
b3Transform m_xfA;
b3ShapeGJKProxy m_proxyA;
b3HullShape m_shapeB;
b3Transform m_xfB;
b3ShapeGJKProxy m_proxyB;
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,73 +0,0 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* 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 SHIRT_H
#define SHIRT_H
class Shirt : public ClothTest
{
public:
Shirt()
{
// Generate 2D mesh
m_shirtGarmentMesh.Set(&m_shirtGarment, 0.1f);
// Create 3D mesh
m_shirtClothMesh.Set(&m_shirtGarmentMesh);
// Perform fitting
for (u32 i = 0; i < 3; ++i)
{
b3ClothMeshMesh* front = m_shirtClothMesh.meshes + i;
for (u32 j = 0; j < front->vertexCount; ++j)
{
u32 v = front->startVertex + j;
m_shirtClothMesh.vertices[v].z = -1.0f;
}
}
for (u32 i = 3; i < 6; ++i)
{
b3ClothMeshMesh* back = m_shirtClothMesh.meshes + i;
for (u32 j = 0; j < back->vertexCount; ++j)
{
u32 v = back->startVertex + j;
m_shirtClothMesh.vertices[v].z = 1.0f;
}
}
// Create cloth
b3ClothDef def;
def.mesh = &m_shirtClothMesh;
def.density = 0.2f;
def.structural = 10000.0f;
m_cloth = m_world.CreateCloth(def);
}
static Test* Create()
{
return new Shirt();
}
b3ShirtGarment m_shirtGarment;
b3GarmentMesh m_shirtGarmentMesh;
b3GarmentClothMesh m_shirtClothMesh;
};
#endif

View File

@ -1,99 +0,0 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* 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 PENDULUM_H
#define PENDULUM_H
class SinglePendulum : public Test
{
public:
SinglePendulum()
{
m_g = -10.0f;
m_r = 10.0f;
m_m = 1.0f;
m_I = m_m * m_r * m_r;
// Initial state
m_theta = -0.5f * B3_PI;
m_omega = 0.0f;
}
void Step()
{
float32 h = g_testSettings->inv_hertz;
// Solution (acceleration)
float32 omega_dot = -m_g / m_r * sin(m_theta);
// Integrate acceleration
m_omega += h * omega_dot;
// Integrate velocity
m_theta += h * m_omega;
// Convert from polar coordinates (r, theta) to Cartesian coordinates (x, y)
b3Vec3 c;
c.x = m_r * sin(m_theta);
c.y = m_r * cos(m_theta);
c.z = 0.0f;
g_draw->DrawSolidSphere(c, 1.0f, b3Color_white);
b3Vec3 pole;
pole.SetZero();
g_draw->DrawSegment(pole, c, b3Color_white);
// Kinetic energy
float32 T = 0.5f * m_I * m_omega * m_omega;
// Potential energy
float32 V = -m_m * m_g * m_r * cos(m_theta);
// Lagrangian
float32 L = T - V;
//
g_draw->DrawString(b3Color_white, "T = %f \nV = %f \nL = %f", T, V, L);
}
static Test* Create()
{
return new SinglePendulum();
}
// Gravity
float32 m_g;
// Mass, inertia
float32 m_m, m_I;
// Radial coordinate
float32 m_r;
// The allowable generalized coordinate in polar coordinate frame.
// Only motions satisfying the constraints can be described
// in this frame. Therefore, all solutions satisfy the constraints.
// This is the so called reduced coordinates approach.
float32 m_theta;
// Velocity
float32 m_omega;
};
#endif

View File

@ -0,0 +1,175 @@
/*
* 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;
def.c_yield = 0.6f;
def.c_creep = 1.0f;
def.c_max = 1.0f;
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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -19,39 +19,31 @@
#ifndef TABLE_CLOTH_H
#define TABLE_CLOTH_H
class TableCloth : public ClothTest
class TableCloth : public Test
{
public:
TableCloth() : m_rectangleGarment(5.0f, 5.0f)
TableCloth()
{
// 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)
// Translate the mesh
for (u32 i = 0; i < m_clothMesh.vertexCount; ++i)
{
m_rectangleClothMesh.vertices[i] = dq * m_rectangleClothMesh.vertices[i];
m_rectangleClothMesh.vertices[i].y += 5.0f;
m_clothMesh.vertices[i].y += 5.0f;
}
// Create cloth
b3ClothDef def;
def.mesh = &m_rectangleClothMesh;
def.mesh = &m_clothMesh;
def.density = 0.2f;
def.bending = 10000.0f;
def.structural = 10000.0f;
def.damping = 0.0f;
def.streching = 10000.0f;
//def.shearing = 10000.0f;
def.damping = 100.0f;
def.thickness = 0.2f;
def.friction = 0.1f;
m_cloth = m_world.CreateCloth(def);
m_cloth = new b3Cloth(def);
for (b3Particle* p = m_cloth->GetParticleList().m_head; p; p = p->GetNext())
{
p->SetRadius(0.2f);
p->SetFriction(0.2f);
}
m_cloth->SetGravity(b3Vec3(0.0f, -9.8f, 0.0f));
m_cloth->SetWorld(&m_world);
{
b3BodyDef bd;
@ -63,7 +55,6 @@ public:
b3HullShape tableShape;
tableShape.m_hull = &m_tableHull;
tableShape.m_radius = 0.2f;
b3ShapeDef sd;
sd.shape = &tableShape;
@ -71,6 +62,71 @@ public:
b->CreateShape(sd);
}
m_clothDragger = new b3ClothDragger(&m_ray, m_cloth);
}
~TableCloth()
{
delete m_clothDragger;
delete m_cloth;
}
void Step()
{
Test::Step();
m_cloth->Step(g_testSettings->inv_hertz, g_testSettings->velocityIterations, g_testSettings->positionIterations);
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()
@ -78,10 +134,10 @@ public:
return new TableCloth();
}
b3RectangleGarment m_rectangleGarment;
b3GarmentMesh m_rectangleGarmentMesh;
b3GarmentClothMesh m_rectangleClothMesh;
b3GridClothMesh<10, 10> m_clothMesh;
b3Cloth* m_cloth;
b3ClothDragger* m_clothDragger;
b3QHull m_tableHull;
};

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -55,31 +55,25 @@ static inline b3Color Color(float32 x, float32 a, float32 b)
return c;
}
class TensionMapping : public ClothTest
class TensionMapping : public Test
{
public:
TensionMapping() : m_rectangleGarment(5.0f, 5.0f)
TensionMapping()
{
// 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_rectangleClothMesh.vertices[i] = dq * m_rectangleClothMesh.vertices[i];
}
// Create cloth
b3ClothDef def;
def.mesh = &m_rectangleClothMesh;
def.mesh = &m_clothMesh;
def.density = 0.2f;
def.structural = 10000.0f;
def.streching = 10000.0f;
def.shearing = 5000.0f;
def.damping = 100.0f;
m_cloth = m_world.CreateCloth(def);
m_cloth = new b3Cloth(def);
m_cloth->SetGravity(b3Vec3(0.0f, -9.8f, 0.0f));
m_cloth->SetWorld(&m_world);
// Freeze some particles
b3AABB3 aabb;
aabb.m_lower.Set(-5.0f, -1.0f, -6.0f);
aabb.m_upper.Set(5.0f, 1.0f, -4.0f);
@ -91,12 +85,22 @@ public:
p->SetType(e_staticParticle);
}
}
m_clothDragger = new b3ClothDragger(&m_ray, m_cloth);
}
~TensionMapping()
{
delete m_clothDragger;
delete m_cloth;
}
void Step()
{
Test::Step();
m_cloth->Step(g_testSettings->inv_hertz, g_testSettings->velocityIterations, g_testSettings->positionIterations);
const b3ClothMesh* mesh = m_cloth->GetMesh();
b3StackArray<b3Vec3, 256> tension;
@ -108,29 +112,45 @@ public:
for (b3Force* f = m_cloth->GetForceList().m_head; f; f = f->GetNext())
{
if (f->GetType() == e_springForce)
if (f->GetType() == e_strechForce)
{
b3SpringForce* s = (b3SpringForce*)f;
b3StrechForce* s = (b3StrechForce*)f;
b3ClothTriangle* triangle = s->GetTriangle();
u32 triangleIndex = triangle->GetTriangle();
b3ClothMeshTriangle* mesh_triangle = m_clothMesh.triangles + triangleIndex;
u32 v1 = mesh_triangle->v1;
u32 v2 = mesh_triangle->v2;
u32 v3 = mesh_triangle->v3;
u32 v1 = s->GetParticle1()->GetVertex();
u32 v2 = s->GetParticle2()->GetVertex();
b3Vec3 f1 = s->GetActionForce1();
b3Vec3 f2 = s->GetActionForce2();
b3Vec3 f3 = s->GetActionForce3();
tension[v1] += s->GetActionForce();
tension[v2] -= s->GetActionForce();
tension[v1] += f1;
tension[v2] += f2;
tension[v3] += f3;
}
}
for (u32 i = 0; i < m_rectangleClothMesh.triangleCount; ++i)
for (u32 i = 0; i < mesh->triangleCount; ++i)
{
b3ClothMeshTriangle* t = m_rectangleClothMesh.triangles + i;
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();
g_draw->DrawTriangle(v1, v2, v3, b3Color_black);
b3Draw_draw->DrawSegment(v1, v2, b3Color_black);
b3Draw_draw->DrawSegment(v2, v3, b3Color_black);
b3Draw_draw->DrawSegment(v3, v1, b3Color_black);
b3Vec3 c = (v1 + v2 + v3) / 3.0f;
float32 s = 0.9f;
v1 = s * (v1 - c) + c;
v2 = s * (v2 - c) + c;
v3 = s * (v3 - c) + c;
b3Vec3 f1 = tension[t->v1];
float32 L1 = b3Length(f1);
@ -143,7 +163,7 @@ public:
float32 L = (L1 + L2 + L3) / 3.0f;
const float32 kMaxT = 100000.0f;
const float32 kMaxT = 10000.0f;
b3Color color = Color(L, 0.0f, kMaxT);
b3Vec3 n1 = b3Cross(v2 - v1, v3 - v1);
@ -151,29 +171,66 @@ public:
g_draw->DrawSolidTriangle(n1, v1, v2, v3, color);
b3Vec3 n2 = -n1;
g_draw->DrawSolidTriangle(n2, v1, v3, v2, color);
g_draw->DrawSolidTriangle(n2, v3, v2, v1, color);
}
if (m_clothDragger.IsDragging() == true)
if (m_clothDragger->IsDragging())
{
g_draw->DrawSegment(m_clothDragger.GetPointA(), m_clothDragger.GetPointB(), b3Color_white);
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 = %u", 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 TensionMapping();
}
b3RectangleGarment m_rectangleGarment;
b3GarmentMesh m_rectangleGarmentMesh;
b3GarmentClothMesh m_rectangleClothMesh;
b3GridClothMesh<10, 10> m_clothMesh;
b3Cloth* m_cloth;
b3ClothDragger* m_clothDragger;
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -56,23 +56,32 @@
#include <bounce/dynamics/contacts/convex_contact.h>
#include <bounce/dynamics/contacts/mesh_contact.h>
#include <bounce/dynamics/rope/rope.h>
#include <bounce/garment/sewing_pattern.h>
#include <bounce/garment/garment.h>
#include <bounce/garment/garment_mesh.h>
#include <bounce/dynamics/cloth/cloth_mesh.h>
#include <bounce/dynamics/cloth/cloth.h>
#include <bounce/dynamics/cloth/particle.h>
#include <bounce/dynamics/cloth/spring_force.h>
#include <bounce/dynamics/body.h>
#include <bounce/dynamics/world.h>
#include <bounce/dynamics/world_listeners.h>
#include <bounce/controllers/cloth_dragger.h>
#include <bounce/controllers/body_dragger.h>
#include <bounce/rope/rope.h>
#include <bounce/cloth/cloth_mesh.h>
#include <bounce/cloth/grid_cloth_mesh.h>
#include <bounce/cloth/garment_cloth_mesh.h>
#include <bounce/cloth/cloth.h>
#include <bounce/cloth/particle.h>
#include <bounce/cloth/cloth_triangle.h>
#include <bounce/cloth/forces/strech_force.h>
#include <bounce/cloth/forces/shear_force.h>
#include <bounce/cloth/forces/spring_force.h>
#include <bounce/cloth/forces/mouse_force.h>
#include <bounce/cloth/garment/sewing_pattern.h>
#include <bounce/cloth/garment/garment.h>
#include <bounce/cloth/garment/garment_mesh.h>
#include <bounce/softbody/softbody_mesh.h>
#include <bounce/softbody/block_softbody_mesh.h>
#include <bounce/softbody/softbody.h>
#include <bounce/softbody/softbody_node.h>
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -19,21 +19,21 @@
#ifndef B3_CLOTH_H
#define B3_CLOTH_H
#include <bounce/common/math/transform.h>
#include <bounce/common/template/list.h>
#include <bounce/common/memory/stack_allocator.h>
#include <bounce/common/memory/block_pool.h>
#include <bounce/common/math/transform.h>
#include <bounce/cloth/cloth_contact_manager.h>
class b3World;
class b3Shape;
class b3Particle;
class b3Force;
class b3BodyContact;
class b3ParticleContact;
class b3TriangleContact;
struct b3ParticleDef;
class b3Particle;
struct b3ForceDef;
class b3Force;
class b3ClothTriangle;
struct b3ClothMesh;
@ -57,33 +57,62 @@ struct b3ClothDef
{
mesh = nullptr;
density = 0.0f;
structural = 0.0f;
streching = 0.0f;
shearing = 0.0f;
bending = 0.0f;
sewing = 0.0f;
damping = 0.0f;
thickness = 0.0f;
friction = 0.2f;
}
// Cloth mesh
const b3ClothMesh* mesh;
// Cloth density in kg/m^3
// Cloth density in kg/m^2
float32 density;
// Structural stiffness
float32 structural;
// Streching stiffness
float32 streching;
// Shearing stiffness
float32 shearing;
// Bending stiffness
float32 bending;
// Sewing stiffness
float32 sewing;
// Damping stiffness
float32 damping;
// Cloth thickness
float32 thickness;
// Cloth coefficient of friction
float32 friction;
};
// A cloth represents a deformable surface as a collection of particles.
// Particles may be connected with each other.
// Particles may be connected with each other by springs.
class b3Cloth
{
public:
// Get the world the cloth belongs to.
b3Cloth(const b3ClothDef& def);
~b3Cloth();
// Set the acceleration of gravity.
void SetGravity(const b3Vec3& gravity);
// Get the acceleration of gravity.
b3Vec3 GetGravity() const;
// Attach a world to this cloth.
// The cloth will be able to respond to collisions with the rigid bodies in the attached world.
void SetWorld(b3World* world);
// Get the world attached to this cloth.
const b3World* GetWorld() const;
b3World* GetWorld();
@ -99,9 +128,6 @@ public:
// Destroy a given force.
void DestroyForce(b3Force* force);
// Perform a ray cast with the cloth.
void RayCast(b3RayCastListener* listener, const b3Vec3& p1, const b3Vec3& p2);
// Perform a ray cast with the cloth.
bool RayCastSingle(b3ClothRayCastSingleOutput* output, const b3Vec3& p1, const b3Vec3& p2) const;
@ -111,8 +137,11 @@ 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 the vertex index.
b3Particle* GetParticle(u32 i);
// Return the cloth triangle given the triangle index.
b3ClothTriangle* GetTriangle(u32 i);
// Return the list of particles in this cloth.
const b3List2<b3Particle>& GetParticleList() const;
@ -123,49 +152,43 @@ public:
// Return the kinetic (or dynamic) energy in this system.
float32 GetEnergy() const;
// Get the next cloth in the world cloth list.
const b3Cloth* GetNext() const;
// Get the next cloth in the world cloth list.
b3Cloth* GetNext();
// Perform a time step.
void Step(float32 dt, u32 velocityIterations, u32 positionIterations);
// Debug draw the cloth using the associated cloth mesh.
void Draw() const;
private:
friend class b3World;
friend class b3List2<b3Cloth>;
b3Cloth(const b3ClothDef& def, b3World* world);
~b3Cloth();
// Perform a time step.
// Called only inside b3World.
void Step(float32 dt, const b3Vec3& gravity);
friend class b3Particle;
friend class b3ClothTriangle;
friend class b3ShearForce;
friend class b3StrechForce;
friend class b3SpringForce;
friend class b3MouseForce;
friend class b3ClothContactManager;
// Compute mass of each particle.
void ComputeMass();
// Update body contacts.
void UpdateBodyContacts();
// Update particle contacts.
void UpdateParticleContacts();
// Update triangle contacts.
void UpdateTriangleContacts();
// Update contacts
void UpdateContacts();
// Solve
void Solve(float32 dt, const b3Vec3& gravity);
void Solve(float32 dt, const b3Vec3& gravity, u32 velocityIterations, u32 positionIterations);
// Stack allocator
b3StackAllocator m_stackAllocator;
// The world attached to this cloth
b3World* m_world;
// Gravity acceleration
b3Vec3 m_gravity;
// Proxy mesh
const b3ClothMesh* m_mesh;
// Vertex particles
b3Particle** m_vertexParticles;
// Particles
b3Particle** m_particles;
// Triangles
b3ClothTriangle* m_triangles;
// Cloth density
float32 m_density;
@ -173,38 +196,26 @@ private:
// Pool of particles
b3BlockPool m_particleBlocks;
// Pool of body contacts
b3BlockPool m_bodyContactBlocks;
// Pool of particle contacts
b3BlockPool m_particleContactBlocks;
// Pool of triangle contacts
b3BlockPool m_triangleContactBlocks;
// List of particles
b3List2<b3Particle> m_particleList;
// List of forces
b3List2<b3Force> m_forceList;
// List of particle contacts
b3List2<b3BodyContact> m_bodyContactList;
// List of particle contacts
b3List2<b3ParticleContact> m_particleContactList;
// List of triangle contacts
b3List2<b3TriangleContact> m_triangleContactList;
// The parent world of this cloth.
b3World* m_world;
// Links to the world cloth list.
b3Cloth* m_prev;
b3Cloth* m_next;
// Contact manager
b3ClothContactManager m_contactManager;
};
inline void b3Cloth::SetGravity(const b3Vec3& gravity)
{
m_gravity = gravity;
}
inline b3Vec3 b3Cloth::GetGravity() const
{
return m_gravity;
}
inline const b3World* b3Cloth::GetWorld() const
{
return m_world;
@ -230,14 +241,4 @@ inline const b3List2<b3Force>& b3Cloth::GetForceList() const
return m_forceList;
}
inline const b3Cloth* b3Cloth::GetNext() const
{
return m_next;
}
inline b3Cloth* b3Cloth::GetNext()
{
return m_next;
}
#endif

View File

@ -0,0 +1,38 @@
/*
* 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_CLOTH_COLLISION_H
#define B3_CLOTH_COLLISION_H
#include <bounce/common/math/vec3.h>
// Cloth primitive type
enum b3ClothAABBProxyType
{
e_particleProxy,
e_triangleProxy
};
// Cloth primitive broadphase proxy
struct b3ClothAABBProxy
{
b3ClothAABBProxyType type;
void* owner;
};
#endif

View File

@ -0,0 +1,63 @@
/*
* 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_CLOTH_CONTACT_MANAGER_H
#define B3_CLOTH_CONTACT_MANAGER_H
#include <bounce/cloth/contacts/cloth_particle_body_contact.h>
#include <bounce/cloth/contacts/cloth_particle_triangle_contact.h>
#include <bounce/collision/broad_phase.h>
#include <bounce/common/memory/block_pool.h>
#include <bounce/common/template/list.h>
class b3Cloth;
// Contact delegator for b3Cloth.
class b3ClothContactManager
{
public:
b3ClothContactManager();
void FindNewContacts();
void AddPair(void* data1, void* data2);
void FindNewClothContacts();
void AddPSPair(b3Particle* p1, b3Shape* s2);
void FindNewBodyContacts();
void UpdateContacts();
void UpdateClothContacts();
void UpdateBodyContacts();
b3ParticleTriangleContact* CreateParticleTriangleContact();
void Destroy(b3ParticleTriangleContact* c);
b3ParticleBodyContact* CreateParticleBodyContact();
void Destroy(b3ParticleBodyContact* c);
b3BlockPool m_particleTriangleContactBlocks;
b3BlockPool m_particleBodyContactBlocks;
b3Cloth* m_cloth;
b3BroadPhase m_broadPhase;
b3List2<b3ParticleTriangleContact> m_particleTriangleContactList;
b3List2<b3ParticleBodyContact> m_particleBodyContactList;
};
#endif

View File

@ -0,0 +1,77 @@
/*
* 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_CLOTH_FORCE_SOLVER_H
#define B3_CLOTH_FORCE_SOLVER_H
#include <bounce/common/math/mat22.h>
#include <bounce/common/math/mat33.h>
class b3StackAllocator;
class b3Particle;
class b3Force;
struct b3DenseVec3;
struct b3DiagMat33;
struct b3SparseMat33;
struct b3SparseMat33View;
struct b3ClothForceSolverDef
{
b3StackAllocator* stack;
u32 particleCount;
b3Particle** particles;
u32 forceCount;
b3Force** forces;
};
struct b3ClothForceSolverData
{
b3DenseVec3* x;
b3DenseVec3* v;
b3DenseVec3* f;
b3DenseVec3* y;
b3SparseMat33* dfdx;
b3SparseMat33* dfdv;
b3DiagMat33* S;
b3DenseVec3* z;
};
class b3ClothForceSolver
{
public:
b3ClothForceSolver(const b3ClothForceSolverDef& def);
~b3ClothForceSolver();
void Solve(float32 dt, const b3Vec3& gravity);
private:
void ApplyForces();
b3StackAllocator* m_allocator;
u32 m_particleCount;
b3Particle** m_particles;
u32 m_forceCount;
b3Force** m_forces;
b3ClothForceSolverData m_solverData;
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -19,7 +19,6 @@
#ifndef B3_CLOTH_MESH_H
#define B3_CLOTH_MESH_H
#include <bounce/common/math/vec2.h>
#include <bounce/common/math/vec3.h>
struct b3ClothMeshTriangle
@ -53,16 +52,4 @@ struct b3ClothMesh
b3ClothMeshSewingLine* sewingLines;
};
struct b3GarmentMesh;
// Convenience structure.
struct b3GarmentClothMesh : public b3ClothMesh
{
b3GarmentClothMesh();
~b3GarmentClothMesh();
// Set this mesh from a 2D garment mesh.
void Set(const b3GarmentMesh* garment);
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -25,16 +25,9 @@
class b3StackAllocator;
class b3Particle;
class b3Body;
class b3Force;
struct b3DenseVec3;
struct b3DiagMat33;
struct b3SparseSymMat33;
class b3BodyContact;
class b3ParticleContact;
class b3TriangleContact;
class b3ParticleBodyContact;
class b3ParticleTriangleContact;
struct b3ClothSolverDef
{
@ -42,33 +35,9 @@ struct b3ClothSolverDef
u32 particleCapacity;
u32 forceCapacity;
u32 bodyContactCapacity;
u32 particleContactCapacity;
u32 triangleContactCapacity;
};
struct b3ClothSolverData
{
b3DenseVec3* x;
b3DenseVec3* v;
b3DenseVec3* f;
b3DenseVec3* y;
b3SparseSymMat33* dfdx;
b3SparseSymMat33* dfdv;
b3DiagMat33* S;
b3DenseVec3* z;
float32 dt;
float32 invdt;
};
struct b3AccelerationConstraint
{
u32 i1;
u32 ndof;
b3Vec3 p, q, z;
void Apply(const b3ClothSolverData* data);
};
class b3ClothSolver
{
public:
@ -77,21 +46,11 @@ public:
void Add(b3Particle* p);
void Add(b3Force* f);
void Add(b3BodyContact* c);
void Add(b3ParticleContact* c);
void Add(b3TriangleContact* c);
void Add(b3ParticleBodyContact* c);
void Add(b3ParticleTriangleContact* c);
void Solve(float32 dt, const b3Vec3& gravity);
void Solve(float32 dt, const b3Vec3& gravity, u32 velocityIterations, u32 positionIterations);
private:
// Apply forces.
void ApplyForces();
// Apply constraints.
void ApplyConstraints();
// Solve Ax = b.
void Solve(b3DenseVec3& x, u32& iterations, const b3SparseSymMat33& A, const b3DenseVec3& b, const b3DiagMat33& S, const b3DenseVec3& z, const b3DenseVec3& y) const;
b3StackAllocator* m_allocator;
u32 m_particleCapacity;
@ -101,24 +60,14 @@ private:
u32 m_forceCapacity;
u32 m_forceCount;
b3Force** m_forces;
u32 m_constraintCapacity;
u32 m_constraintCount;
b3AccelerationConstraint* m_constraints;
u32 m_bodyContactCapacity;
u32 m_bodyContactCount;
b3BodyContact** m_bodyContacts;
b3ParticleBodyContact** m_bodyContacts;
u32 m_particleContactCapacity;
u32 m_particleContactCount;
b3ParticleContact** m_particleContacts;
u32 m_triangleContactCapacity;
u32 m_triangleContactCount;
b3TriangleContact** m_triangleContacts;
b3ClothSolverData m_solverData;
b3ParticleTriangleContact** m_triangleContacts;
};
#endif

View File

@ -0,0 +1,112 @@
/*
* 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_CLOTH_TRIANGLE_H
#define B3_CLOTH_TRIANGLE_H
#include <bounce/cloth/cloth_collision.h>
// A cloth triangle
class b3ClothTriangle
{
public:
// Return the triangle index.
u32 GetTriangle() const;
// Set the triangle radius.
void SetRadius(float32 radius);
// Return the triangle radius.
float32 GetRadius() const;
// Set the triangle coefficient of friction.
void SetFriction(float32 friction);
// Return the triangle coefficient of friction.
float32 GetFriction() const;
private:
friend class b3Cloth;
friend class b3Particle;
friend class b3ShearForce;
friend class b3StrechForce;
friend class b3MouseForce;
friend class b3ClothContactManager;
friend class b3ParticleTriangleContact;
friend class b3ClothSolver;
friend class b3ClothContactSolver;
b3ClothTriangle() { }
~b3ClothTriangle() { }
// Synchronize AABB
void Synchronize(const b3Vec3& displacement);
// Cloth
b3Cloth* m_cloth;
// Triangle index
u32 m_triangle;
// Radius
float32 m_radius;
// Coefficient of friction
float32 m_friction;
// AABB Proxy
b3ClothAABBProxy m_aabbProxy;
// Broadphase ID
u32 m_broadPhaseId;
// Alpha
float32 m_alpha;
// Strech matrix
float32 m_du1, m_dv1;
float32 m_du2, m_dv2;
float32 m_inv_det;
};
inline u32 b3ClothTriangle::GetTriangle() const
{
return m_triangle;
}
inline void b3ClothTriangle::SetRadius(float32 radius)
{
m_radius = radius;
Synchronize(b3Vec3_zero);
}
inline float32 b3ClothTriangle::GetRadius() const
{
return m_radius;
}
inline void b3ClothTriangle::SetFriction(float32 friction)
{
m_friction = friction;
}
inline float32 b3ClothTriangle::GetFriction() const
{
return m_friction;
}
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -27,11 +27,8 @@ class b3StackAllocator;
class b3Particle;
class b3Body;
class b3BodyContact;
class b3ParticleContact;
class b3TriangleContact;
struct b3DenseVec3;
class b3ParticleBodyContact;
class b3ParticleTriangleContact;
struct b3ClothSolverBodyContactVelocityConstraint
{
@ -77,44 +74,11 @@ struct b3ClothSolverBodyContactPositionConstraint
b3Vec3 rA;
b3Vec3 rB;
b3Vec3 normalA;
b3Vec3 localPointA;
b3Vec3 localPointB;
};
struct b3ClothSolverParticleContactVelocityConstraint
{
u32 indexA;
float32 invMassA;
u32 indexB;
float32 invMassB;
float32 friction;
b3Vec3 point;
b3Vec3 normal;
float32 normalMass;
float32 normalImpulse;
float32 velocityBias;
b3Vec3 tangent1;
b3Vec3 tangent2;
b3Mat22 tangentMass;
b3Vec2 tangentImpulse;
};
struct b3ClothSolverParticleContactPositionConstraint
{
u32 indexA;
float32 invMassA;
float32 radiusA;
u32 indexB;
float32 invMassB;
float32 radiusB;
};
struct b3ClothSolverTriangleContactVelocityConstraint
{
u32 indexA;
@ -126,14 +90,21 @@ struct b3ClothSolverTriangleContactVelocityConstraint
float32 invMassC;
u32 indexD;
float32 invMassD;
float32 wB, wC, wD;
b3Vec3 JA;
b3Vec3 JB;
b3Vec3 JC;
b3Vec3 JD;
b3Vec3 normal;
float32 normalMass;
float32 normalImpulse;
float32 friction;
b3Vec3 tangent1;
b3Vec3 tangent2;
float32 tangentMass1;
float32 tangentMass2;
float32 tangentImpulse1;
float32 tangentImpulse2;
};
struct b3ClothSolverTriangleContactPositionConstraint
@ -150,24 +121,21 @@ struct b3ClothSolverTriangleContactPositionConstraint
float32 invMassD;
float32 triangleRadius;
bool front;
float32 wB, wC, wD;
};
struct b3ClothContactSolverDef
{
b3StackAllocator* allocator;
b3DenseVec3* positions;
b3DenseVec3* velocities;
b3Vec3* positions;
b3Vec3* velocities;
u32 bodyContactCount;
b3BodyContact** bodyContacts;
u32 particleContactCount;
b3ParticleContact** particleContacts;
b3ParticleBodyContact** bodyContacts;
u32 triangleContactCount;
b3TriangleContact** triangleContacts;
b3ParticleTriangleContact** triangleContacts;
};
inline float32 b3MixFriction(float32 u1, float32 u2)
@ -182,45 +150,31 @@ public:
~b3ClothContactSolver();
void InitializeBodyContactConstraints();
void InitializeParticleContactConstraints();
void InitializeTriangleContactConstraints();
void WarmStart();
void WarmStartBodyContactConstraints();
void WarmStartTriangleContactConstraints();
void SolveBodyContactVelocityConstraints();
void SolveParticleContactVelocityConstraints();
void SolveTriangleContactVelocityConstraints();
void StoreImpulses();
bool SolveBodyContactPositionConstraints();
bool SolveParticleContactPositionConstraints();
bool SolveTriangleContactPositionConstraints();
protected:
b3StackAllocator* m_allocator;
b3DenseVec3* m_positions;
b3DenseVec3* m_velocities;
b3Vec3* m_positions;
b3Vec3* m_velocities;
u32 m_bodyContactCount;
b3BodyContact** m_bodyContacts;
b3ParticleBodyContact** m_bodyContacts;
b3ClothSolverBodyContactVelocityConstraint* m_bodyVelocityConstraints;
b3ClothSolverBodyContactPositionConstraint* m_bodyPositionConstraints;
u32 m_particleContactCount;
b3ParticleContact** m_particleContacts;
b3ClothSolverParticleContactVelocityConstraint* m_particleVelocityConstraints;
b3ClothSolverParticleContactPositionConstraint* m_particlePositionConstraints;
u32 m_triangleContactCount;
b3TriangleContact** m_triangleContacts;
b3ParticleTriangleContact** m_triangleContacts;
b3ClothSolverTriangleContactVelocityConstraint* m_triangleVelocityConstraints;
b3ClothSolverTriangleContactPositionConstraint* m_trianglePositionConstraints;
};

View File

@ -0,0 +1,76 @@
/*
* 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_CLOTH_PARTICLE_BODY_CONTACT_H
#define B3_CLOTH_PARTICLE_BODY_CONTACT_H
#include <bounce/common/template/list.h>
#include <bounce/common/math/vec2.h>
#include <bounce/common/math/vec3.h>
#include <bounce/common/math/transform.h>
class b3Particle;
class b3Shape;
// A contact between a particle and a body
class b3ParticleBodyContact
{
public:
private:
friend class b3List2<b3ParticleBodyContact>;
friend class b3Cloth;
friend class b3Particle;
friend class b3ClothContactManager;
friend class b3ClothSolver;
friend class b3ClothContactSolver;
friend struct b3ParticleBodyContactWorldPoint;
b3ParticleBodyContact() { }
~b3ParticleBodyContact() { }
void Update();
b3Particle* m_p1;
b3Shape* m_s2;
bool m_active;
// Contact constraint
b3Vec3 m_normal1;
b3Vec3 m_localPoint1;
b3Vec3 m_localPoint2;
float32 m_normalImpulse;
// Friction constraint
b3Vec3 m_tangent1, m_tangent2;
b3Vec2 m_tangentImpulse;
b3ParticleBodyContact* m_prev;
b3ParticleBodyContact* m_next;
};
struct b3ParticleBodyContactWorldPoint
{
void Initialize(const b3ParticleBodyContact* c, float32 rA, const b3Transform& xfA, float32 rB, const b3Transform& xfB);
b3Vec3 point;
b3Vec3 normal;
float32 separation;
};
#endif

View File

@ -0,0 +1,65 @@
/*
* 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_CLOTH_PARTICLE_TRIANGLE_CONTACT_H
#define B3_CLOTH_PARTICLE_TRIANGLE_CONTACT_H
#include <bounce/common/template/list.h>
class b3Particle;
class b3ClothTriangle;
// Contact between particle and a triangle
class b3ParticleTriangleContact
{
public:
private:
friend class b3List2<b3ParticleTriangleContact>;
friend class b3Cloth;
friend class b3Particle;
friend class b3ClothTriangle;
friend class b3ClothContactManager;
friend class b3ClothContactSolver;
b3ParticleTriangleContact() { }
~b3ParticleTriangleContact() { }
void Update();
// Particle
b3Particle* m_p1;
// Triangle
b3ClothTriangle* m_t2;
b3Particle* m_p2;
b3Particle* m_p3;
b3Particle* m_p4;
float32 m_w2, m_w3, m_w4;
float32 m_normalImpulse;
float32 m_tangentImpulse1;
float32 m_tangentImpulse2;
bool m_active;
b3ParticleTriangleContact* m_prev;
b3ParticleTriangleContact* m_next;
};
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -22,16 +22,17 @@
#include <bounce/common/math/transform.h>
#include <bounce/common/template/list.h>
struct b3ClothSolverData;
struct b3ClothForceSolverData;
class b3Particle;
// Force types
enum b3ForceType
{
e_frictionForce,
e_strechForce,
e_shearForce,
e_springForce,
e_bendForce,
e_mouseForce,
};
struct b3ForceDef
@ -48,10 +49,13 @@ public:
//
b3Force* GetNext();
//
virtual bool HasParticle(const b3Particle* particle) const = 0;
protected:
friend class b3List2<b3Force>;
friend class b3Cloth;
friend class b3ClothSolver;
friend class b3ClothForceSolver;
friend class b3Particle;
static b3Force* Create(const b3ForceDef* def);
@ -60,7 +64,7 @@ protected:
b3Force() { }
virtual ~b3Force() { }
virtual void Apply(const b3ClothSolverData* data) = 0;
virtual void Apply(const b3ClothForceSolverData* data) = 0;
// Force type
b3ForceType m_type;

View File

@ -0,0 +1,140 @@
/*
* 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_MOUSE_FORCE_H
#define B3_MOUSE_FORCE_H
#include <bounce/cloth/forces/force.h>
class b3ClothTriangle;
struct b3MouseForceDef : public b3ForceDef
{
b3MouseForceDef()
{
type = e_mouseForce;
}
// Particle
b3Particle* particle;
// Triangle
b3ClothTriangle* triangle;
// Barycentric coordinates on triangle
float32 w2, w3, w4;
// Mouse stiffness
float32 mouse;
// Damping stiffness
float32 damping;
};
// Mouse force acting on a particle and triangle.
class b3MouseForce : public b3Force
{
public:
bool HasParticle(const b3Particle* particle) const;
b3Particle* GetParticle() const;
b3ClothTriangle* GetTriangle() const;
float32 GetMouseStiffness() const;
float32 GetDampingStiffness() const;
b3Vec3 GetActionForce1() const;
b3Vec3 GetActionForce2() const;
b3Vec3 GetActionForce3() const;
b3Vec3 GetActionForce4() const;
private:
friend class b3Force;
friend class b3Cloth;
b3MouseForce(const b3MouseForceDef* def);
~b3MouseForce();
void Apply(const b3ClothForceSolverData* data);
// Solver shared
// Particle
b3Particle* m_particle;
// Triangle
b3ClothTriangle* m_triangle;
// Barycentric coordinates
float32 m_w2, m_w3, m_w4;
// Mouse stiffness
float32 m_km;
// Damping stiffness
float32 m_kd;
// Action forces
b3Vec3 m_f1, m_f2, m_f3, m_f4;
};
inline b3Particle* b3MouseForce::GetParticle() const
{
return m_particle;
}
inline b3ClothTriangle* b3MouseForce::GetTriangle() const
{
return m_triangle;
}
inline float32 b3MouseForce::GetMouseStiffness() const
{
return m_km;
}
inline float32 b3MouseForce::GetDampingStiffness() const
{
return m_kd;
}
inline b3Vec3 b3MouseForce::GetActionForce1() const
{
return m_f1;
}
inline b3Vec3 b3MouseForce::GetActionForce2() const
{
return m_f2;
}
inline b3Vec3 b3MouseForce::GetActionForce3() const
{
return m_f3;
}
inline b3Vec3 b3MouseForce::GetActionForce4() const
{
return m_f4;
}
#endif

View File

@ -0,0 +1,114 @@
/*
* 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_SHEAR_FORCE_H
#define B3_SHEAR_FORCE_H
#include <bounce/cloth/forces/force.h>
class b3ClothTriangle;
struct b3ShearForceDef : public b3ForceDef
{
b3ShearForceDef()
{
type = e_shearForce;
}
// Triangle
b3ClothTriangle* triangle;
// Shearing stiffness
float32 shearing;
// Damping stiffness
float32 damping;
};
// Shear force acting on a cloth triangle.
class b3ShearForce : public b3Force
{
public:
bool HasParticle(const b3Particle* particle) const;
b3ClothTriangle* GetTriangle() const;
float32 GetShearingStiffness() const;
float32 GetDampingStiffness() const;
b3Vec3 GetActionForce1() const;
b3Vec3 GetActionForce2() const;
b3Vec3 GetActionForce3() const;
private:
friend class b3Force;
friend class b3Cloth;
b3ShearForce(const b3ShearForceDef* def);
~b3ShearForce();
void Apply(const b3ClothForceSolverData* data);
// Solver shared
// Triangle
b3ClothTriangle* m_triangle;
// Shearing stiffness
float32 m_ks;
// Damping stiffness
float32 m_kd;
// Action forces
b3Vec3 m_f1, m_f2, m_f3;
};
inline b3ClothTriangle* b3ShearForce::GetTriangle() const
{
return m_triangle;
}
inline float32 b3ShearForce::GetShearingStiffness() const
{
return m_ks;
}
inline float32 b3ShearForce::GetDampingStiffness() const
{
return m_kd;
}
inline b3Vec3 b3ShearForce::GetActionForce1() const
{
return m_f1;
}
inline b3Vec3 b3ShearForce::GetActionForce2() const
{
return m_f2;
}
inline b3Vec3 b3ShearForce::GetActionForce3() const
{
return m_f3;
}
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
* 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
@ -19,7 +19,7 @@
#ifndef B3_SPRING_FORCE_H
#define B3_SPRING_FORCE_H
#include <bounce/dynamics/cloth/force.h>
#include <bounce/cloth/forces/force.h>
struct b3SpringForceDef : public b3ForceDef
{
@ -67,6 +67,8 @@ public:
float32 GetDampingStiffness() const;
b3Vec3 GetActionForce() const;
bool HasParticle(const b3Particle* particle) const;
private:
friend class b3Force;
friend class b3Cloth;
@ -74,7 +76,7 @@ private:
b3SpringForce(const b3SpringForceDef* def);
~b3SpringForce();
void Apply(const b3ClothSolverData* data);
void Apply(const b3ClothForceSolverData* data);
// Solver shared
@ -127,4 +129,9 @@ inline b3Vec3 b3SpringForce::GetActionForce() const
return m_f;
}
inline bool b3SpringForce::HasParticle(const b3Particle* particle) const
{
return m_p1 == particle || m_p2 == particle;
}
#endif

View File

@ -0,0 +1,126 @@
/*
* 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_STRECH_FORCE_H
#define B3_STRECH_FORCE_H
#include <bounce/cloth/forces/force.h>
class b3ClothTriangle;
struct b3StrechForceDef : public b3ForceDef
{
b3StrechForceDef()
{
type = e_strechForce;
}
// Triangle
b3ClothTriangle* triangle;
// Streching stiffness
float32 streching;
// Damping stiffness
float32 damping;
// Desired strechiness in u direction
float32 bu;
// Desired strechiness in v direction
float32 bv;
};
// Strech force acting on a cloth triangle.
class b3StrechForce : public b3Force
{
public:
bool HasParticle(const b3Particle* particle) const;
b3ClothTriangle* GetTriangle() const;
float32 GetStrechingStiffness() const;
float32 GetDampingStiffness() const;
b3Vec3 GetActionForce1() const;
b3Vec3 GetActionForce2() const;
b3Vec3 GetActionForce3() const;
private:
friend class b3Force;
friend class b3Cloth;
b3StrechForce(const b3StrechForceDef* def);
~b3StrechForce();
void Apply(const b3ClothForceSolverData* data);
// Solver shared
// Triangle
b3ClothTriangle* m_triangle;
// Streching stiffness
float32 m_ks;
// Damping stiffness
float32 m_kd;
// Desired strechiness in u direction
float32 m_bu;
// Desired strechiness in v direction
float32 m_bv;
// Action forces
b3Vec3 m_f1, m_f2, m_f3;
};
inline b3ClothTriangle* b3StrechForce::GetTriangle() const
{
return m_triangle;
}
inline float32 b3StrechForce::GetStrechingStiffness() const
{
return m_ks;
}
inline float32 b3StrechForce::GetDampingStiffness() const
{
return m_kd;
}
inline b3Vec3 b3StrechForce::GetActionForce1() const
{
return m_f1;
}
inline b3Vec3 b3StrechForce::GetActionForce2() const
{
return m_f2;
}
inline b3Vec3 b3StrechForce::GetActionForce3() const
{
return m_f3;
}
#endif

Some files were not shown because too many files have changed in this diff Show More