Skip to content

Commit 7151865

Browse files
committed
Introduce kF_UseGjkConvexCastRaytest, and make kF_UseSubSimplexConvexCastRaytest the default for btCollisionWorld::rayTest See #34
Add btCollisionObject::setIgnoreCollisionCheck to disable collisions between specific instances, without having a btTypedConstraint. See #165 Make btMultiBody and btMultiBodyJointMotor backwards compatible with Bullet 2.82 API (single-DOF API)
1 parent 907ac49 commit 7151865

File tree

10 files changed

+86
-48
lines changed

10 files changed

+86
-48
lines changed

ObsoleteDemos/FeatherstoneMultiBodyDemo/FeatherstoneMultiBodyDemo.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -394,23 +394,16 @@ btMultiBody* FeatherstoneMultiBodyDemo::createFeatherstoneMultiBody(class btMult
394394
if (1)
395395
{
396396
btCollisionShape* box = new btBoxShape(btVector3(halfExtents[0],halfExtents[1],halfExtents[2])*scaling);
397-
btRigidBody* body = new btRigidBody(mass,0,box,inertia);
398397
btMultiBodyLinkCollider* col= new btMultiBodyLinkCollider(bod,-1);
399-
400-
body->setCollisionShape(box);
401398
col->setCollisionShape(box);
402-
403399
btTransform tr;
404400
tr.setIdentity();
405401
tr.setOrigin(local_origin[0]);
406402
tr.setRotation(btQuaternion(quat[0],quat[1],quat[2],quat[3]));
407-
body->setWorldTransform(tr);
408403
col->setWorldTransform(tr);
409-
410404
world->addCollisionObject(col, 2,1+2);
411405
col->setFriction(friction);
412-
bod->setBaseCollider(col);
413-
406+
bod->setBaseCollider(col);
414407
}
415408
}
416409

src/BulletCollision/CollisionDispatch/btCollisionDispatcher.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ bool btCollisionDispatcher::needsCollision(const btCollisionObject* body0,const
189189

190190
if ((!body0->isActive()) && (!body1->isActive()))
191191
needsCollision = false;
192-
else if (!body0->checkCollideWith(body1))
192+
else if ((!body0->checkCollideWith(body1)) || (!body1->checkCollideWith(body0)))
193193
needsCollision = false;
194194

195195
return needsCollision ;

src/BulletCollision/CollisionDispatch/btCollisionObject.h

+30-5
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ ATTRIBUTE_ALIGNED16(class) btCollisionObject
110110
/// If some object should have elaborate collision filtering by sub-classes
111111
int m_checkCollideWith;
112112

113+
btAlignedObjectArray<const btCollisionObject*> m_objectsWithoutCollisionCheck;
114+
113115
///internal update revision number. It will be increased when the object changes. This allows some subsystems to perform lazy evaluation.
114116
int m_updateRevision;
115117

116-
virtual bool checkCollideWithOverride(const btCollisionObject* /* co */) const
117-
{
118-
return true;
119-
}
120118

121119
public:
122120

@@ -225,7 +223,34 @@ ATTRIBUTE_ALIGNED16(class) btCollisionObject
225223
return m_collisionShape;
226224
}
227225

228-
226+
void setIgnoreCollisionCheck(const btCollisionObject* co, bool ignoreCollisionCheck)
227+
{
228+
if (ignoreCollisionCheck)
229+
{
230+
//We don't check for duplicates. Is it ok to leave that up to the user of this API?
231+
//int index = m_objectsWithoutCollisionCheck.findLinearSearch(co);
232+
//if (index == m_objectsWithoutCollisionCheck.size())
233+
//{
234+
m_objectsWithoutCollisionCheck.push_back(co);
235+
//}
236+
}
237+
else
238+
{
239+
m_objectsWithoutCollisionCheck.remove(co);
240+
}
241+
m_checkCollideWith = m_objectsWithoutCollisionCheck.size() > 0;
242+
}
243+
244+
virtual bool checkCollideWithOverride(const btCollisionObject* co) const
245+
{
246+
int index = m_objectsWithoutCollisionCheck.findLinearSearch(co);
247+
if (index < m_objectsWithoutCollisionCheck.size())
248+
{
249+
return false;
250+
}
251+
return true;
252+
}
253+
229254

