refactoring

This commit is contained in:
Irlan
2018-10-08 16:18:28 -03:00
parent b48b16843a
commit f98374143a
47 changed files with 895 additions and 1046 deletions

View File

@ -0,0 +1,98 @@
/*
* 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.
*/
#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-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 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,184 @@
/*
* 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.
*/
#include <testbed/framework/cloth_dragger.h>
#include <bounce/common/draw.h>
b3ClothDragger::b3ClothDragger(b3Ray3* ray, b3Cloth* cloth)
{
m_spring = false;
m_ray = ray;
m_cloth = cloth;
m_triangle = nullptr;
}
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_triangle = m_mesh->triangles + rayOut.triangle;
m_x = rayOut.fraction;
b3Particle* p1 = m_cloth->GetVertexParticle(m_triangle->v1);
b3Particle* p2 = m_cloth->GetVertexParticle(m_triangle->v2);
b3Particle* p3 = m_cloth->GetVertexParticle(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_spring)
{
b3ParticleDef pd;
pd.type = e_staticParticle;
pd.position = B;
m_particle = m_cloth->CreateParticle(pd);
{
b3SpringForceDef sfd;
sfd.p1 = m_particle;
sfd.p2 = p1;
sfd.restLength = 0.0f;
sfd.structural = 10000.0f;
m_s1 = (b3SpringForce*)m_cloth->CreateForce(sfd);
}
{
b3SpringForceDef sfd;
sfd.p1 = m_particle;
sfd.p2 = p2;
sfd.restLength = 0.0f;
sfd.structural = 10000.0f;
m_s2 = (b3SpringForce*)m_cloth->CreateForce(sfd);
}
{
b3SpringForceDef sfd;
sfd.p1 = m_particle;
sfd.p2 = p3;
sfd.restLength = 0.0f;
sfd.structural = 10000.0f;
m_s3 = (b3SpringForce*)m_cloth->CreateForce(sfd);
}
}
else
{
m_t1 = p1->GetType();
p1->SetType(e_staticParticle);
m_t2 = p2->GetType();
p2->SetType(e_staticParticle);
m_t3 = p3->GetType();
p3->SetType(e_staticParticle);
}
return true;
}
void b3ClothDragger::Drag()
{
B3_ASSERT(IsDragging() == true);
b3Vec3 A = GetPointA();
b3Vec3 B = GetPointB();
b3Vec3 dx = B - A;
if (m_spring)
{
m_particle->SetPosition(B);
}
else
{
b3Particle* p1 = m_cloth->GetVertexParticle(m_triangle->v1);
p1->ApplyTranslation(dx);
b3Particle* p2 = m_cloth->GetVertexParticle(m_triangle->v2);
p2->ApplyTranslation(dx);
b3Particle* p3 = m_cloth->GetVertexParticle(m_triangle->v3);
p3->ApplyTranslation(dx);
}
}
void b3ClothDragger::StopDragging()
{
B3_ASSERT(IsDragging() == true);
if (m_spring)
{
m_cloth->DestroyForce(m_s1);
m_cloth->DestroyForce(m_s2);
m_cloth->DestroyForce(m_s3);
m_cloth->DestroyParticle(m_particle);
}
else
{
m_cloth->GetVertexParticle(m_triangle->v1)->SetType(m_t1);
m_cloth->GetVertexParticle(m_triangle->v2)->SetType(m_t2);
m_cloth->GetVertexParticle(m_triangle->v3)->SetType(m_t3);
}
m_triangle = nullptr;
}
b3Vec3 b3ClothDragger::GetPointA() const
{
B3_ASSERT(IsDragging() == true);
b3Vec3 A = m_cloth->GetVertexParticle(m_triangle->v1)->GetPosition();
b3Vec3 B = m_cloth->GetVertexParticle(m_triangle->v2)->GetPosition();
b3Vec3 C = m_cloth->GetVertexParticle(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,70 @@
/*
* 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 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/spring_force.h>
// A cloth triangle dragger.
class b3ClothDragger
{
public:
b3ClothDragger(b3Ray3* ray, b3Cloth* cloth);
~b3ClothDragger();
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;
b3ClothMeshTriangle* m_triangle;
float32 m_u, m_v;
bool m_spring;
b3Particle* m_particle;
b3SpringForce* m_s1;
b3SpringForce* m_s2;
b3SpringForce* m_s3;
b3ParticleType m_t1, m_t2, m_t3;
};
inline bool b3ClothDragger::IsDragging() const
{
return m_triangle != nullptr;
}
#endif

View File

@ -18,7 +18,6 @@
#include <testbed/framework/test.h>
#include <testbed/framework/profiler.h>
#include <imgui/imgui.h>
extern u32 b3_allocCalls, b3_maxAllocCalls;
extern u32 b3_convexCalls, b3_convexCacheHits;
@ -36,8 +35,7 @@ void b3EndProfileScope()
}
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;
@ -115,11 +113,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)
@ -130,11 +123,6 @@ void Test::MouseMove(const b3Ray3& pw)
{
m_bodyDragger.Drag();
}
if (m_clothDragger.IsDragging() == true)
{
m_clothDragger.Drag();
}
}
void Test::MouseLeftDown(const b3Ray3& pw)
@ -146,14 +134,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)
@ -164,11 +144,4 @@ void Test::MouseLeftUp(const b3Ray3& pw)
EndDragging();
}
if (m_clothDragger.IsDragging() == true)
{
m_clothDragger.StopDragging();
EndDragging();
}
}

View File

@ -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

@ -60,7 +60,6 @@
#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>