Added initial broadphase support for softbody versus softbody and softbody versus rigidbody (see btSoftSoftCollisionAlgorithm and btSoftRididCollisionAlgorithm)

Added 	SOFTBODY_SHAPE_PROXYTYPE.
Some refactoring for btSoftBody, needs more work.
This commit is contained in:
erwin.coumans
2008-04-06 06:53:00 +00:00
parent 39d929cbb0
commit b37a37a28e
14 changed files with 2512 additions and 1859 deletions

View File

@@ -26,7 +26,8 @@ subject to the following restrictions:
#include "../GimpactTestDemo/TorusMesh.h" #include "../GimpactTestDemo/TorusMesh.h"
#include <stdio.h> //printf debugging #include <stdio.h> //printf debugging
#include "../../Extras/ConvexHull/btConvexHull.h" #include "../../Extras/ConvexHull/btConvexHull.h"
#include "btSoftBodyRigidBodyCollisionConfiguration.h"
#include "BulletDynamics/SoftBody/btSoftBodyHelpers.h"
static float gCollisionMargin = 0.05f/*0.05f*/; static float gCollisionMargin = 0.05f/*0.05f*/;
#include "SoftDemo.h" #include "SoftDemo.h"
@@ -42,19 +43,17 @@ const int maxProxies = 32766;
const int maxOverlap = 65535; const int maxOverlap = 65535;
// //
btSoftBody* CreateFromConvexHull( btSoftBody::ISoftBody* isoftbody, btSoftBody* btSoftBodyHelpers::CreateFromConvexHull(btSoftBody::btSoftBodyWorldInfo& worldInfo, const btVector3* vertices,
const btVector3* vertices,
int nvertices) int nvertices)
{ {
HullDesc hdsc(QF_TRIANGLES,nvertices,vertices); HullDesc hdsc(QF_TRIANGLES,nvertices,vertices);
HullResult hres; HullResult hres;
HullLibrary hlib;/*??*/ HullLibrary hlib;/*??*/
hdsc.mMaxVertices=nvertices; hdsc.mMaxVertices=nvertices;
hlib.CreateConvexHull(hdsc,hres); hlib.CreateConvexHull(hdsc,hres);
btSoftBody* psb=btSoftBody::Create( isoftbody, btSoftBody* psb=new btSoftBody(worldInfo,(int)hres.mNumOutputVertices,
(int)hres.mNumOutputVertices,
hres.mOutputVertices,0); hres.mOutputVertices,0);
for(int i=0;i<(int)hres.mNumFaces;++i) for(int i=0;i<(int)hres.mNumFaces;++i)
{ {
const int idx[]={ hres.mIndices[i*3+0], const int idx[]={ hres.mIndices[i*3+0],
hres.mIndices[i*3+1], hres.mIndices[i*3+1],
@@ -67,9 +66,9 @@ for(int i=0;i<(int)hres.mNumFaces;++i)
1,btSoftBody::eLType::Structural); 1,btSoftBody::eLType::Structural);
psb->AppendFace(idx[0],idx[1],idx[2]); psb->AppendFace(idx[0],idx[1],idx[2]);
} }
hlib.ReleaseResult(hres); hlib.ReleaseResult(hres);
psb->RandomizeConstraints(); psb->RandomizeConstraints();
return(psb); return(psb);
} }
@@ -117,32 +116,17 @@ void SoftDemo::createStack( btCollisionShape* boxShape, float halfCubeSize, int
#ifdef DHSJHDSAKJDHSJKADHSAKJDHSA
//
// ISoftBody implementation
//
//
void SoftDemo::SoftBodyImpl::Attach(btSoftBody*)
{}
//
void SoftDemo::SoftBodyImpl::Detach(btSoftBody*)
{}
//
void SoftDemo::SoftBodyImpl::StartCollide(const btVector3&,const btVector3&)
{}
// //
bool SoftDemo::SoftBodyImpl::CheckContactPrecise(const btVector3& x, bool SoftDemo::SoftBodyImpl::CheckContactPrecise(const btVector3& x,
btSoftBody::ISoftBody::sCti& cti) btSoftBody::ISoftBody::sCti& cti)
{ {
btScalar maxdepth=0; btScalar maxdepth=0;
btGjkEpaSolver2::sResults res; btGjkEpaSolver2::sResults res;
btDynamicsWorld* pdw=pdemo->m_dynamicsWorld; // btDynamicsWorld* pdw=pdemo->m_dynamicsWorld;
btCollisionObjectArray& coa=pdw->getCollisionObjectArray();
for(int i=0,ni=coa.size();i<ni;++i) btCollisionObjectArray& coa=pdw->getCollisionObjectArray();
for(int i=0,ni=coa.size();i<ni;++i)
{ {
btRigidBody* prb=(btRigidBody*)(coa[i]); btRigidBody* prb=(btRigidBody*)(coa[i]);
btCollisionShape* shp=prb->getCollisionShape(); btCollisionShape* shp=prb->getCollisionShape();
@@ -160,18 +144,18 @@ for(int i=0,ni=coa.size();i<ni;++i)
} }
} }
} }
return(maxdepth<0); return(maxdepth<0);
} }
// //
bool SoftDemo::SoftBodyImpl::CheckContact( const btVector3& x, bool SoftDemo::SoftBodyImpl::CheckContact( const btVector3& x,
btSoftBody::ISoftBody::sCti& cti) btSoftBody::sCti& cti)
{ {
btScalar maxdepth=0; btScalar maxdepth=0;
btGjkEpaSolver2::sResults res; btGjkEpaSolver2::sResults res;
btDynamicsWorld* pdw=pdemo->m_dynamicsWorld; btDynamicsWorld* pdw=pdemo->m_dynamicsWorld;
btCollisionObjectArray& coa=pdw->getCollisionObjectArray(); btCollisionObjectArray& coa=pdw->getCollisionObjectArray();
for(int i=0,ni=coa.size();i<ni;++i) for(int i=0,ni=coa.size();i<ni;++i)
{ {
btVector3 nrm; btVector3 nrm;
btRigidBody* prb=(btRigidBody*)(coa[i]); btRigidBody* prb=(btRigidBody*)(coa[i]);
@@ -189,7 +173,7 @@ for(int i=0,ni=coa.size();i<ni;++i)
cti.m_offset = -dot(cti.m_normal,wit); cti.m_offset = -dot(cti.m_normal,wit);
} }
} }
return(maxdepth<0); return(maxdepth<0);
} }
// //
@@ -200,10 +184,10 @@ void SoftDemo::SoftBodyImpl::EndCollide()
void SoftDemo::SoftBodyImpl::EvaluateMedium( const btVector3& x, void SoftDemo::SoftBodyImpl::EvaluateMedium( const btVector3& x,
btSoftBody::ISoftBody::sMedium& medium) btSoftBody::ISoftBody::sMedium& medium)
{ {
medium.m_velocity = btVector3(0,0,0); medium.m_velocity = btVector3(0,0,0);
medium.m_pressure = 0; medium.m_pressure = 0;
medium.m_density = air_density; medium.m_density = air_density;
if(water_density>0) if(water_density>0)
{ {
const btScalar depth=-(dot(x,water_normal)+water_offset); const btScalar depth=-(dot(x,water_normal)+water_offset);
if(depth>0) if(depth>0)
@@ -216,6 +200,7 @@ if(water_density>0)
} }
} }
#endif
extern int gNumManifold; extern int gNumManifold;
@@ -231,7 +216,7 @@ void SoftDemo::clientMoveAndDisplay()
float dt = getDeltaTimeMicroseconds() * 0.000001f; float dt = getDeltaTimeMicroseconds() * 0.000001f;
// printf("dt = %f: ",dt); // printf("dt = %f: ",dt);
if (m_dynamicsWorld) if (m_dynamicsWorld)
@@ -267,14 +252,22 @@ void SoftDemo::clientMoveAndDisplay()
#endif #endif
/* soft bodies */ /* soft bodies collision detection */
for(int ib=0;ib<m_softbodies.size();++ib)
{
btSoftBody* psb=m_softbodies[ib];
// psb->updateAabb(dt);
}
/* soft bodies simulation */
for(int ib=0;ib<m_softbodies.size();++ib) for(int ib=0;ib<m_softbodies.size();++ib)
{ {
btSoftBody* psb=m_softbodies[ib]; btSoftBody* psb=m_softbodies[ib];
psb->AddVelocity(m_dynamicsWorld->getGravity()*dt); psb->AddVelocity(m_dynamicsWorld->getGravity()*dt);
psb->Step(dt); psb->Step(dt);
} }
m_sparsesdf.GarbageCollect();
m_softBodyWorldInfo.m_sparsesdf.GarbageCollect();
//optional but useful: debug drawing //optional but useful: debug drawing
@@ -332,18 +325,18 @@ void SoftDemo::displayCallback(void) {
static inline btScalar UnitRand() static inline btScalar UnitRand()
{ {
return(rand()/(btScalar)RAND_MAX); return(rand()/(btScalar)RAND_MAX);
} }
static inline btScalar SignedUnitRand() static inline btScalar SignedUnitRand()
{ {
return(UnitRand()*2-1); return(UnitRand()*2-1);
} }
static inline btVector3 Vector3Rand() static inline btVector3 Vector3Rand()
{ {
const btVector3 p=btVector3(SignedUnitRand(),SignedUnitRand(),SignedUnitRand()); const btVector3 p=btVector3(SignedUnitRand(),SignedUnitRand(),SignedUnitRand());
return(p.normalized()); return(p.normalized());
} }
// //
@@ -351,12 +344,12 @@ return(p.normalized());
// //
static void Ctor_RbUpStack(SoftDemo* pdemo,int count) static void Ctor_RbUpStack(SoftDemo* pdemo,int count)
{ {
float mass=10; float mass=10;
btCollisionShape* shape[]={ new btSphereShape(1.5), btCollisionShape* shape[]={ new btSphereShape(1.5),
new btBoxShape(btVector3(1,1,1)), new btBoxShape(btVector3(1,1,1)),
new btCylinderShapeX(btVector3(4,1,1))}; new btCylinderShapeX(btVector3(4,1,1))};
static const int nshapes=sizeof(shape)/sizeof(shape[0]); static const int nshapes=sizeof(shape)/sizeof(shape[0]);
for(int i=0;i<count;++i) for(int i=0;i<count;++i)
{ {
btTransform startTransform; btTransform startTransform;
startTransform.setIdentity(); startTransform.setIdentity();
@@ -370,10 +363,10 @@ for(int i=0;i<count;++i)
// //
static void Ctor_BigBall(SoftDemo* pdemo,btScalar mass=10) static void Ctor_BigBall(SoftDemo* pdemo,btScalar mass=10)
{ {
btTransform startTransform; btTransform startTransform;
startTransform.setIdentity(); startTransform.setIdentity();
startTransform.setOrigin(btVector3(0,13,0)); startTransform.setOrigin(btVector3(0,13,0));
btRigidBody* body=pdemo->localCreateRigidBody(mass,startTransform,new btSphereShape(3)); btRigidBody* body=pdemo->localCreateRigidBody(mass,startTransform,new btSphereShape(3));
} }
// //
@@ -381,11 +374,11 @@ btRigidBody* body=pdemo->localCreateRigidBody(mass,startTransform,new btSphereS
// //
static void Ctor_BigPlate(SoftDemo* pdemo,btScalar mass=15) static void Ctor_BigPlate(SoftDemo* pdemo,btScalar mass=15)
{ {
btTransform startTransform; btTransform startTransform;
startTransform.setIdentity(); startTransform.setIdentity();
startTransform.setOrigin(btVector3(0,4,0.5)); startTransform.setOrigin(btVector3(0,4,0.5));
btRigidBody* body=pdemo->localCreateRigidBody(mass,startTransform,new btBoxShape(btVector3(5,1,5))); btRigidBody* body=pdemo->localCreateRigidBody(mass,startTransform,new btBoxShape(btVector3(5,1,5)));
body->setFriction(1); body->setFriction(1);
} }
// //
@@ -393,8 +386,8 @@ body->setFriction(1);
// //
static void Ctor_LinearStair(SoftDemo* pdemo,const btVector3& org,const btVector3& sizes,btScalar angle,int count) static void Ctor_LinearStair(SoftDemo* pdemo,const btVector3& org,const btVector3& sizes,btScalar angle,int count)
{ {
btBoxShape* shape=new btBoxShape(sizes); btBoxShape* shape=new btBoxShape(sizes);
for(int i=0;i<count;++i) for(int i=0;i<count;++i)
{ {
btTransform startTransform; btTransform startTransform;
startTransform.setIdentity(); startTransform.setIdentity();
@@ -409,8 +402,8 @@ for(int i=0;i<count;++i)
// //
static btSoftBody* Ctor_SoftBox(SoftDemo* pdemo,const btVector3& p,const btVector3& s) static btSoftBody* Ctor_SoftBox(SoftDemo* pdemo,const btVector3& p,const btVector3& s)
{ {
const btVector3 h=s*0.5; const btVector3 h=s*0.5;
const btVector3 c[]={ p+h*btVector3(-1,-1,-1), const btVector3 c[]={ p+h*btVector3(-1,-1,-1),
p+h*btVector3(+1,-1,-1), p+h*btVector3(+1,-1,-1),
p+h*btVector3(-1,+1,-1), p+h*btVector3(-1,+1,-1),
p+h*btVector3(+1,+1,-1), p+h*btVector3(+1,+1,-1),
@@ -418,10 +411,11 @@ const btVector3 c[]={ p+h*btVector3(-1,-1,-1),
p+h*btVector3(+1,-1,+1), p+h*btVector3(+1,-1,+1),
p+h*btVector3(-1,+1,+1), p+h*btVector3(-1,+1,+1),
p+h*btVector3(+1,+1,+1)}; p+h*btVector3(+1,+1,+1)};
btSoftBody* psb=CreateFromConvexHull(&pdemo->m_softbodyimpl,c,8); btSoftBody* psb=btSoftBodyHelpers::CreateFromConvexHull(pdemo->m_softBodyWorldInfo,c,8);
psb->GenerateBendingConstraints(2,1); psb->GenerateBendingConstraints(2,1);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
return(psb);
return(psb);
} }
// //
@@ -429,16 +423,17 @@ return(psb);
// //
static btSoftBody* Ctor_SoftBoulder(SoftDemo* pdemo,const btVector3& p,const btVector3& s,int np,int id) static btSoftBody* Ctor_SoftBoulder(SoftDemo* pdemo,const btVector3& p,const btVector3& s,int np,int id)
{ {
btAlignedObjectArray<btVector3> pts; btAlignedObjectArray<btVector3> pts;
if(id) srand(id); if(id) srand(id);
for(int i=0;i<np;++i) for(int i=0;i<np;++i)
{ {
pts.push_back(Vector3Rand()*s+p); pts.push_back(Vector3Rand()*s+p);
} }
btSoftBody* psb=CreateFromConvexHull(&pdemo->m_softbodyimpl,&pts[0],pts.size()); btSoftBody* psb=btSoftBodyHelpers::CreateFromConvexHull(pdemo->m_softBodyWorldInfo,&pts[0],pts.size());
psb->GenerateBendingConstraints(2,1); psb->GenerateBendingConstraints(2,1);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
return(psb);
return(psb);
} }
//#define TRACEDEMO { pdemo->demoname=__FUNCTION__+5;printf("Launching demo: " __FUNCTION__ "\r\n"); } //#define TRACEDEMO { pdemo->demoname=__FUNCTION__+5;printf("Launching demo: " __FUNCTION__ "\r\n"); }
@@ -448,12 +443,11 @@ return(psb);
// //
static void Init_Ropes(SoftDemo* pdemo) static void Init_Ropes(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
const int n=15; const int n=15;
for(int i=0;i<n;++i) for(int i=0;i<n;++i)
{ {
btSoftBody* psb=CreateRope( &pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreateRope(pdemo->m_softBodyWorldInfo, btVector3(-10,0,i*0.25),
btVector3(-10,0,i*0.25),
btVector3(10,0,i*0.25), btVector3(10,0,i*0.25),
16, 16,
1+2); 1+2);
@@ -461,6 +455,7 @@ for(int i=0;i<n;++i)
psb->m_cfg.kLST = 0.1+(i/(btScalar)(n-1))*0.9; psb->m_cfg.kLST = 0.1+(i/(btScalar)(n-1))*0.9;
psb->SetTotalMass(20); psb->SetTotalMass(20);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
} }
} }
@@ -469,27 +464,26 @@ for(int i=0;i<n;++i)
// //
static void Init_RopeAttach(SoftDemo* pdemo) static void Init_RopeAttach(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
struct Functors struct Functors
{ {
static btSoftBody* CtorRope(SoftDemo* pdemo,const btVector3& p) static btSoftBody* CtorRope(SoftDemo* pdemo,const btVector3& p)
{ {
btSoftBody* psb=CreateRope(&pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreateRope(pdemo->m_softBodyWorldInfo,p,p+btVector3(10,0,0),8,1);
p,p+btVector3(10,0,0),8,1);
psb->m_cfg.kDF = 0; psb->m_cfg.kDF = 0;
psb->SetTotalMass(50); psb->SetTotalMass(50);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
return(psb); return(psb);
} }
}; };
btTransform startTransform; btTransform startTransform;
startTransform.setIdentity(); startTransform.setIdentity();
startTransform.setOrigin(btVector3(12,8,0)); startTransform.setOrigin(btVector3(12,8,0));
btRigidBody* body=pdemo->localCreateRigidBody(50,startTransform,new btBoxShape(btVector3(2,6,2))); btRigidBody* body=pdemo->localCreateRigidBody(50,startTransform,new btBoxShape(btVector3(2,6,2)));
btSoftBody* psb0=Functors::CtorRope(pdemo,btVector3(0,8,-1)); btSoftBody* psb0=Functors::CtorRope(pdemo,btVector3(0,8,-1));
btSoftBody* psb1=Functors::CtorRope(pdemo,btVector3(0,8,+1)); btSoftBody* psb1=Functors::CtorRope(pdemo,btVector3(0,8,+1));
psb0->AppendAnchor(psb0->m_nodes.size()-1,body); psb0->AppendAnchor(psb0->getNodes().size()-1,body);
psb1->AppendAnchor(psb1->m_nodes.size()-1,body); psb1->AppendAnchor(psb1->getNodes().size()-1,body);
} }
// //
@@ -497,22 +491,22 @@ psb1->AppendAnchor(psb1->m_nodes.size()-1,body);
// //
static void Init_ClothAttach(SoftDemo* pdemo) static void Init_ClothAttach(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
const btScalar s=4; const btScalar s=4;
const btScalar h=6; const btScalar h=6;
const int r=9; const int r=9;
btSoftBody* psb=CreatePatch(&pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo,btVector3(-s,h,-s),
btVector3(-s,h,-s),
btVector3(+s,h,-s), btVector3(+s,h,-s),
btVector3(-s,h,+s), btVector3(-s,h,+s),
btVector3(+s,h,+s),r,r,4+8,true); btVector3(+s,h,+s),r,r,4+8,true);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
btTransform startTransform;
startTransform.setIdentity(); btTransform startTransform;
startTransform.setOrigin(btVector3(0,h,-(s+3.5))); startTransform.setIdentity();
btRigidBody* body=pdemo->localCreateRigidBody(20,startTransform,new btBoxShape(btVector3(s,1,3))); startTransform.setOrigin(btVector3(0,h,-(s+3.5)));
psb->AppendAnchor(0,body); btRigidBody* body=pdemo->localCreateRigidBody(20,startTransform,new btBoxShape(btVector3(s,1,3)));
psb->AppendAnchor(r-1,body); psb->AppendAnchor(0,body);
psb->AppendAnchor(r-1,body);
} }
// //
@@ -520,17 +514,17 @@ psb->AppendAnchor(r-1,body);
// //
static void Init_Impact(SoftDemo* pdemo) static void Init_Impact(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
btSoftBody* psb=CreateRope( &pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreateRope(pdemo->m_softBodyWorldInfo, btVector3(0,0,0),
btVector3(0,0,0),
btVector3(0,-1,0), btVector3(0,-1,0),
0, 0,
1); 1);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
btTransform startTransform;
startTransform.setIdentity(); btTransform startTransform;
startTransform.setOrigin(btVector3(0,20,0)); startTransform.setIdentity();
btRigidBody* body=pdemo->localCreateRigidBody(10,startTransform,new btBoxShape(btVector3(2,2,2))); startTransform.setOrigin(btVector3(0,20,0));
btRigidBody* body=pdemo->localCreateRigidBody(10,startTransform,new btBoxShape(btVector3(2,2,2)));
} }
// //
@@ -538,15 +532,14 @@ btRigidBody* body=pdemo->localCreateRigidBody(10,startTransform,new btBoxShape(
// //
static void Init_Aero(SoftDemo* pdemo) static void Init_Aero(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
const btScalar s=2; const btScalar s=2;
const btScalar h=10; const btScalar h=10;
const int segments=6; const int segments=6;
const int count=50; const int count=50;
for(int i=0;i<count;++i) for(int i=0;i<count;++i)
{ {
btSoftBody* psb=CreatePatch(&pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo,btVector3(-s,h,-s),
btVector3(-s,h,-s),
btVector3(+s,h,-s), btVector3(+s,h,-s),
btVector3(-s,h,+s), btVector3(-s,h,+s),
btVector3(+s,h,+s), btVector3(+s,h,+s),
@@ -568,8 +561,9 @@ for(int i=0;i<count;++i)
psb->SetTotalMass(0.1); psb->SetTotalMass(0.1);
psb->AddForce(btVector3(0,2,0),0); psb->AddForce(btVector3(0,2,0),0);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
} }
pdemo->m_autocam=true; pdemo->m_autocam=true;
} }
// //
@@ -577,10 +571,10 @@ pdemo->m_autocam=true;
// //
static void Init_Friction(SoftDemo* pdemo) static void Init_Friction(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
const btScalar bs=2; const btScalar bs=2;
const btScalar ts=bs+bs/4; const btScalar ts=bs+bs/4;
for(int i=0,ni=20;i<ni;++i) for(int i=0,ni=20;i<ni;++i)
{ {
const btVector3 p(-ni*ts/2+i*ts,-10+bs,40); const btVector3 p(-ni*ts/2+i*ts,-10+bs,40);
btSoftBody* psb=Ctor_SoftBox(pdemo,p,btVector3(bs,bs,bs)); btSoftBody* psb=Ctor_SoftBox(pdemo,p,btVector3(bs,bs,bs));
@@ -594,19 +588,19 @@ for(int i=0,ni=20;i<ni;++i)
// //
static void Init_Pressure(SoftDemo* pdemo) static void Init_Pressure(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
btSoftBody* psb=CreateEllipsoid(&pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreateEllipsoid(pdemo->m_softBodyWorldInfo,btVector3(35,25,0),
btVector3(35,25,0),
btVector3(1,1,1)*3, btVector3(1,1,1)*3,
512); 512);
psb->m_cfg.kLST = 0.1; psb->m_cfg.kLST = 0.1;
psb->m_cfg.kDF = 1; psb->m_cfg.kDF = 1;
psb->m_cfg.kPR = 2500; psb->m_cfg.kPR = 2500;
psb->SetTotalMass(30,true); psb->SetTotalMass(30,true);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
Ctor_BigPlate(pdemo);
Ctor_LinearStair(pdemo,btVector3(0,0,0),btVector3(2,1,5),0,10); Ctor_BigPlate(pdemo);
pdemo->m_autocam=true; Ctor_LinearStair(pdemo,btVector3(0,0,0),btVector3(2,1,5),0,10);
pdemo->m_autocam=true;
} }
// //
@@ -614,19 +608,19 @@ pdemo->m_autocam=true;
// //
static void Init_Volume(SoftDemo* pdemo) static void Init_Volume(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
btSoftBody* psb=CreateEllipsoid(&pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreateEllipsoid(pdemo->m_softBodyWorldInfo,btVector3(35,25,0),
btVector3(35,25,0),
btVector3(1,1,1)*3, btVector3(1,1,1)*3,
512); 512);
psb->m_cfg.kLST = 0.45; psb->m_cfg.kLST = 0.45;
psb->m_cfg.kVC = 20; psb->m_cfg.kVC = 20;
psb->SetTotalMass(50,true); psb->SetTotalMass(50,true);
psb->SetPose(true,false); psb->SetPose(true,false);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
Ctor_BigPlate(pdemo);
Ctor_LinearStair(pdemo,btVector3(0,0,0),btVector3(2,1,5),0,10); Ctor_BigPlate(pdemo);
pdemo->m_autocam=true; Ctor_LinearStair(pdemo,btVector3(0,0,0),btVector3(2,1,5),0,10);
pdemo->m_autocam=true;
} }
// //
@@ -634,21 +628,20 @@ pdemo->m_autocam=true;
// //
static void Init_Sticks(SoftDemo* pdemo) static void Init_Sticks(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
const int n=16; const int n=16;
const int sg=4; const int sg=4;
const btScalar sz=5; const btScalar sz=5;
const btScalar hg=4; const btScalar hg=4;
const btScalar in=1/(btScalar)(n-1); const btScalar in=1/(btScalar)(n-1);
for(int y=0;y<n;++y) for(int y=0;y<n;++y)
{ {
for(int x=0;x<n;++x) for(int x=0;x<n;++x)
{ {
const btVector3 org(-sz+sz*2*x*in, const btVector3 org(-sz+sz*2*x*in,
-10, -10,
-sz+sz*2*y*in); -sz+sz*2*y*in);
btSoftBody* psb=CreateRope( &pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreateRope( pdemo->m_softBodyWorldInfo, org,
org,
org+btVector3(hg*0.001,hg,0), org+btVector3(hg*0.001,hg,0),
sg, sg,
1); 1);
@@ -662,9 +655,10 @@ for(int y=0;y<n;++y)
psb->SetMass(1,0); psb->SetMass(1,0);
psb->SetTotalMass(0.01); psb->SetTotalMass(0.01);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
} }
} }
Ctor_BigBall(pdemo); Ctor_BigBall(pdemo);
} }
// //
@@ -672,20 +666,22 @@ Ctor_BigBall(pdemo);
// //
static void Init_Cloth(SoftDemo* pdemo) static void Init_Cloth(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
const btScalar s=8; const btScalar s=8;
btSoftBody* psb=CreatePatch(&pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreatePatch( pdemo->m_softBodyWorldInfo,btVector3(-s,0,-s),
btVector3(-s,0,-s),
btVector3(+s,0,-s), btVector3(+s,0,-s),
btVector3(-s,0,+s), btVector3(-s,0,+s),
btVector3(+s,0,+s), btVector3(+s,0,+s),
31,31, 31,31,
// 31,31,
1+2+4+8,true); 1+2+4+8,true);
psb->GenerateBendingConstraints(2,1); psb->GenerateBendingConstraints(2,1);
psb->m_cfg.kLST = 0.4; psb->m_cfg.kLST = 0.4;
psb->SetTotalMass(150); psb->SetTotalMass(150);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
Ctor_RbUpStack(pdemo,10);
Ctor_RbUpStack(pdemo,10);
} }
// //
@@ -693,18 +689,18 @@ Ctor_RbUpStack(pdemo,10);
// //
static void Init_Bunny(SoftDemo* pdemo) static void Init_Bunny(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
btSoftBody* psb=CreateFromTriMesh( &pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo,gVerticesBunny,
gVerticesBunny,
&gIndicesBunny[0][0], &gIndicesBunny[0][0],
BUNNY_NUM_TRIANGLES); BUNNY_NUM_TRIANGLES);
psb->GenerateBendingConstraints(2,0.5); psb->GenerateBendingConstraints(2,0.5);
psb->m_cfg.iterations = 2; psb->m_cfg.iterations = 2;
psb->m_cfg.kDF = 0.5; psb->m_cfg.kDF = 0.5;
psb->RandomizeConstraints(); psb->RandomizeConstraints();
psb->Scale(btVector3(6,6,6)); psb->Scale(btVector3(6,6,6));
psb->SetTotalMass(100,true); psb->SetTotalMass(100,true);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
} }
// //
@@ -712,20 +708,20 @@ pdemo->m_softbodies.push_back(psb);
// //
static void Init_BunnyMatch(SoftDemo* pdemo) static void Init_BunnyMatch(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
btSoftBody* psb=CreateFromTriMesh( &pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVerticesBunny,
gVerticesBunny,
&gIndicesBunny[0][0], &gIndicesBunny[0][0],
BUNNY_NUM_TRIANGLES); BUNNY_NUM_TRIANGLES);
psb->GenerateBendingConstraints(2,0.5); psb->GenerateBendingConstraints(2,0.5);
psb->m_cfg.kDF = 0.5; psb->m_cfg.kDF = 0.5;
psb->m_cfg.kLST = 0.1; psb->m_cfg.kLST = 0.1;
psb->m_cfg.kMT = 0.01; psb->m_cfg.kMT = 0.01;
psb->RandomizeConstraints(); psb->RandomizeConstraints();
psb->Scale(btVector3(6,6,6)); psb->Scale(btVector3(6,6,6));
psb->SetTotalMass(100,true); psb->SetTotalMass(100,true);
psb->SetPose(true,true); psb->SetPose(true,true);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
} }
// //
@@ -733,20 +729,20 @@ pdemo->m_softbodies.push_back(psb);
// //
static void Init_Torus(SoftDemo* pdemo) static void Init_Torus(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
btSoftBody* psb=CreateFromTriMesh( &pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreateFromTriMesh( pdemo->m_softBodyWorldInfo, gVertices,
gVertices,
&gIndices[0][0], &gIndices[0][0],
NUM_TRIANGLES); NUM_TRIANGLES);
psb->GenerateBendingConstraints(2,1); psb->GenerateBendingConstraints(2,1);
psb->m_cfg.iterations=2; psb->m_cfg.iterations=2;
psb->RandomizeConstraints(); psb->RandomizeConstraints();
btMatrix3x3 m; btMatrix3x3 m;
m.setEulerZYX(SIMD_PI/2,0,0); m.setEulerZYX(SIMD_PI/2,0,0);
psb->Transform(btTransform(m,btVector3(0,4,0))); psb->Transform(btTransform(m,btVector3(0,4,0)));
psb->Scale(btVector3(2,2,2)); psb->Scale(btVector3(2,2,2));
psb->SetTotalMass(50,true); psb->SetTotalMass(50,true);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
} }
// //
@@ -754,31 +750,32 @@ pdemo->m_softbodies.push_back(psb);
// //
static void Init_TorusMatch(SoftDemo* pdemo) static void Init_TorusMatch(SoftDemo* pdemo)
{ {
//TRACEDEMO //TRACEDEMO
btSoftBody* psb=CreateFromTriMesh( &pdemo->m_softbodyimpl, btSoftBody* psb=btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVertices,
gVertices,
&gIndices[0][0], &gIndices[0][0],
NUM_TRIANGLES); NUM_TRIANGLES);
psb->GenerateBendingConstraints(2,1); psb->GenerateBendingConstraints(2,1);
psb->m_cfg.kLST=0.1; psb->m_cfg.kLST=0.1;
psb->m_cfg.kMT=0.05; psb->m_cfg.kMT=0.05;
psb->RandomizeConstraints(); psb->RandomizeConstraints();
btMatrix3x3 m; btMatrix3x3 m;
m.setEulerZYX(SIMD_PI/2,0,0); m.setEulerZYX(SIMD_PI/2,0,0);
psb->Transform(btTransform(m,btVector3(0,4,0))); psb->Transform(btTransform(m,btVector3(0,4,0)));
psb->Scale(btVector3(2,2,2)); psb->Scale(btVector3(2,2,2));
psb->SetTotalMass(50,true); psb->SetTotalMass(50,true);
psb->SetPose(true,true); psb->SetPose(true,true);
pdemo->m_softbodies.push_back(psb); pdemo->m_softbodies.push_back(psb);
} }
static unsigned current_demo=0; static unsigned current_demo=0;
void SoftDemo::clientResetScene() void SoftDemo::clientResetScene()
{ {
DemoApplication::clientResetScene(); DemoApplication::clientResetScene();
/* Clean up */ /* Clean up */
for(int i=m_dynamicsWorld->getNumCollisionObjects()-1;i>0;i--) for(int i=m_dynamicsWorld->getNumCollisionObjects()-1;i>0;i--)
{ {
btCollisionObject* obj=m_dynamicsWorld->getCollisionObjectArray()[i]; btCollisionObject* obj=m_dynamicsWorld->getCollisionObjectArray()[i];
btRigidBody* body=btRigidBody::upcast(obj); btRigidBody* body=btRigidBody::upcast(obj);
@@ -789,14 +786,15 @@ for(int i=m_dynamicsWorld->getNumCollisionObjects()-1;i>0;i--)
m_dynamicsWorld->removeCollisionObject(obj); m_dynamicsWorld->removeCollisionObject(obj);
delete obj; delete obj;
} }
for(int i=0;i<m_softbodies.size();++i) for(int i=0;i<m_softbodies.size();++i)
{ {
delete m_softbodies[i]; btSoftBody* softbody = m_softbodies[i];
delete softbody;
} }
m_softbodies.clear(); m_softbodies.clear();
m_sparsesdf.Reset(); m_softBodyWorldInfo.m_sparsesdf.Reset();
/* Init */ /* Init */
void (*demofncs[])(SoftDemo*)= void (*demofncs[])(SoftDemo*)=
{ {
Init_Cloth, Init_Cloth,
Init_Pressure, Init_Pressure,
@@ -813,48 +811,52 @@ void (*demofncs[])(SoftDemo*)=
Init_Bunny, Init_Bunny,
Init_BunnyMatch, Init_BunnyMatch,
}; };
current_demo=current_demo%(sizeof(demofncs)/sizeof(demofncs[0])); current_demo=current_demo%(sizeof(demofncs)/sizeof(demofncs[0]));
m_softbodyimpl.air_density = (btScalar)1.2;
m_softbodyimpl.water_density = 0;
m_softbodyimpl.water_offset = 0; m_softBodyWorldInfo.air_density = (btScalar)1.2;
m_softbodyimpl.water_normal = btVector3(0,0,0); m_softBodyWorldInfo.water_density = 0;
m_autocam = false; m_softBodyWorldInfo.water_offset = 0;
demofncs[current_demo](this); m_softBodyWorldInfo.water_normal = btVector3(0,0,0);
m_autocam = false;
demofncs[current_demo](this);
} }
void SoftDemo::renderme() void SoftDemo::renderme()
{ {
DemoApplication::renderme(); DemoApplication::renderme();
btIDebugDraw* idraw=m_dynamicsWorld->getDebugDrawer(); btIDebugDraw* idraw=m_dynamicsWorld->getDebugDrawer();
/* Bodies */ /* Bodies */
btVector3 ps(0,0,0); btVector3 ps(0,0,0);
int nps=0; int nps=0;
for(int ib=0;ib<m_softbodies.size();++ib) for(int ib=0;ib<m_softbodies.size();++ib)
{ {
btSoftBody* psb=m_softbodies[ib]; btSoftBody* psb=m_softbodies[ib];
nps+=psb->m_nodes.size(); nps+=psb->getNodes().size();
for(int i=0;i<psb->m_nodes.size();++i) for(int i=0;i<psb->getNodes().size();++i)
{ {
ps+=psb->m_nodes[i].m_x; ps+=psb->getNodes()[i].m_x;
} }
DrawFrame(psb,idraw); btSoftBodyHelpers::DrawFrame(psb,idraw);
Draw(psb,idraw,fDrawFlags::Std); btSoftBodyHelpers::Draw(psb,idraw,fDrawFlags::Std);
} }
ps/=nps; ps/=nps;
if(m_autocam) if(m_autocam)
m_cameraTargetPosition+=(ps-m_cameraTargetPosition)*0.01; m_cameraTargetPosition+=(ps-m_cameraTargetPosition)*0.01;
else else
m_cameraTargetPosition=btVector3(0,0,0); m_cameraTargetPosition=btVector3(0,0,0);
/* Water level */ /* Water level */
static const btVector3 axis[]={btVector3(1,0,0), static const btVector3 axis[]={btVector3(1,0,0),
btVector3(0,1,0), btVector3(0,1,0),
btVector3(0,0,1)}; btVector3(0,0,1)};
if(m_softbodyimpl.water_density>0) if(m_softBodyWorldInfo.water_density>0)
{ {
const btVector3 c= btVector3((btScalar)0.25,(btScalar)0.25,1); const btVector3 c= btVector3((btScalar)0.25,(btScalar)0.25,1);
const btScalar a= (btScalar)0.5; const btScalar a= (btScalar)0.5;
const btVector3 n= m_softbodyimpl.water_normal; const btVector3 n= m_softBodyWorldInfo.water_normal;
const btVector3 o= -n*m_softbodyimpl.water_offset; const btVector3 o= -n*m_softBodyWorldInfo.water_offset;
const btVector3 x= cross(n,axis[n.minAxis()]).normalized(); const btVector3 x= cross(n,axis[n.minAxis()]).normalized();
const btVector3 y= cross(x,n).normalized(); const btVector3 y= cross(x,n).normalized();
const btScalar s= 25; const btScalar s= 25;
@@ -865,7 +867,7 @@ if(m_softbodyimpl.water_density>0)
void SoftDemo::keyboardCallback(unsigned char key, int x, int y) void SoftDemo::keyboardCallback(unsigned char key, int x, int y)
{ {
switch(key) switch(key)
{ {
case ']': ++current_demo;clientResetScene();break; case ']': ++current_demo;clientResetScene();break;
case '[': --current_demo;clientResetScene();break; case '[': --current_demo;clientResetScene();break;
@@ -878,44 +880,40 @@ switch(key)
void SoftDemo::initPhysics() void SoftDemo::initPhysics()
{ {
//#define USE_GROUND_PLANE 1
#ifdef USE_GROUND_PLANE
m_collisionShapes.push_back(new btStaticPlaneShape(btVector3(0,1,0),0.5));
#else
///Please don't make the box sizes larger then 1000: the collision detection will be inaccurate.
///See http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=346
m_collisionShapes.push_back(new btBoxShape (btVector3(200,CUBE_HALF_EXTENTS,200))); m_collisionShapes.push_back(new btBoxShape (btVector3(200,CUBE_HALF_EXTENTS,200)));
//m_collisionShapes.push_back(new btCylinderShapeZ (btVector3(5,5,5)));
//m_collisionShapes.push_back(new btSphereShape(5));
#endif
m_collisionShapes.push_back(new btCylinderShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS))); m_collisionShapes.push_back(new btCylinderShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)));
m_dispatcher=0; m_dispatcher=0;
m_collisionConfiguration = new btDefaultCollisionConfiguration();
///register some softbody collision algorithms on top of the default btDefaultCollisionConfiguration
m_collisionConfiguration = new btSoftBodyRigidBodyCollisionConfiguration();
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
m_softBodyWorldInfo.m_dispatcher = m_dispatcher;
////////////////////////////
///Register softbody versus softbody collision algorithm
///Register softbody versus rigidbody collision algorithm
////////////////////////////
btVector3 worldAabbMin(-1000,-1000,-1000); btVector3 worldAabbMin(-1000,-1000,-1000);
btVector3 worldAabbMax(1000,1000,1000); btVector3 worldAabbMax(1000,1000,1000);
m_broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies); m_broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
/// For large worlds or over 16384 objects, use the bt32BitAxisSweep3 broadphase
// m_broadphase = new bt32BitAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
/// When trying to debug broadphase issues, try to use the btSimpleBroadphase
// m_broadphase = new btSimpleBroadphase;
//box-box is in Extras/AlternativeCollisionAlgorithms:it requires inclusion of those files
m_softBodyWorldInfo.m_broadphase = m_broadphase;
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver(); btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver();
m_solver = solver; m_solver = solver;
//default solverMode is SOLVER_RANDMIZE_ORDER. Warmstarting seems not to improve convergence, see
//solver->setSolverMode(0);//btSequentialImpulseConstraintSolver::SOLVER_USE_WARMSTARTING | btSequentialImpulseConstraintSolver::SOLVER_RANDMIZE_ORDER);
btDiscreteDynamicsWorld* world = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); btDiscreteDynamicsWorld* world = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
m_dynamicsWorld = world; m_dynamicsWorld = world;
@@ -938,11 +936,10 @@ void SoftDemo::initPhysics()
btRigidBody* body = localCreateRigidBody(0.f,tr,m_collisionShapes[0]); btRigidBody* body = localCreateRigidBody(0.f,tr,m_collisionShapes[0]);
// clientResetScene(); // clientResetScene();
m_softbodyimpl.pdemo=this; m_softBodyWorldInfo.m_sparsesdf.Initialize();
m_sparsesdf.Initialize(); clientResetScene();
clientResetScene();
} }

View File

@@ -21,7 +21,7 @@ subject to the following restrictions:
#include "DemoApplication.h" #include "DemoApplication.h"
#include "LinearMath/btAlignedObjectArray.h" #include "LinearMath/btAlignedObjectArray.h"
#include "BulletDynamics/SoftBody/btSoftBody.h" #include "BulletDynamics/SoftBody/btSoftBody.h"
#include "BulletDynamics/SoftBody/btSparseSDF.h"
class btBroadphaseInterface; class btBroadphaseInterface;
class btCollisionShape; class btCollisionShape;
@@ -31,31 +31,27 @@ class btConstraintSolver;
struct btCollisionAlgorithmCreateFunc; struct btCollisionAlgorithmCreateFunc;
class btDefaultCollisionConfiguration; class btDefaultCollisionConfiguration;
///collisions between two btSoftBody's
class btSoftSoftCollisionAlgorithm;
///collisions between a btSoftBody and a btRigidBody
class btSoftRididCollisionAlgorithm;
///CcdPhysicsDemo shows basic stacking using Bullet physics, and allows toggle of Ccd (using key '1') ///CcdPhysicsDemo shows basic stacking using Bullet physics, and allows toggle of Ccd (using key '1')
class SoftDemo : public DemoApplication class SoftDemo : public DemoApplication
{ {
public: public:
struct SoftBodyImpl : btSoftBody::ISoftBody
{ btAlignedObjectArray<btSoftSoftCollisionAlgorithm*> m_SoftSoftCollisionAlgorithms;
void Attach(btSoftBody*);
void Detach(btSoftBody*); btAlignedObjectArray<btSoftRididCollisionAlgorithm*> m_SoftRigidCollisionAlgorithms;
void StartCollide(const btVector3&,const btVector3&);
bool CheckContactPrecise(const btVector3&, btSoftBody::btSoftBodyWorldInfo m_softBodyWorldInfo;
btSoftBody::ISoftBody::sCti&);
bool CheckContact( const btVector3&,
btSoftBody::ISoftBody::sCti&);
void EndCollide();
void EvaluateMedium( const btVector3&,
btSoftBody::ISoftBody::sMedium&);
SoftDemo* pdemo;
btScalar air_density;
btScalar water_density;
btScalar water_offset;
btVector3 water_normal;
} m_softbodyimpl;
btAlignedObjectArray<btSoftBody*> m_softbodies; btAlignedObjectArray<btSoftBody*> m_softbodies;
btSparseSdf<3> m_sparsesdf;
bool m_autocam; bool m_autocam;
@@ -66,12 +62,6 @@ struct SoftBodyImpl : btSoftBody::ISoftBody
btCollisionDispatcher* m_dispatcher; btCollisionDispatcher* m_dispatcher;
#ifdef USE_PARALLEL_DISPATCHER
#ifdef WIN32
class Win32ThreadSupport* m_threadSupportCollision;
class Win32ThreadSupport* m_threadSupportSolver;
#endif
#endif
btConstraintSolver* m_solver; btConstraintSolver* m_solver;
@@ -80,7 +70,7 @@ struct SoftBodyImpl : btSoftBody::ISoftBody
btDefaultCollisionConfiguration* m_collisionConfiguration; btDefaultCollisionConfiguration* m_collisionConfiguration;
public: public:
void initPhysics(); void initPhysics();

View File

@@ -0,0 +1,77 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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 "btSoftBodyRigidBodyCollisionConfiguration.h"
#include "btSoftRigidCollisionAlgorithm.h"
#include "btSoftSoftCollisionAlgorithm.h"
btSoftBodyRigidBodyCollisionConfiguration::btSoftBodyRigidBodyCollisionConfiguration(btStackAlloc* stackAlloc,btPoolAllocator* persistentManifoldPool,btPoolAllocator* collisionAlgorithmPool)
:btDefaultCollisionConfiguration(stackAlloc,persistentManifoldPool,collisionAlgorithmPool)
{
void* mem;
mem = btAlignedAlloc(sizeof(btSoftSoftCollisionAlgorithm::CreateFunc),16);
m_softSoftCreateFunc = new(mem) btSoftSoftCollisionAlgorithm::CreateFunc;
mem = btAlignedAlloc(sizeof(btSoftRigidCollisionAlgorithm::CreateFunc),16);
m_softRigidCreateFunc = new(mem) btSoftRigidCollisionAlgorithm::CreateFunc;
mem = btAlignedAlloc(sizeof(btSoftRigidCollisionAlgorithm::CreateFunc),16);
m_swappedSoftRigidCreateFunc = new(mem) btSoftRigidCollisionAlgorithm::CreateFunc;
m_swappedSoftRigidCreateFunc->m_swapped=true;
}
btSoftBodyRigidBodyCollisionConfiguration::~btSoftBodyRigidBodyCollisionConfiguration()
{
m_softSoftCreateFunc->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_softSoftCreateFunc);
m_softRigidCreateFunc->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_softRigidCreateFunc);
m_swappedSoftRigidCreateFunc->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_swappedSoftRigidCreateFunc);
}
///creation of soft-soft and soft-rigid, and otherwise fallback to base class implementation
btCollisionAlgorithmCreateFunc* btSoftBodyRigidBodyCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1)
{
///try to handle the softbody interactions first
if ((proxyType0 == SOFTBODY_SHAPE_PROXYTYPE ) && (proxyType1==SOFTBODY_SHAPE_PROXYTYPE))
{
return m_softSoftCreateFunc;
}
///other can't be also softbody, so assume rigid for now
if (proxyType0 == SOFTBODY_SHAPE_PROXYTYPE )
{
return m_softRigidCreateFunc;
}
///other can't be also softbody, so assume rigid for now
if (proxyType1 == SOFTBODY_SHAPE_PROXYTYPE )
{
return m_swappedSoftRigidCreateFunc;
}
///fallback to the regular rigid collision shape
return btDefaultCollisionConfiguration::getCollisionAlgorithmCreateFunc(proxyType0,proxyType1);
}

View File

@@ -0,0 +1,46 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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 BT_SOFTBODY_RIGIDBODY_COLLISION_CONFIGURATION
#define BT_SOFTBODY_RIGIDBODY_COLLISION_CONFIGURATION
#include "BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h"
class btVoronoiSimplexSolver;
class btGjkEpaPenetrationDepthSolver;
///btSoftBodyRigidBodyCollisionConfiguration add softbody interaction on top of btDefaultCollisionConfiguration
class btSoftBodyRigidBodyCollisionConfiguration : public btDefaultCollisionConfiguration
{
//default CreationFunctions, filling the m_doubleDispatch table
btCollisionAlgorithmCreateFunc* m_softSoftCreateFunc;
btCollisionAlgorithmCreateFunc* m_softRigidCreateFunc;
btCollisionAlgorithmCreateFunc* m_swappedSoftRigidCreateFunc;
public:
btSoftBodyRigidBodyCollisionConfiguration(btStackAlloc* stackAlloc=0,btPoolAllocator* persistentManifoldPool=0,btPoolAllocator* collisionAlgorithmPool=0);
virtual ~btSoftBodyRigidBodyCollisionConfiguration();
///creation of soft-soft and soft-rigid, and otherwise fallback to base class implementation
virtual btCollisionAlgorithmCreateFunc* getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1);
};
#endif //BT_SOFTBODY_RIGIDBODY_COLLISION_CONFIGURATION