230255

231256

src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,11 @@ void btCollisionWorld::rayTestSingleInternal(const btTransform& rayFromTrans,con
294294
//btContinuousConvexCollision convexCaster(castShape,convexShape,&simplexSolver,0);
295295
bool condition = true;
296296
btConvexCast* convexCasterPtr = 0;
297-
if (resultCallback.m_flags & btTriangleRaycastCallback::kF_UseSubSimplexConvexCastRaytest)
298-
convexCasterPtr = &subSimplexConvexCaster;
299-
else
297+
//use kF_UseSubSimplexConvexCastRaytest by default
298+
if (resultCallback.m_flags & btTriangleRaycastCallback::kF_UseGjkConvexCastRaytest)
300299
convexCasterPtr = &gjkConvexCaster;
300+
else
301+
convexCasterPtr = &subSimplexConvexCaster;
301302

302303
btConvexCast& convexCaster = *convexCasterPtr;
303304

src/BulletCollision/NarrowPhaseCollision/btRaycastCallback.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ subject to the following restrictions:
2121
struct btBroadphaseProxy;
2222
class btConvexShape;
2323

24+
2425
class btTriangleRaycastCallback: public btTriangleCallback
2526
{
2627
public:
@@ -32,10 +33,12 @@ class btTriangleRaycastCallback: public btTriangleCallback
3233
//@BP Mod - allow backface filtering and unflipped normals
3334
enum EFlags
3435
{
35-
kF_None = 0,
36+
kF_None = 0,
3637
kF_FilterBackfaces = 1 << 0,
3738
kF_KeepUnflippedNormal = 1 << 1, // Prevents returned face normal getting flipped when a ray hits a back-facing triangle
38-
kF_UseSubSimplexConvexCastRaytest = 1 << 2, // Uses an approximate but faster ray versus convex intersection algorithm
39+
///SubSimplexConvexCastRaytest is the default, even if kF_None is set.
40+
kF_UseSubSimplexConvexCastRaytest = 1 << 2, // Uses an approximate but faster ray versus convex intersection algorithm
41+
kF_UseGjkConvexCastRaytest = 1 << 3,
3942
kF_Terminator = 0xFFFFFFFF
4043
};
4144
unsigned int m_flags;

src/BulletDynamics/Dynamics/btRigidBody.cpp

+30-20
Original file line numberDiff line numberDiff line change
@@ -317,38 +317,48 @@ void btRigidBody::setCenterOfMassTransform(const btTransform& xform)
317317
}
318318

319319

320-
bool btRigidBody::checkCollideWithOverride(const btCollisionObject* co) const
321-
{
322-
const btRigidBody* otherRb = btRigidBody::upcast(co);
323-
if (!otherRb)
324-
return true;
325-
326-
for (int i = 0; i < m_constraintRefs.size(); ++i)
327-
{
328-
const btTypedConstraint* c = m_constraintRefs[i];
329-
if (c->isEnabled())
330-
if (&c->getRigidBodyA() == otherRb || &c->getRigidBodyB() == otherRb)
331-
return false;
332-
}
333-
334-
return true;
335-
}
336320

337321

338322

339323
void btRigidBody::addConstraintRef(btTypedConstraint* c)
340324
{
325+
///disable collision with the 'other' body
326+
341327
int index = m_constraintRefs.findLinearSearch(c);
328+
//don't add constraints that are already referenced
329+
btAssert(index == m_constraintRefs.size());
342330
if (index == m_constraintRefs.size())
343-
m_constraintRefs.push_back(c);
344-
345-
m_checkCollideWith = true;
331+
{
332+
m_constraintRefs.push_back(c);
333+
btCollisionObject* colObjA = &c->getRigidBodyA();
334+
btCollisionObject* colObjB = &c->getRigidBodyB();
335+
if (colObjA == this)
336+
{
337+
colObjA->setIgnoreCollisionCheck(colObjB, true);
338+
}
339+
else
340+
{
341+
colObjB->setIgnoreCollisionCheck(colObjA, true);
342+
}
343+
}
346344
}
347345

348346
void btRigidBody::removeConstraintRef(btTypedConstraint* c)
349347
{
348+
int index = m_constraintRefs.findLinearSearch(c);
349+
//don't remove constraints that are not referenced
350+
btAssert(index < m_constraintRefs.size());
350351
m_constraintRefs.remove(c);
351-
m_checkCollideWith = m_constraintRefs.size() > 0;
352+
btCollisionObject* colObjA = &c->getRigidBodyA();
353+
btCollisionObject* colObjB = &c->getRigidBodyB();
354+
if (colObjA == this)
355+
{
356+
colObjA->setIgnoreCollisionCheck(colObjB, false);
357+
}
358+
else
359+
{
360+
colObjB->setIgnoreCollisionCheck(colObjA, false);
361+
}
352362
}
353363

354364
int btRigidBody::calculateSerializeBufferSize() const

src/BulletDynamics/Dynamics/btRigidBody.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class btRigidBody : public btCollisionObject
8787
//m_optionalMotionState allows to automatic synchronize the world transform for active objects
8888
btMotionState* m_optionalMotionState;
8989

90-
//keep track of typed constraints referencing this rigid body
90+
//keep track of typed constraints referencing this rigid body, to disable collision between linked bodies
9191
btAlignedObjectArray<btTypedConstraint*> m_constraintRefs;
9292

9393
int m_rigidbodyFlags;
@@ -506,8 +506,6 @@ class btRigidBody : public btCollisionObject
506506
return (getBroadphaseProxy() != 0);
507507
}
508508

