now b3World is responsable for creating or destroying b3Cloth; clean up; update tests

This commit is contained in:
Irlan
2018-05-26 00:41:41 -03:00
parent 8d2affb0b2
commit 43013ad80b
13 changed files with 218 additions and 204 deletions

View File

@ -19,13 +19,11 @@
#ifndef B3_CLOTH_H
#define B3_CLOTH_H
#include <bounce/common/math/mat33.h>
#include <bounce/common/template/array.h>
#include <bounce/common/memory/stack_allocator.h>
// Maximum number of shapes per cloth.
#define B3_CLOTH_SHAPE_CAPACITY 32
#include <bounce/common/math/transform.h>
#include <bounce/common/template/list.h>
class b3StackAllocator;
class b3World;
class b3Shape;
struct b3ClothMesh;
@ -157,8 +155,8 @@ struct b3Spring
void ApplyForces(const b3ClothSolverData* data);
};
// Read-only contact
struct b3ParticleContact
// Read-only body contact between a particle and a solid
struct b3BodyContact
{
b3Particle* p1;
b3Shape* s2;
@ -173,22 +171,13 @@ struct b3ParticleContact
class b3Cloth
{
public:
b3Cloth();
~b3Cloth();
// Initialize this cloth from a definition.
void Initialize(const b3ClothDef& def);
// Get the world the cloth belongs to.
const b3World* GetWorld() const;
b3World* GetWorld();
// Return the cloth mesh used to initialize this cloth.
b3ClothMesh* GetMesh() const;
// Set the gravitational acceleration applied to this cloth.
// Units are m/s^2.
void SetGravity(const b3Vec3& gravity);
// Return the gravitational acceleration applied to this cloth.
const b3Vec3& GetGravity() const;
// Return the number of particles in this cloth.
u32 GetParticleCount() const;
@ -220,26 +209,28 @@ public:
// Return the kinetic (or dynamic) energy in this system.
float32 GetEnergy() const;
// Add a collision shape to the list of shapes in this cloth.
// The cloth will be able to respond to collisions with each shape in the list of shapes.
// Current the shape will be treated as a static shape.
void AddShape(b3Shape* shape);
// Get the next cloth in the world cloth list.
const b3Cloth* GetNext() const;
// Return the number of collision shapes in this cloth.
u32 GetShapeCount() const;
// Return the list of collision shapes added to this cloth.
b3Shape** GetShapeList();
// Perform a time step.
void Step(float32 dt);
// Get the next cloth in the world cloth list.
b3Cloth* GetNext();
// Set the positions of the mesh vertices to the positions of their associated particles.
void Apply() const;
// Debug draw the cloth using the associated cloth mesh.
void Draw() const;
protected:
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);
// Compute mass of each particle.
void ResetMass();
@ -248,11 +239,12 @@ protected:
void UpdateContacts();
// Solve
void Solve(float32 dt);
void Solve(float32 dt, const b3Vec3& gravity);
b3StackAllocator m_allocator;
b3StackAllocator* m_allocator;
b3Vec3 m_gravity;
b3ClothMesh* m_mesh;
float32 m_density;
u32 m_particleCount;
b3Particle* m_particles;
@ -260,31 +252,32 @@ protected:
u32 m_springCount;
b3Spring* m_springs;
b3ParticleContact* m_contacts;
b3BodyContact* m_contacts;
//u32 m_contactCount;
b3Shape* m_shapes[B3_CLOTH_SHAPE_CAPACITY];
u32 m_shapeCount;
b3ClothMesh* m_mesh;
float32 m_density;
// The parent world of this cloth.
b3World* m_world;
// Links to the world cloth list.
b3Cloth* m_prev;
b3Cloth* m_next;
};
inline const b3World* b3Cloth::GetWorld() const
{
return m_world;
}
inline b3World* b3Cloth::GetWorld()
{
return m_world;
}
inline b3ClothMesh* b3Cloth::GetMesh() const
{
return m_mesh;
}
inline const b3Vec3& b3Cloth::GetGravity() const
{
return m_gravity;
}
inline void b3Cloth::SetGravity(const b3Vec3& gravity)
{
m_gravity = gravity;
}
inline u32 b3Cloth::GetParticleCount() const
{
return m_particleCount;
@ -368,14 +361,14 @@ inline float32 b3Cloth::GetEnergy() const
return 0.5f * E;
}
inline u32 b3Cloth::GetShapeCount() const
inline const b3Cloth* b3Cloth::GetNext() const
{
return m_shapeCount;
return m_next;
}
inline b3Shape** b3Cloth::GetShapeList()
inline b3Cloth* b3Cloth::GetNext()
{
return m_shapes;
return m_next;
}
#endif

View File

@ -28,7 +28,7 @@ struct b3SparseMat33;
struct b3Particle;
struct b3Spring;
struct b3ParticleContact;
struct b3BodyContact;
class b3Shape;
class b3StackAllocator;
@ -65,7 +65,7 @@ public:
void Add(b3Particle* p);
void Add(b3Spring* s);
void Add(b3ParticleContact* c);
void Add(b3BodyContact* c);
void Solve(float32 dt, const b3Vec3& gravity);
private:
@ -93,7 +93,7 @@ private:
u32 m_contactCapacity;
u32 m_contactCount;
b3ParticleContact** m_contacts;
b3BodyContact** m_contacts;
u32 m_constraintCapacity;
u32 m_constraintCount;

View File

@ -26,7 +26,10 @@
#include <bounce/dynamics/joint_manager.h>
#include <bounce/dynamics/contact_manager.h>
struct b3ClothDef;
struct b3BodyDef;
class b3Cloth;
class b3Body;
class b3QueryListener;
class b3RayCastListener;
@ -67,6 +70,12 @@ public:
// The acceleration has units of m/s^2.
void SetGravity(const b3Vec3& gravity);
// Create a new deformable cloth.
b3Cloth* CreateCloth(const b3ClothDef& def);
// Destroy an existing deformable cloth.
void DestroyCloth(b3Cloth* cloth);
// Create a new rigid body.
b3Body* CreateBody(const b3BodyDef& def);
@ -129,6 +138,7 @@ private :
e_clearForcesFlag = 0x0002,
};
friend class b3Cloth;
friend class b3Body;
friend class b3Shape;
friend class b3Contact;
@ -138,14 +148,20 @@ private :
void Solve(float32 dt, u32 velocityIterations, u32 positionIterations);
void StepCloth(float32 dt);
bool m_sleeping;
bool m_warmStarting;
u32 m_flags;
b3Vec3 m_gravity;
b3StackAllocator m_stackAllocator;
b3BlockPool m_clothBlocks;
b3BlockPool m_bodyBlocks;
// List of clothes
b3List2<b3Cloth> m_clothList;
// List of bodies
b3List2<b3Body> m_bodyList;