View File

@@ -0,0 +1,94 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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 "btSoftRigidCollisionAlgorithm.h"
#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
#include "BulletCollision/CollisionShapes/btSphereShape.h"
#include "BulletCollision/CollisionShapes/btBoxShape.h"
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
#include "BulletDynamics/SoftBody/btSoftBody.h"
///TODO: include all the shapes that the softbody can collide with
///alternatively, implement special case collision algorithms (just like for rigid collision shapes)
//#include <stdio.h>
btSoftRigidCollisionAlgorithm::btSoftRigidCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* col0,btCollisionObject* col1, bool isSwapped)
: btCollisionAlgorithm(ci),
//m_ownManifold(false),
//m_manifoldPtr(mf),
m_isSwapped(isSwapped)
{
m_softBody = m_isSwapped? (btSoftBody*)col1 : (btSoftBody*)col0;
m_rigidCollisionObject = m_isSwapped? col0 : col1;
//quick fix, add overlapping rigidbody to softbody, so it can be handled within btSoftBody::Step method
m_softBody->m_overlappingRigidBodies.push_back(m_rigidCollisionObject);
///store the contacts straight into the btSoftBody for now?
/*if (!m_manifoldPtr && m_dispatcher->needsCollision(sphereObj,boxObj))
{
m_manifoldPtr = m_dispatcher->getNewManifold(sphereObj,boxObj);
m_ownManifold = true;
}
*/
}
btSoftRigidCollisionAlgorithm::~btSoftRigidCollisionAlgorithm()
{
m_softBody->m_overlappingRigidBodies.remove(m_rigidCollisionObject);
/*if (m_ownManifold)
{
if (m_manifoldPtr)
m_dispatcher->releaseManifold(m_manifoldPtr);
}
*/
}
#include <stdio.h>
void btSoftRigidCollisionAlgorithm::processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
{
(void)dispatchInfo;
(void)resultOut;
//printf("btSoftRigidCollisionAlgorithm\n");
btSoftBody* softBody = m_isSwapped? (btSoftBody*)body1 : (btSoftBody*)body0;
btCollisionObject* rigidCollisionObject = m_isSwapped? body0 : body1;
///do your stuff here
}
btScalar btSoftRigidCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* col0,btCollisionObject* col1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
{
(void)resultOut;
(void)dispatchInfo;
(void)col0;
(void)col1;
//not yet
return btScalar(1.);
}

