From e13578fee3f8f638c8722be684ccce5a06560834 Mon Sep 17 00:00:00 2001 From: Xuchen Han Date: Fri, 4 Oct 2019 17:53:53 -0700 Subject: [PATCH] add option to turn self-collision on/off --- examples/SharedMemory/PhysicsClientC_API.cpp | 9 +++++ examples/SharedMemory/PhysicsClientC_API.h | 1 + .../PhysicsServerCommandProcessor.cpp | 6 ++++ examples/SharedMemory/SharedMemoryCommands.h | 2 ++ src/BulletSoftBody/btSoftBody.cpp | 33 +++++++++++++------ src/BulletSoftBody/btSoftBody.h | 3 ++ 6 files changed, 44 insertions(+), 10 deletions(-) diff --git a/examples/SharedMemory/PhysicsClientC_API.cpp b/examples/SharedMemory/PhysicsClientC_API.cpp index 608a577b2..914e47037 100644 --- a/examples/SharedMemory/PhysicsClientC_API.cpp +++ b/examples/SharedMemory/PhysicsClientC_API.cpp @@ -386,6 +386,15 @@ B3_SHARED_API int b3LoadSoftBodySetCollisionHardness(b3SharedMemoryCommandHandle return 0; } +B3_SHARED_API int b3LoadSoftBodySetSelfCollision(b3SharedMemoryCommandHandle commandHandle, bool useSelfCollision) +{ + struct SharedMemoryCommand* command = (struct SharedMemoryCommand*)commandHandle; + b3Assert(command->m_type == CMD_LOAD_SOFT_BODY); + command->m_loadSoftBodyArguments.m_useSelfCollision = useSelfCollision; + command->m_updateFlags |= LOAD_SOFT_BODY_SET_SELF_COLLISION; + return 0; +} + B3_SHARED_API int b3LoadSoftBodySetFrictionCoefficient(b3SharedMemoryCommandHandle commandHandle, double frictionCoefficient) { struct SharedMemoryCommand* command = (struct SharedMemoryCommand*)commandHandle; diff --git a/examples/SharedMemory/PhysicsClientC_API.h b/examples/SharedMemory/PhysicsClientC_API.h index 39da0f455..52569807b 100644 --- a/examples/SharedMemory/PhysicsClientC_API.h +++ b/examples/SharedMemory/PhysicsClientC_API.h @@ -636,6 +636,7 @@ extern "C" B3_SHARED_API int b3LoadSoftBodyAddMassSpringForce(b3SharedMemoryCommandHandle commandHandle, double springElasticStiffness , double springDampingStiffness); B3_SHARED_API int b3LoadSoftBodyAddGravityForce(b3SharedMemoryCommandHandle commandHandle, double gravityX, double gravityY, double gravityZ); B3_SHARED_API int b3LoadSoftBodySetCollisionHardness(b3SharedMemoryCommandHandle commandHandle, double collisionHardness); + B3_SHARED_API int b3LoadSoftBodySetSelfCollision(b3SharedMemoryCommandHandle commandHandle, bool useSelfCollision); B3_SHARED_API int b3LoadSoftBodySetFrictionCoefficient(b3SharedMemoryCommandHandle commandHandle, double frictionCoefficient); B3_SHARED_API int b3LoadSoftBodyUseBendingSprings(b3SharedMemoryCommandHandle commandHandle, int useBendingSprings); diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 2d8bc9bd3..e5dc10bb4 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -8144,6 +8144,12 @@ bool PhysicsServerCommandProcessor::processLoadSoftBodyCommand(const struct Shar psb->m_cfg.collisions |= btSoftBody::fCollision::VF_DD; psb->setCollisionFlags(0); psb->setTotalMass(mass); + bool use_self_collision = false; + if (clientCmd.m_updateFlags & LOAD_SOFT_BODY_USE_SELF_COLLISION) + { + use_self_collision = loadSoftBodyArgs.m_useSelfCollision; + } + psb->setSelfCollision(use_self_collision); #else btSoftBody::Material* pm = psb->appendMaterial(); pm->m_kLST = 0.5; diff --git a/examples/SharedMemory/SharedMemoryCommands.h b/examples/SharedMemory/SharedMemoryCommands.h index 7fd317aa4..d23ac1594 100644 --- a/examples/SharedMemory/SharedMemoryCommands.h +++ b/examples/SharedMemory/SharedMemoryCommands.h @@ -501,6 +501,7 @@ enum EnumLoadSoftBodyUpdateFlags LOAD_SOFT_BODY_SET_FRICTION_COEFFICIENT = 1<<10, LOAD_SOFT_BODY_ADD_BENDING_SPRINGS = 1<<11, LOAD_SOFT_BODY_ADD_NEOHOOKEAN_FORCE = 1<<12, + LOAD_SOFT_BODY_SET_SELF_COLLISION = 1<<13, }; enum EnumSimParamInternalSimFlags @@ -525,6 +526,7 @@ struct LoadSoftBodyArgs double m_corotatedLambda; bool m_useBendingSprings; double m_collisionHardness; + double m_useSelfCollision; double m_frictionCoeff; double m_NeoHookeanMu; double m_NeoHookeanLambda; diff --git a/src/BulletSoftBody/btSoftBody.cpp b/src/BulletSoftBody/btSoftBody.cpp index 6f46b63b7..ef3a201c5 100644 --- a/src/BulletSoftBody/btSoftBody.cpp +++ b/src/BulletSoftBody/btSoftBody.cpp @@ -3378,6 +3378,16 @@ btSoftBody::vsolver_t btSoftBody::getSolver(eVSolver::_ solver) return (0); } +void btSoftBody::setSelfCollision(bool useSelfCollision) +{ + m_useSelfCollision = useSelfCollision; +} + +bool btSoftBody::useSelfCollision() +{ + return m_useSelfCollision; +} + // void btSoftBody::defaultCollisionHandler(const btCollisionObjectWrapper* pcoWrap) { @@ -3563,16 +3573,19 @@ void btSoftBody::defaultCollisionHandler(btSoftBody* psb) } else { - btSoftColliders::CollideFF_DD docollide; - docollide.mrg = getCollisionShape()->getMargin() + - psb->getCollisionShape()->getMargin(); - docollide.psb[0] = this; - docollide.psb[1] = psb; - /* psb0 faces vs psb0 faces */ - btDbvntNode* root = copyToDbvnt(this->m_fdbvt.m_root); - calculateNormalCone(root); - this->m_fdbvt.selfCollideT(root,docollide); - delete root; + if (psb->useSelfCollision()) + { + btSoftColliders::CollideFF_DD docollide; + docollide.mrg = getCollisionShape()->getMargin() + + psb->getCollisionShape()->getMargin(); + docollide.psb[0] = this; + docollide.psb[1] = psb; + /* psb0 faces vs psb0 faces */ + btDbvntNode* root = copyToDbvnt(this->m_fdbvt.m_root); + calculateNormalCone(root); + this->m_fdbvt.selfCollideT(root,docollide); + delete root; + } // btSoftColliders::CollideFF_DD docollide; // /* common */ diff --git a/src/BulletSoftBody/btSoftBody.h b/src/BulletSoftBody/btSoftBody.h index 1016fe9f6..8acf22a21 100644 --- a/src/BulletSoftBody/btSoftBody.h +++ b/src/BulletSoftBody/btSoftBody.h @@ -790,6 +790,7 @@ public: btAlignedObjectArray m_renderNodesInterpolationWeights; btAlignedObjectArray > m_renderNodesParents; + bool m_useSelfCollision; btAlignedObjectArray m_clusterConnectivity; //cluster connectivity, for self-collision @@ -1006,6 +1007,8 @@ public: /* defaultCollisionHandlers */ void defaultCollisionHandler(const btCollisionObjectWrapper* pcoWrap); void defaultCollisionHandler(btSoftBody* psb); + void setSelfCollision(bool useSelfCollision); + bool useSelfCollision(); // // Functionality to deal with new accelerated solvers.