509-
virtual bool checkCollideWithOverride(const btCollisionObject* co) const;
510-
511509
void addConstraintRef(btTypedConstraint* c);
512510
void removeConstraintRef(btTypedConstraint* c);
513511

src/BulletDynamics/Featherstone/btMultiBody.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ class btMultiBody
4545
// initialization
4646
//
4747

48-
btMultiBody(int n_links, // NOT including the base
49-
btScalar mass, // mass of base
50-
const btVector3 &inertia, // inertia of base, in base frame; assumed diagonal
51-
bool fixedBase, // whether the base is fixed (true) or can move (false)
52-
bool canSleep,
53-
bool multiDof
48+
btMultiBody(int n_links, // NOT including the base
49+
btScalar mass, // mass of base
50+
const btVector3 &inertia, // inertia of base, in base frame; assumed diagonal
51+
bool fixedBase, // whether the base is fixed (true) or can move (false)
52+
bool canSleep,
53+
bool multiDof = false
5454
);
5555

56+
5657
~btMultiBody();
5758

5859
void setupPrismatic(int i, // 0 to num_links-1

src/BulletDynamics/Featherstone/btMultiBodyJointMotor.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ subject to the following restrictions:
2121
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
2222

2323

24+
btMultiBodyJointMotor::btMultiBodyJointMotor(btMultiBody* body, int link, btScalar desiredVelocity, btScalar maxMotorImpulse)
25+
:btMultiBodyJointMotor(body,link,0,desiredVelocity,maxMotorImpulse)
26+
{
27+
}
28+
29+
2430
btMultiBodyJointMotor::btMultiBodyJointMotor(btMultiBody* body, int link, int linkDoF, btScalar desiredVelocity, btScalar maxMotorImpulse)
2531
//:btMultiBodyConstraint(body,0,link,-1,1,true),
2632
:btMultiBodyConstraint(body,body,link,link,1,true),

src/BulletDynamics/Featherstone/btMultiBodyJointMotor.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class btMultiBodyJointMotor : public btMultiBodyConstraint
3030

3131
public:
3232

33+
btMultiBodyJointMotor(btMultiBody* body, int link,btScalar desiredVelocity, btScalar maxMotorImpulse);
3334
btMultiBodyJointMotor(btMultiBody* body, int link, int linkDoF, btScalar desiredVelocity, btScalar maxMotorImpulse);
3435
virtual ~btMultiBodyJointMotor();
3536

0 commit comments

Comments
 (0)