View File

@@ -0,0 +1,69 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SOFT_RIGID_COLLISION_ALGORITHM_H
#define SOFT_RIGID_COLLISION_ALGORITHM_H
#include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
#include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
#include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
class btPersistentManifold;
#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
#include "LinearMath/btVector3.h"
struct btSoftBody;
/// btSoftRigidCollisionAlgorithm provides collision detection between btSoftBody and btRigidBody
class btSoftRigidCollisionAlgorithm : public btCollisionAlgorithm
{
// bool m_ownManifold;
// btPersistentManifold* m_manifoldPtr;
btSoftBody* m_softBody;
btCollisionObject* m_rigidCollisionObject;
///for rigid versus soft (instead of soft versus rigid), we use this swapped boolean
bool m_isSwapped;
public:
btSoftRigidCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* col0,btCollisionObject* col1, bool isSwapped);
virtual ~btSoftRigidCollisionAlgorithm();
virtual void processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
virtual btScalar calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
struct CreateFunc :public btCollisionAlgorithmCreateFunc
{
virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1)
{
void* mem = ci.m_dispatcher1->allocateCollisionAlgorithm(sizeof(btSoftRigidCollisionAlgorithm));
if (!m_swapped)
{
return new(mem) btSoftRigidCollisionAlgorithm(0,ci,body0,body1,false);
} else
{
return new(mem) btSoftRigidCollisionAlgorithm(0,ci,body0,body1,true);
}
}
};
};
#endif //SOFT_RIGID_COLLISION_ALGORITHM_H

View File

@@ -0,0 +1,104 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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 "btSoftSoftCollisionAlgorithm.h"
#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
#include "BulletCollision/CollisionShapes/btBoxShape.h"
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
#include "BulletDynamics/SoftBody/btSoftBody.h"
#define USE_PERSISTENT_CONTACTS 1
btSoftSoftCollisionAlgorithm::btSoftSoftCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* obj0,btCollisionObject* obj1)
: btCollisionAlgorithm(ci)
//m_ownManifold(false),
//m_manifoldPtr(mf)
{
m_softBody0 = (btSoftBody*) obj0;
m_softBody1 = (btSoftBody*) obj1;
m_softBody0->m_overlappingSoftBodies.push_back(m_softBody1);
m_softBody1->m_overlappingSoftBodies.push_back(m_softBody0);
/*if (!m_manifoldPtr && m_dispatcher->needsCollision(obj0,obj1))
{
m_manifoldPtr = m_dispatcher->getNewManifold(obj0,obj1);
m_ownManifold = true;
}
*/
}
btSoftSoftCollisionAlgorithm::~btSoftSoftCollisionAlgorithm()
{
m_softBody0->m_overlappingSoftBodies.remove(m_softBody1);
m_softBody1->m_overlappingSoftBodies.remove(m_softBody0);
//this gets called when the overlap stops.
//here is where contacts (manifolds) should be removed
/*
if (m_ownManifold)
{
if (m_manifoldPtr)
m_dispatcher->releaseManifold(m_manifoldPtr);
}
*/
}
void btSoftSoftCollisionAlgorithm::processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
{
btCollisionObject* col0 = body0;
btCollisionObject* col1 = body1;
/*
btBoxShape* box0 = (btBoxShape*)col0->getCollisionShape();
btBoxShape* box1 = (btBoxShape*)col1->getCollisionShape();
/// report a contact. internally this will be kept persistent, and contact reduction is done
resultOut->setPersistentManifold(m_manifoldPtr);
#ifndef USE_PERSISTENT_CONTACTS
m_manifoldPtr->clearManifold();
#endif //USE_PERSISTENT_CONTACTS
btDiscreteCollisionDetectorInterface::ClosestPointInput input;
input.m_maximumDistanceSquared = 1e30f;
input.m_transformA = body0->getWorldTransform();
input.m_transformB = body1->getWorldTransform();
btBoxBoxDetector detector(box0,box1);
detector.getClosestPoints(input,*resultOut,dispatchInfo.m_debugDraw);
#ifdef USE_PERSISTENT_CONTACTS
// refreshContactPoints is only necessary when using persistent contact points. otherwise all points are newly added
if (m_ownManifold)
{
resultOut->refreshContactPoints();
}
#endif //USE_PERSISTENT_CONTACTS
*/
}
btScalar btSoftSoftCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
{
//not yet
return 1.f;
}

View File

@@ -0,0 +1,62 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SOFT_SOFT_COLLISION_ALGORITHM_H
#define SOFT_SOFT_COLLISION_ALGORITHM_H
#include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
#include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
#include "BulletCollision/BroadphaseCollision/btDispatcher.h"
#include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
class btPersistentManifold;
struct btSoftBody;
///collision detection between two btSoftBody shapes
class btSoftSoftCollisionAlgorithm : public btCollisionAlgorithm
{
bool m_ownManifold;
btPersistentManifold* m_manifoldPtr;
btSoftBody* m_softBody0;
btSoftBody* m_softBody1;
public:
btSoftSoftCollisionAlgorithm(const btCollisionAlgorithmConstructionInfo& ci)
: btCollisionAlgorithm(ci) {}
virtual void processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
virtual btScalar calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
btSoftSoftCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* body0,btCollisionObject* body1);
virtual ~btSoftSoftCollisionAlgorithm();
struct CreateFunc :public btCollisionAlgorithmCreateFunc
{
virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1)
{
int bbsize = sizeof(btSoftSoftCollisionAlgorithm);
void* ptr = ci.m_dispatcher1->allocateCollisionAlgorithm(bbsize);
return new(ptr) btSoftSoftCollisionAlgorithm(0,ci,body0,body1);
}
};
};
#endif //SOFT_SOFT_COLLISION_ALGORITHM_H

View File

@@ -59,6 +59,8 @@ CONCAVE_SHAPES_END_HERE,
COMPOUND_SHAPE_PROXYTYPE, COMPOUND_SHAPE_PROXYTYPE,
SOFTBODY_SHAPE_PROXYTYPE,
MAX_BROADPHASE_COLLISION_TYPES MAX_BROADPHASE_COLLISION_TYPES
}; };

View File

@@ -81,7 +81,7 @@ public:
} }
btCollisionAlgorithmCreateFunc* getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1); virtual btCollisionAlgorithmCreateFunc* getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1);
}; };

File diff suppressed because it is too large Load Diff

View File

@@ -14,8 +14,8 @@ subject to the following restrictions:
*/ */
///btSoftBody implementation by Nathanael Presson ///btSoftBody implementation by Nathanael Presson
#ifndef _312AEEF3_52DA_4ff6_B804_FCF937182E46_ #ifndef _BT_SOFT_BODY_H
#define _312AEEF3_52DA_4ff6_B804_FCF937182E46_ #define _BT_SOFT_BODY_H
#include "LinearMath/btAlignedObjectArray.h" #include "LinearMath/btAlignedObjectArray.h"
#include "LinearMath/btPoint3.h" #include "LinearMath/btPoint3.h"
@@ -23,67 +23,100 @@ subject to the following restrictions:
#include "LinearMath/btIDebugDraw.h" #include "LinearMath/btIDebugDraw.h"
#include "BulletDynamics/Dynamics/btRigidBody.h" #include "BulletDynamics/Dynamics/btRigidBody.h"
// #include "BulletCollision/CollisionShapes/btConcaveShape.h"
// btSoftBody #include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
// #include "BulletDynamics/SoftBody/btSparseSDF.h"
struct btSoftBody
{ class btBroadphaseInterface;
class btCollisionDispatcher;
/// btSoftBody is work-in-progress
struct btSoftBody : public btCollisionObject
{
// //
// Enumerations // Enumerations
// //
/* eLType */ ///eLType
struct eLType { enum _ { struct eLType { enum _ {
Structural, /* Master constraints */ Structural, ///Master constraints
Bending, /* Secondary constraints */ Bending, ///Secondary constraints
};}; };};
/* eAeroModel */ ////eAeroModel
struct eAeroModel { enum _ { struct eAeroModel { enum _ {
V_Point, /* Vertex normals are oriented toward velocity */ V_Point, ///Vertex normals are oriented toward velocity
V_TwoSided, /* Vertex normals are fliped to match velocity */ V_TwoSided, ///Vertex normals are fliped to match velocity
V_OneSided, /* Vertex normals are taken as it is */ V_OneSided, ///Vertex normals are taken as it is
F_TwoSided, /* Face normals are fliped to match velocity */ F_TwoSided, ///Face normals are fliped to match velocity
F_OneSided, /* Face normals are taken as it is */ F_OneSided, ///Face normals are taken as it is
};}; };};
// struct btSoftBodyWorldInfo
// Interfaces
//
/* ISoftBody */
struct ISoftBody
{ {
btScalar air_density;
btScalar water_density;
btScalar water_offset;
btVector3 water_normal;
btBroadphaseInterface* m_broadphase;
btCollisionDispatcher* m_dispatcher;
btSparseSdf<3> m_sparsesdf;
};
//reference or copy?
btSoftBodyWorldInfo& m_worldInfo;
///constructor
btSoftBody(btSoftBody::btSoftBodyWorldInfo& worldInfo,int node_count,
const btVector3* x,
const btScalar* m);
///sCti is Softbody contact info
struct sCti struct sCti
{ {
btRigidBody* m_body; /* Rigid body */ btRigidBody* m_body; /* Rigid body */
btVector3 m_normal; /* Outward normal */ btVector3 m_normal; /* Outward normal */
btScalar m_offset; /* Offset from origin */ btScalar m_offset; /* Offset from origin */
}; };
void StartCollide(const btVector3& aabbMin,const btVector3& aabbMax)
{
//??
}
void EndCollide()
{
//??
}
//
bool CheckContact( const btVector3& x, btSoftBody::sCti& cti);
////
///destructor
virtual ~btSoftBody();
struct sMedium struct sMedium
{ {
btVector3 m_velocity; /* Velocity */ btVector3 m_velocity; /* Velocity */
btScalar m_pressure; /* Pressure */ btScalar m_pressure; /* Pressure */
btScalar m_density; /* Density */ btScalar m_density; /* Density */
}; };
virtual void Attach(btSoftBody*)
{} virtual void EvaluateMedium( const btVector3& /*position*/, sMedium& medium)
virtual void Detach(btSoftBody*) {
{ delete this; } medium.m_velocity=btVector3(0,0,0);
virtual void StartCollide( const btVector3& /*minbounds*/,
const btVector3& /*maxbounds*/)
{}
virtual bool CheckContact (const btVector3& /*position*/,
sCti& /*contact*/)
{ return(false); }
virtual void EndCollide()
{}
virtual void EvaluateMedium( const btVector3& /*position*/,
sMedium& medium)
{ medium.m_velocity=btVector3(0,0,0);
medium.m_pressure=0; medium.m_pressure=0;
medium.m_density=0; } medium.m_density=0;
}; }
// //
// Internal types // Internal types
@@ -97,7 +130,7 @@ struct btSoftBody
{ {
void* m_tag; // User data void* m_tag; // User data
}; };
/* Node */ ///Node
struct Node : Element struct Node : Element
{ {
btVector3 m_x; // Position btVector3 m_x; // Position
@@ -117,7 +150,7 @@ struct btSoftBody
btScalar m_kST; // Stiffness coefficient btScalar m_kST; // Stiffness coefficient
btScalar m_c0; // (ima+imb)*kLST btScalar m_c0; // (ima+imb)*kLST
btScalar m_c1; // rl^2 btScalar m_c1; // rl^2
eLType::_ m_type; // Link type btSoftBody::eLType::_ m_type; // Link type
}; };
/* Face */ /* Face */
struct Face : Element struct Face : Element
@@ -129,7 +162,7 @@ struct btSoftBody
/* Contact */ /* Contact */
struct Contact struct Contact
{ {
ISoftBody::sCti m_cti; // Contact infos btSoftBody::sCti m_cti; // Contact infos
Node* m_node; // Owner node Node* m_node; // Owner node
btMatrix3x3 m_c0; // Impulse matrix btMatrix3x3 m_c0; // Impulse matrix
btVector3 m_c1; // Relative anchor btVector3 m_c1; // Relative anchor
@@ -160,7 +193,7 @@ struct btSoftBody
/* Config */ /* Config */
struct Config struct Config
{ {
eAeroModel::_ aeromodel; // Aerodynamic model (default: V_Point) btSoftBody::eAeroModel::_ aeromodel; // Aerodynamic model (default: V_Point)
btScalar kLST; // Linear stiffness coefficient [0,1] btScalar kLST; // Linear stiffness coefficient [0,1]
btScalar kDP; // Damping coefficient [0,1] btScalar kDP; // Damping coefficient [0,1]
btScalar kDG; // Drag coefficient [0,+inf] btScalar kDG; // Drag coefficient [0,+inf]
@@ -196,11 +229,99 @@ struct btSoftBody
Config m_cfg; // Configuration Config m_cfg; // Configuration
Pose m_pose; // Pose Pose m_pose; // Pose
ISoftBody* m_isb; // ISoftBody
void* m_tag; // User data void* m_tag; // User data
//////////////////////
///btSoftBodyCollisionShape is work-in-progress collision shape for softbodies
class btSoftBodyCollisionShape : public btConcaveShape
{
static btVector3 m_sScaling;
public:
tNodeArray m_nodes; // Nodes tNodeArray m_nodes; // Nodes
tLinkArray m_links; // Links tLinkArray m_links; // Links
tFaceArray m_faces; // Faces tFaceArray m_faces; // Faces
btSoftBodyCollisionShape();
virtual ~btSoftBodyCollisionShape();
virtual void processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const;
///getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t.
virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const
{
///not yet
btAssert(0);
}
virtual int getShapeType() const
{
return SOFTBODY_SHAPE_PROXYTYPE;
}
virtual void setLocalScaling(const btVector3& scaling)
{
///not yet
btAssert(0);
}
virtual const btVector3& getLocalScaling() const
{
///not yet
btAssert(0);
return m_sScaling;
}
virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const
{
///not yet
btAssert(0);
}
virtual const char* getName()const
{
return "SoftBody";
}
};
btSoftBodyCollisionShape* m_softBodyCollisionShape;
btCollisionObjectArray m_overlappingRigidBodies;
btAlignedObjectArray<btSoftBody*> m_overlappingSoftBodies;
//////////////////////
inline tNodeArray& getNodes()
{
return m_softBodyCollisionShape->m_nodes;
}
inline const tNodeArray& getNodes() const
{
return m_softBodyCollisionShape->m_nodes;
}
inline tLinkArray& getLinks()
{
return m_softBodyCollisionShape->m_links;
}
inline const tLinkArray& getLinks() const
{
return m_softBodyCollisionShape->m_links;
}
inline tFaceArray& getFaces()
{
return m_softBodyCollisionShape->m_faces;
}
inline const tFaceArray& getFaces() const
{
return m_softBodyCollisionShape->m_faces;
}
tAnchorArray m_anchors; // Anchors tAnchorArray m_anchors; // Anchors
tContactArray m_contacts; // Contacts tContactArray m_contacts; // Contacts
btScalar m_timeacc; // Time accumulator btScalar m_timeacc; // Time accumulator
@@ -211,11 +332,6 @@ struct btSoftBody
// Api // Api
// //
/* Create a soft body */
static btSoftBody* Create( ISoftBody* isoftbody,
int node_count,
const btVector3* x=0,
const btScalar* m=0);
/* Delete a body */ /* Delete a body */
void Delete(); void Delete();
/* Check for existing link */ /* Check for existing link */
@@ -287,70 +403,16 @@ struct btSoftBody
/* Step */ /* Step */
void Step( btScalar dt); void Step( btScalar dt);
}; void updateBounds();
// void updateTransform()
// Helpers {
// updateBounds();
}
/* fDrawFlags */
struct fDrawFlags { enum _ {
Nodes = 0x0001,
SLinks = 0x0002,
BLinks = 0x0004,
Faces = 0x0008,
Tetras = 0x0010,
Normals = 0x0020,
Contacts = 0x0040,
Anchors = 0x0080,
/* presets */
Links = SLinks+BLinks,
Std = SLinks+Faces+Anchors,
StdTetra = Std-Faces+Tetras,
};};
/* Draw body */ };
void Draw( btSoftBody* psb,
btIDebugDraw* idraw,
int drawflags=fDrawFlags::Std);
/* Draw body infos */
void DrawInfos( btSoftBody* psb,
btIDebugDraw* idraw,
bool masses,
bool areas,
bool stress);
/* Draw rigid frame */
void DrawFrame( btSoftBody* psb,
btIDebugDraw* idraw);
/* Create a rope */
btSoftBody* CreateRope( btSoftBody::ISoftBody* isoftbody,
const btVector3& from,
const btVector3& to,
int res,
int fixeds);
/* Create a patch */
btSoftBody* CreatePatch( btSoftBody::ISoftBody* isoftbody,
const btVector3& corner00,
const btVector3& corner10,
const btVector3& corner01,
const btVector3& corner11,
int resx,
int resy,
int fixeds,
bool gendiags);
/* Create an ellipsoid */
btSoftBody* CreateEllipsoid(btSoftBody::ISoftBody* isoftbody,
const btVector3& center,
const btVector3& radius,
int res);
/* Create from convex-hull */
btSoftBody* CreateFromConvexHull( btSoftBody::ISoftBody* isoftbody,
const btVector3* vertices,
int nvertices);
/* Create from trimesh */
btSoftBody* CreateFromTriMesh( btSoftBody::ISoftBody* isoftbody,
const btScalar* vertices,
const int* triangles,
int ntriangles);
#endif
#endif //_BT_SOFT_BODY_H

View File

@@ -17,23 +17,21 @@ subject to the following restrictions:
#include "btSoftBody.h" #include "btSoftBody.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "btSoftBodyHelpers.h"
namespace btsoftbody_internals
{
// //
static void drawVertex( btIDebugDraw* idraw, void btSoftBodyHelpers::drawVertex( btIDebugDraw* idraw,
const btVector3& x,btScalar s,const btVector3& c) const btVector3& x,btScalar s,const btVector3& c)
{ {
idraw->drawLine(x-btVector3(s,0,0),x+btVector3(s,0,0),c); idraw->drawLine(x-btVector3(s,0,0),x+btVector3(s,0,0),c);
idraw->drawLine(x-btVector3(0,s,0),x+btVector3(0,s,0),c); idraw->drawLine(x-btVector3(0,s,0),x+btVector3(0,s,0),c);
idraw->drawLine(x-btVector3(0,0,s),x+btVector3(0,0,s),c); idraw->drawLine(x-btVector3(0,0,s),x+btVector3(0,0,s),c);
} }
// //
static btVector3 stresscolor(btScalar stress) btVector3 btSoftBodyHelpers::stresscolor(btScalar stress)
{ {
static const btVector3 spectrum[]= { static const btVector3 spectrum[]= {
btVector3(1,0,1), btVector3(1,0,1),
btVector3(0,0,1), btVector3(0,0,1),
btVector3(0,1,1), btVector3(0,1,1),
@@ -42,47 +40,45 @@ static const btVector3 spectrum[]= {
btVector3(1,0,0), btVector3(1,0,0),
btVector3(1,0,0), btVector3(1,0,0),
}; };
static const int ncolors=sizeof(spectrum)/sizeof(spectrum[0])-1; static const int ncolors=sizeof(spectrum)/sizeof(spectrum[0])-1;
static const btScalar one=1; static const btScalar one=1;
stress=btMax<btScalar>(0,btMin<btScalar>(1,stress))*ncolors; stress=btMax<btScalar>(0,btMin<btScalar>(1,stress))*ncolors;
const int sel=(int)stress; const int sel=(int)stress;
const btScalar frc=stress-sel; const btScalar frc=stress-sel;
return(spectrum[sel]+(spectrum[sel+1]-spectrum[sel])*frc); return(spectrum[sel]+(spectrum[sel+1]-spectrum[sel])*frc);
} }
}
using namespace btsoftbody_internals;
// //
void Draw( btSoftBody* psb, void btSoftBodyHelpers::Draw( btSoftBody* psb,
btIDebugDraw* idraw, btIDebugDraw* idraw,
int drawflags) int drawflags)
{ {
const btScalar scl=(btScalar)0.1; const btScalar scl=(btScalar)0.1;
const btScalar nscl=scl*5; const btScalar nscl=scl*5;
const btScalar alpha=(btScalar)0.5; const btScalar alpha=(btScalar)0.5;
const btVector3 scolor=btVector3(0,0,0); const btVector3 scolor=btVector3(0,0,0);
const btVector3 bcolor=btVector3(1,1,0); const btVector3 bcolor=btVector3(1,1,0);
const btVector3 ncolor=btVector3(1,1,1); const btVector3 ncolor=btVector3(1,1,1);
const btVector3 ccolor=btVector3(1,0,0); const btVector3 ccolor=btVector3(1,0,0);
/* Nodes */ /* Nodes */
if(0!=(drawflags&fDrawFlags::Nodes)) if(0!=(drawflags&fDrawFlags::Nodes))
{ {
for(int i=0;i<psb->m_nodes.size();++i) for(int i=0;i<psb->getNodes().size();++i)
{ {
const btSoftBody::Node& n=psb->m_nodes[i]; const btSoftBody::Node& n=psb->getNodes()[i];
idraw->drawLine(n.m_x-btVector3(scl,0,0),n.m_x+btVector3(scl,0,0),btVector3(1,0,0)); idraw->drawLine(n.m_x-btVector3(scl,0,0),n.m_x+btVector3(scl,0,0),btVector3(1,0,0));
idraw->drawLine(n.m_x-btVector3(0,scl,0),n.m_x+btVector3(0,scl,0),btVector3(0,1,0)); idraw->drawLine(n.m_x-btVector3(0,scl,0),n.m_x+btVector3(0,scl,0),btVector3(0,1,0));
idraw->drawLine(n.m_x-btVector3(0,0,scl),n.m_x+btVector3(0,0,scl),btVector3(0,0,1)); idraw->drawLine(n.m_x-btVector3(0,0,scl),n.m_x+btVector3(0,0,scl),btVector3(0,0,1));
} }
} }
/* Links */ /* Links */
if(0!=(drawflags&fDrawFlags::Links)) if(0!=(drawflags&fDrawFlags::Links))
{ {
for(int i=0;i<psb->m_links.size();++i) for(int i=0;i<psb->getLinks().size();++i)
{ {
const btSoftBody::Link& l=psb->m_links[i]; const btSoftBody::Link& l=psb->getLinks()[i];
switch(l.m_type) switch(l.m_type)
{ {
case btSoftBody::eLType::Structural: case btSoftBody::eLType::Structural:
@@ -92,19 +88,19 @@ if(0!=(drawflags&fDrawFlags::Links))
} }
} }
} }
/* Normals */ /* Normals */
if(0!=(drawflags&fDrawFlags::Normals)) if(0!=(drawflags&fDrawFlags::Normals))
{ {
for(int i=0;i<psb->m_nodes.size();++i) for(int i=0;i<psb->getNodes().size();++i)
{ {
const btSoftBody::Node& n=psb->m_nodes[i]; const btSoftBody::Node& n=psb->getNodes()[i];
const btVector3 d=n.m_n*nscl; const btVector3 d=n.m_n*nscl;
idraw->drawLine(n.m_x,n.m_x+d,ncolor); idraw->drawLine(n.m_x,n.m_x+d,ncolor);
idraw->drawLine(n.m_x,n.m_x-d,ncolor*0.5); idraw->drawLine(n.m_x,n.m_x-d,ncolor*0.5);
} }
} }
/* Contacts */ /* Contacts */
if(0!=(drawflags&fDrawFlags::Contacts)) if(0!=(drawflags&fDrawFlags::Contacts))
{ {
static const btVector3 axis[]={btVector3(1,0,0), static const btVector3 axis[]={btVector3(1,0,0),
btVector3(0,1,0), btVector3(0,1,0),
@@ -121,8 +117,8 @@ if(0!=(drawflags&fDrawFlags::Contacts))
idraw->drawLine(o,o+c.m_cti.m_normal*nscl*3,btVector3(1,1,0)); idraw->drawLine(o,o+c.m_cti.m_normal*nscl*3,btVector3(1,1,0));
} }
} }
/* Anchors */ /* Anchors */
if(0!=(drawflags&fDrawFlags::Anchors)) if(0!=(drawflags&fDrawFlags::Anchors))
{ {
for(int i=0;i<psb->m_anchors.size();++i) for(int i=0;i<psb->m_anchors.size();++i)
{ {
@@ -132,24 +128,24 @@ if(0!=(drawflags&fDrawFlags::Anchors))
drawVertex(idraw,q,0.25,btVector3(0,1,0)); drawVertex(idraw,q,0.25,btVector3(0,1,0));
idraw->drawLine(a.m_node->m_x,q,btVector3(1,1,1)); idraw->drawLine(a.m_node->m_x,q,btVector3(1,1,1));
} }
for(int i=0;i<psb->m_nodes.size();++i) for(int i=0;i<psb->getNodes().size();++i)
{ {
const btSoftBody::Node& n=psb->m_nodes[i]; const btSoftBody::Node& n=psb->getNodes()[i];
if(n.m_im<=0) if(n.m_im<=0)
{ {
drawVertex(idraw,n.m_x,0.25,btVector3(1,0,0)); drawVertex(idraw,n.m_x,0.25,btVector3(1,0,0));
} }
} }
} }
/* Faces */ /* Faces */
if(0!=(drawflags&fDrawFlags::Faces)) if(0!=(drawflags&fDrawFlags::Faces))
{ {
const btScalar scl=(btScalar)0.7; const btScalar scl=(btScalar)0.7;
const btScalar alp=(btScalar)1; const btScalar alp=(btScalar)1;
const btVector3 col(0,(btScalar)0.7,0); const btVector3 col(0,(btScalar)0.7,0);
for(int i=0;i<psb->m_faces.size();++i) for(int i=0;i<psb->getFaces().size();++i)
{ {
const btSoftBody::Face& f=psb->m_faces[i]; const btSoftBody::Face& f=psb->getFaces()[i];
const btVector3 x[]={f.m_n[0]->m_x,f.m_n[1]->m_x,f.m_n[2]->m_x}; const btVector3 x[]={f.m_n[0]->m_x,f.m_n[1]->m_x,f.m_n[2]->m_x};
const btVector3 c=(x[0]+x[1]+x[2])/3; const btVector3 c=(x[0]+x[1]+x[2])/3;
idraw->drawTriangle((x[0]-c)*scl+c, idraw->drawTriangle((x[0]-c)*scl+c,
@@ -162,15 +158,15 @@ if(0!=(drawflags&fDrawFlags::Faces))
} }
// //
void DrawInfos( btSoftBody* psb, void btSoftBodyHelpers::DrawInfos( btSoftBody* psb,
btIDebugDraw* idraw, btIDebugDraw* idraw,
bool masses, bool masses,
bool areas, bool areas,
bool stress) bool stress)
{ {
for(int i=0;i<psb->m_nodes.size();++i) for(int i=0;i<psb->getNodes().size();++i)
{ {
const btSoftBody::Node& n=psb->m_nodes[i]; const btSoftBody::Node& n=psb->getNodes()[i];
char text[2048]={0}; char text[2048]={0};
char buff[1024]; char buff[1024];
if(masses) if(masses)
@@ -188,10 +184,10 @@ for(int i=0;i<psb->m_nodes.size();++i)
} }
// //
void DrawFrame( btSoftBody* psb, void btSoftBodyHelpers::DrawFrame( btSoftBody* psb,
btIDebugDraw* idraw) btIDebugDraw* idraw)
{ {
if(psb->m_pose.m_bframe) if(psb->m_pose.m_bframe)
{ {
static const btScalar ascl=10; static const btScalar ascl=10;
static const btScalar nscl=(btScalar)0.1; static const btScalar nscl=(btScalar)0.1;
@@ -214,39 +210,37 @@ if(psb->m_pose.m_bframe)
} }
// //
btSoftBody* CreateRope( btSoftBody::ISoftBody* isoftbody, btSoftBody* btSoftBodyHelpers::CreateRope( btSoftBody::btSoftBodyWorldInfo& worldInfo, const btVector3& from,
const btVector3& from,
const btVector3& to, const btVector3& to,
int res, int res,
int fixeds) int fixeds)
{ {
/* Create nodes */ /* Create nodes */
const int r=res+2; const int r=res+2;
btVector3* x=new btVector3[r]; btVector3* x=new btVector3[r];
btScalar* m=new btScalar[r]; btScalar* m=new btScalar[r];
for(int i=0;i<r;++i) for(int i=0;i<r;++i)
{ {
const btScalar t=i/(btScalar)(r-1); const btScalar t=i/(btScalar)(r-1);
x[i]=lerp(from,to,t); x[i]=lerp(from,to,t);
m[i]=1; m[i]=1;
} }
btSoftBody* psb=btSoftBody::Create(isoftbody,r,x,m); btSoftBody* psb= new btSoftBody(worldInfo,r,x,m);
if(fixeds&1) psb->SetMass(0,0); if(fixeds&1) psb->SetMass(0,0);
if(fixeds&2) psb->SetMass(r-1,0); if(fixeds&2) psb->SetMass(r-1,0);
delete[] x; delete[] x;
delete[] m; delete[] m;
/* Create links */ /* Create links */
for(int i=1;i<r;++i) for(int i=1;i<r;++i)
{ {
psb->AppendLink(i-1,i,1,btSoftBody::eLType::Structural); psb->AppendLink(i-1,i,1,btSoftBody::eLType::Structural);
} }
/* Finished */ /* Finished */
return(psb); return(psb);
} }
// //
btSoftBody* CreatePatch( btSoftBody::ISoftBody* isoftbody, btSoftBody* btSoftBodyHelpers::CreatePatch(btSoftBody::btSoftBodyWorldInfo& worldInfo,const btVector3& corner00,
const btVector3& corner00,
const btVector3& corner10, const btVector3& corner10,
const btVector3& corner01, const btVector3& corner01,
const btVector3& corner11, const btVector3& corner11,
@@ -256,14 +250,14 @@ btSoftBody* CreatePatch( btSoftBody::ISoftBody* isoftbody,
bool gendiags) bool gendiags)
{ {
#define IDX(_x_,_y_) ((_y_)*rx+(_x_)) #define IDX(_x_,_y_) ((_y_)*rx+(_x_))
/* Create nodes */ /* Create nodes */
if((resx<2)||(resy<2)) return(0); if((resx<2)||(resy<2)) return(0);
const int rx=resx; const int rx=resx;
const int ry=resy; const int ry=resy;
const int tot=rx*ry; const int tot=rx*ry;
btVector3* x=new btVector3[tot]; btVector3* x=new btVector3[tot];
btScalar* m=new btScalar[tot]; btScalar* m=new btScalar[tot];
for(int iy=0;iy<ry;++iy) for(int iy=0;iy<ry;++iy)
{ {
const btScalar ty=iy/(btScalar)(ry-1); const btScalar ty=iy/(btScalar)(ry-1);
const btVector3 py0=lerp(corner00,corner01,ty); const btVector3 py0=lerp(corner00,corner01,ty);
@@ -275,15 +269,15 @@ for(int iy=0;iy<ry;++iy)
m[IDX(ix,iy)]=1; m[IDX(ix,iy)]=1;
} }
} }
btSoftBody* psb=btSoftBody::Create(isoftbody,tot,x,m); btSoftBody* psb=new btSoftBody(worldInfo,tot,x,m);
if(fixeds&1) psb->SetMass(IDX(0,0),0); if(fixeds&1) psb->SetMass(IDX(0,0),0);
if(fixeds&2) psb->SetMass(IDX(rx-1,0),0); if(fixeds&2) psb->SetMass(IDX(rx-1,0),0);
if(fixeds&4) psb->SetMass(IDX(0,ry-1),0); if(fixeds&4) psb->SetMass(IDX(0,ry-1),0);
if(fixeds&8) psb->SetMass(IDX(rx-1,ry-1),0); if(fixeds&8) psb->SetMass(IDX(rx-1,ry-1),0);
delete[] x; delete[] x;
delete[] m; delete[] m;
/* Create links and faces */ /* Create links and faces */
for(int iy=0;iy<ry;++iy) for(int iy=0;iy<ry;++iy)
{ {
for(int ix=0;ix<rx;++ix) for(int ix=0;ix<rx;++ix)
{ {
@@ -319,18 +313,17 @@ for(int iy=0;iy<ry;++iy)
} }
} }
} }
/* Finished */ /* Finished */
#undef IDX #undef IDX
return(psb); return(psb);
} }
// //
btSoftBody* CreateEllipsoid(btSoftBody::ISoftBody* isoftbody, btSoftBody* btSoftBodyHelpers::CreateEllipsoid(btSoftBody::btSoftBodyWorldInfo& worldInfo,const btVector3& center,
const btVector3& center,
const btVector3& radius, const btVector3& radius,
int res) int res)
{ {
struct Hammersley struct Hammersley
{ {
static void Generate(btVector3* x,int n) static void Generate(btVector3* x,int n)
{ {
@@ -345,43 +338,42 @@ struct Hammersley
} }
} }
}; };
btAlignedObjectArray<btVector3> vtx; btAlignedObjectArray<btVector3> vtx;
vtx.resize(3+res); vtx.resize(3+res);
Hammersley::Generate(&vtx[0],vtx.size()); Hammersley::Generate(&vtx[0],vtx.size());
for(int i=0;i<vtx.size();++i) for(int i=0;i<vtx.size();++i)
{ {
vtx[i]=vtx[i]*radius+center; vtx[i]=vtx[i]*radius+center;
} }
return(CreateFromConvexHull(isoftbody,&vtx[0],vtx.size())); return(CreateFromConvexHull(worldInfo,&vtx[0],vtx.size()));
} }
// //
btSoftBody* CreateFromTriMesh( btSoftBody::ISoftBody* isoftbody, btSoftBody* btSoftBodyHelpers::CreateFromTriMesh(btSoftBody::btSoftBodyWorldInfo& worldInfo,const btScalar* vertices,
const btScalar* vertices,
const int* triangles, const int* triangles,
int ntriangles) int ntriangles)
{ {
int maxidx=0; int maxidx=0;
for(int i=0,ni=ntriangles*3;i<ni;++i) for(int i=0,ni=ntriangles*3;i<ni;++i)
{ {
maxidx=btMax(triangles[i],maxidx); maxidx=btMax(triangles[i],maxidx);
} }
++maxidx; ++maxidx;
btAlignedObjectArray<bool> chks; btAlignedObjectArray<bool> chks;
btAlignedObjectArray<btVector3> vtx; btAlignedObjectArray<btVector3> vtx;
chks.resize(maxidx*maxidx,false); chks.resize(maxidx*maxidx,false);
vtx.resize(maxidx); vtx.resize(maxidx);
for(int i=0,j=0,ni=maxidx*3;i<ni;++j,i+=3) for(int i=0,j=0,ni=maxidx*3;i<ni;++j,i+=3)
{ {
vtx[j]=btVector3(vertices[i],vertices[i+1],vertices[i+2]); vtx[j]=btVector3(vertices[i],vertices[i+1],vertices[i+2]);
} }
btSoftBody* psb=btSoftBody::Create(isoftbody,vtx.size(),&vtx[0],0); btSoftBody* psb=new btSoftBody(worldInfo,vtx.size(),&vtx[0],0);
for(int i=0,ni=ntriangles*3;i<ni;i+=3) for(int i=0,ni=ntriangles*3;i<ni;i+=3)
{ {
const int idx[]={triangles[i],triangles[i+1],triangles[i+2]}; const int idx[]={triangles[i],triangles[i+1],triangles[i+2]};
#define IDX(_x_,_y_) ((_y_)*maxidx+(_x_)) #define IDX(_x_,_y_) ((_y_)*maxidx+(_x_))
for(int j=2,k=0;k<3;j=k++) for(int j=2,k=0;k<3;j=k++)
{ {
if(!chks[IDX(idx[j],idx[k])]) if(!chks[IDX(idx[j],idx[k])])
@@ -391,9 +383,9 @@ for(int i=0,ni=ntriangles*3;i<ni;i+=3)
psb->AppendLink(idx[j],idx[k],1,btSoftBody::eLType::Structural); psb->AppendLink(idx[j],idx[k],1,btSoftBody::eLType::Structural);
} }
} }
#undef IDX #undef IDX
psb->AppendFace(idx[0],idx[1],idx[2]); psb->AppendFace(idx[0],idx[1],idx[2]);
} }
psb->RandomizeConstraints(); psb->RandomizeConstraints();
return(psb); return(psb);
} }

View File

@@ -0,0 +1,90 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2008 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SOFT_BODY_HELPERS_H
#define SOFT_BODY_HELPERS_H
#include "btSoftBody.h"
//
// Helpers
//
/* fDrawFlags */
struct fDrawFlags { enum _ {
Nodes = 0x0001,
SLinks = 0x0002,
BLinks = 0x0004,
Faces = 0x0008,
Tetras = 0x0010,
Normals = 0x0020,
Contacts = 0x0040,
Anchors = 0x0080,
/* presets */
Links = SLinks+BLinks,
Std = SLinks+Faces+Anchors,
StdTetra = Std-Faces+Tetras,
};};
struct btSoftBodyHelpers
{
static btVector3 stresscolor(btScalar stress);
static void drawVertex( btIDebugDraw* idraw,
const btVector3& x,btScalar s,const btVector3& c);
/* Draw body */
static void Draw( btSoftBody* psb,
btIDebugDraw* idraw,
int drawflags=fDrawFlags::Std);
/* Draw body infos */
static void DrawInfos( btSoftBody* psb,
btIDebugDraw* idraw,
bool masses,
bool areas,
bool stress);
/* Draw rigid frame */
static void DrawFrame( btSoftBody* psb,
btIDebugDraw* idraw);
/* Create a rope */
static btSoftBody* CreateRope( btSoftBody::btSoftBodyWorldInfo& worldInfo,const btVector3& from,
const btVector3& to,
int res,
int fixeds);
/* Create a patch */
static btSoftBody* CreatePatch( btSoftBody::btSoftBodyWorldInfo& worldInfo,const btVector3& corner00,
const btVector3& corner10,
const btVector3& corner01,
const btVector3& corner11,
int resx,
int resy,
int fixeds,
bool gendiags);
/* Create an ellipsoid */
static btSoftBody* CreateEllipsoid(btSoftBody::btSoftBodyWorldInfo& worldInfo,const btVector3& center,
const btVector3& radius,
int res);
/* Create from convex-hull */
static btSoftBody* CreateFromConvexHull( btSoftBody::btSoftBodyWorldInfo& worldInfo, const btVector3* vertices,
int nvertices);
/* Create from trimesh */
static btSoftBody* CreateFromTriMesh( btSoftBody::btSoftBodyWorldInfo& worldInfo, const btScalar* vertices,
const int* triangles,
int ntriangles);
};
#endif //SOFT_BODY_HELPERS_H