Differential D1739 Diff 5899 extern/bullet2/src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp
Changeset View
Changeset View
Standalone View
Standalone View
extern/bullet2/src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp
| Show All 28 Lines | |||||
| #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h" | #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h" | ||||
| #include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h" | #include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h" | ||||
| #include "BulletCollision/BroadphaseCollision/btDbvt.h" | #include "BulletCollision/BroadphaseCollision/btDbvt.h" | ||||
| #include "LinearMath/btAabbUtil2.h" | #include "LinearMath/btAabbUtil2.h" | ||||
| #include "LinearMath/btQuickprof.h" | #include "LinearMath/btQuickprof.h" | ||||
| #include "LinearMath/btSerializer.h" | #include "LinearMath/btSerializer.h" | ||||
| #include "BulletCollision/CollisionShapes/btConvexPolyhedron.h" | #include "BulletCollision/CollisionShapes/btConvexPolyhedron.h" | ||||
| #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h" | #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h" | ||||
| #include "BulletCollision/Gimpact/btGImpactShape.h" | |||||
| //#define DISABLE_DBVT_COMPOUNDSHAPE_RAYCAST_ACCELERATION | //#define DISABLE_DBVT_COMPOUNDSHAPE_RAYCAST_ACCELERATION | ||||
| //#define USE_BRUTEFORCE_RAYBROADPHASE 1 | //#define USE_BRUTEFORCE_RAYBROADPHASE 1 | ||||
| //RECALCULATE_AABB is slower, but benefit is that you don't need to call 'stepSimulation' or 'updateAabbs' before using a rayTest | //RECALCULATE_AABB is slower, but benefit is that you don't need to call 'stepSimulation' or 'updateAabbs' before using a rayTest | ||||
| //#define RECALCULATE_AABB_RAYCAST 1 | //#define RECALCULATE_AABB_RAYCAST 1 | ||||
| //When the user doesn't provide dispatcher or broadphase, create basic versions (and delete them in destructor) | //When the user doesn't provide dispatcher or broadphase, create basic versions (and delete them in destructor) | ||||
| ▲ Show 20 Lines • Show All 241 Lines • ▼ Show 20 Lines | if (collisionShape->isConvex()) | ||||
| btConvexShape* convexShape = (btConvexShape*) collisionShape; | btConvexShape* convexShape = (btConvexShape*) collisionShape; | ||||
| btVoronoiSimplexSolver simplexSolver; | btVoronoiSimplexSolver simplexSolver; | ||||
| btSubsimplexConvexCast subSimplexConvexCaster(castShape,convexShape,&simplexSolver); | btSubsimplexConvexCast subSimplexConvexCaster(castShape,convexShape,&simplexSolver); | ||||
| btGjkConvexCast gjkConvexCaster(castShape,convexShape,&simplexSolver); | btGjkConvexCast gjkConvexCaster(castShape,convexShape,&simplexSolver); | ||||
| //btContinuousConvexCollision convexCaster(castShape,convexShape,&simplexSolver,0); | //btContinuousConvexCollision convexCaster(castShape,convexShape,&simplexSolver,0); | ||||
| bool condition = true; | |||||
| btConvexCast* convexCasterPtr = 0; | btConvexCast* convexCasterPtr = 0; | ||||
| if (resultCallback.m_flags & btTriangleRaycastCallback::kF_UseSubSimplexConvexCastRaytest) | //use kF_UseSubSimplexConvexCastRaytest by default | ||||
| convexCasterPtr = &subSimplexConvexCaster; | if (resultCallback.m_flags & btTriangleRaycastCallback::kF_UseGjkConvexCastRaytest) | ||||
| else | |||||
| convexCasterPtr = &gjkConvexCaster; | convexCasterPtr = &gjkConvexCaster; | ||||
| else | |||||
| convexCasterPtr = &subSimplexConvexCaster; | |||||
| btConvexCast& convexCaster = *convexCasterPtr; | btConvexCast& convexCaster = *convexCasterPtr; | ||||
| if (convexCaster.calcTimeOfImpact(rayFromTrans,rayToTrans,colObjWorldTransform,colObjWorldTransform,castResult)) | if (convexCaster.calcTimeOfImpact(rayFromTrans,rayToTrans,colObjWorldTransform,colObjWorldTransform,castResult)) | ||||
| { | { | ||||
| //add hit | //add hit | ||||
| if (castResult.m_normal.length2() > btScalar(0.0001)) | if (castResult.m_normal.length2() > btScalar(0.0001)) | ||||
| { | { | ||||
| if (castResult.m_fraction < resultCallback.m_closestHitFraction) | if (castResult.m_fraction < resultCallback.m_closestHitFraction) | ||||
| { | { | ||||
| //todo: figure out what this is about. When is rayFromTest.getBasis() not identity? | |||||
| #ifdef USE_SUBSIMPLEX_CONVEX_CAST | #ifdef USE_SUBSIMPLEX_CONVEX_CAST | ||||
| //rotate normal into worldspace | //rotate normal into worldspace | ||||
| castResult.m_normal = rayFromTrans.getBasis() * castResult.m_normal; | castResult.m_normal = rayFromTrans.getBasis() * castResult.m_normal; | ||||
| #endif //USE_SUBSIMPLEX_CONVEX_CAST | #endif //USE_SUBSIMPLEX_CONVEX_CAST | ||||
| castResult.m_normal.normalize(); | castResult.m_normal.normalize(); | ||||
| btCollisionWorld::LocalRayResult localRayResult | btCollisionWorld::LocalRayResult localRayResult | ||||
| ( | ( | ||||
| ▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | if (collisionShape->isConcave()) | ||||
| { | { | ||||
| ///optimized version for btBvhTriangleMeshShape | ///optimized version for btBvhTriangleMeshShape | ||||
| btBvhTriangleMeshShape* triangleMesh = (btBvhTriangleMeshShape*)collisionShape; | btBvhTriangleMeshShape* triangleMesh = (btBvhTriangleMeshShape*)collisionShape; | ||||
| BridgeTriangleRaycastCallback rcb(rayFromLocal,rayToLocal,&resultCallback,collisionObjectWrap->getCollisionObject(),triangleMesh,colObjWorldTransform); | BridgeTriangleRaycastCallback rcb(rayFromLocal,rayToLocal,&resultCallback,collisionObjectWrap->getCollisionObject(),triangleMesh,colObjWorldTransform); | ||||
| rcb.m_hitFraction = resultCallback.m_closestHitFraction; | rcb.m_hitFraction = resultCallback.m_closestHitFraction; | ||||
| triangleMesh->performRaycast(&rcb,rayFromLocal,rayToLocal); | triangleMesh->performRaycast(&rcb,rayFromLocal,rayToLocal); | ||||
| } | } | ||||
| else if(collisionShape->getShapeType()==GIMPACT_SHAPE_PROXYTYPE) | else | ||||
| { | |||||
| btGImpactMeshShape* concaveShape = (btGImpactMeshShape*)collisionShape; | |||||
| BridgeTriangleRaycastCallback rcb(rayFromLocal,rayToLocal,&resultCallback,collisionObjectWrap->getCollisionObject(),concaveShape, colObjWorldTransform); | |||||
| rcb.m_hitFraction = resultCallback.m_closestHitFraction; | |||||
| concaveShape->processAllTrianglesRay(&rcb,rayFromLocal,rayToLocal); | |||||
| }else | |||||
| { | { | ||||
| //generic (slower) case | //generic (slower) case | ||||
| btConcaveShape* concaveShape = (btConcaveShape*)collisionShape; | btConcaveShape* concaveShape = (btConcaveShape*)collisionShape; | ||||
| btTransform worldTocollisionObject = colObjWorldTransform.inverse(); | btTransform worldTocollisionObject = colObjWorldTransform.inverse(); | ||||
| btVector3 rayFromLocal = worldTocollisionObject * rayFromTrans.getOrigin(); | btVector3 rayFromLocal = worldTocollisionObject * rayFromTrans.getOrigin(); | ||||
| btVector3 rayToLocal = worldTocollisionObject * rayToTrans.getOrigin(); | btVector3 rayToLocal = worldTocollisionObject * rayToTrans.getOrigin(); | ||||
| ▲ Show 20 Lines • Show All 840 Lines • ▼ Show 20 Lines | virtual void processTriangle(btVector3* triangle,int partId, int triangleIndex) | ||||
| m_debugDrawer->drawLine(wv2,wv0,m_color); | m_debugDrawer->drawLine(wv2,wv0,m_color); | ||||
| } | } | ||||
| }; | }; | ||||
| void btCollisionWorld::debugDrawObject(const btTransform& worldTransform, const btCollisionShape* shape, const btVector3& color) | void btCollisionWorld::debugDrawObject(const btTransform& worldTransform, const btCollisionShape* shape, const btVector3& color) | ||||
| { | { | ||||
| // Draw a small simplex at the center of the object | // Draw a small simplex at the center of the object | ||||
| if (getDebugDrawer() && getDebugDrawer()->getDebugMode() & btIDebugDraw::DBG_DrawFrames) | |||||
| { | |||||
| getDebugDrawer()->drawTransform(worldTransform,1); | getDebugDrawer()->drawTransform(worldTransform,1); | ||||
| } | |||||
| if (shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) | if (shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) | ||||
| { | { | ||||
| const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(shape); | const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(shape); | ||||
| for (int i=compoundShape->getNumChildShapes()-1;i>=0;i--) | for (int i=compoundShape->getNumChildShapes()-1;i>=0;i--) | ||||
| { | { | ||||
| btTransform childTrans = compoundShape->getChildTransform(i); | btTransform childTrans = compoundShape->getChildTransform(i); | ||||
| const btCollisionShape* colShape = compoundShape->getChildShape(i); | const btCollisionShape* colShape = compoundShape->getChildShape(i); | ||||
| ▲ Show 20 Lines • Show All 161 Lines • ▼ Show 20 Lines | if (shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| void btCollisionWorld::debugDrawWorld() | void btCollisionWorld::debugDrawWorld() | ||||
| { | { | ||||
| if (getDebugDrawer() && getDebugDrawer()->getDebugMode() & btIDebugDraw::DBG_DrawContactPoints) | if (getDebugDrawer()) | ||||
| { | |||||
| btIDebugDraw::DefaultColors defaultColors = getDebugDrawer()->getDefaultColors(); | |||||
| if ( getDebugDrawer()->getDebugMode() & btIDebugDraw::DBG_DrawContactPoints) | |||||
| { | |||||
| if (getDispatcher()) | |||||
| { | { | ||||
| int numManifolds = getDispatcher()->getNumManifolds(); | int numManifolds = getDispatcher()->getNumManifolds(); | ||||
| btVector3 color(1,1,0); | |||||
| for (int i=0;i<numManifolds;i++) | for (int i=0;i<numManifolds;i++) | ||||
| { | { | ||||
| btPersistentManifold* contactManifold = getDispatcher()->getManifoldByIndexInternal(i); | btPersistentManifold* contactManifold = getDispatcher()->getManifoldByIndexInternal(i); | ||||
| //btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0()); | //btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0()); | ||||
| //btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1()); | //btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1()); | ||||
| int numContacts = contactManifold->getNumContacts(); | int numContacts = contactManifold->getNumContacts(); | ||||
| for (int j=0;j<numContacts;j++) | for (int j=0;j<numContacts;j++) | ||||
| { | { | ||||
| btManifoldPoint& cp = contactManifold->getContactPoint(j); | btManifoldPoint& cp = contactManifold->getContactPoint(j); | ||||
| getDebugDrawer()->drawContactPoint(cp.m_positionWorldOnB,cp.m_normalWorldOnB,cp.getDistance(),cp.getLifeTime(),color); | getDebugDrawer()->drawContactPoint(cp.m_positionWorldOnB,cp.m_normalWorldOnB,cp.getDistance(),cp.getLifeTime(),defaultColors.m_contactPoint); | ||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| if (getDebugDrawer() && (getDebugDrawer()->getDebugMode() & (btIDebugDraw::DBG_DrawWireframe | btIDebugDraw::DBG_DrawAabb))) | if ((getDebugDrawer()->getDebugMode() & (btIDebugDraw::DBG_DrawWireframe | btIDebugDraw::DBG_DrawAabb))) | ||||
| { | { | ||||
| int i; | int i; | ||||
| for ( i=0;i<m_collisionObjects.size();i++) | for ( i=0;i<m_collisionObjects.size();i++) | ||||
| { | { | ||||
| btCollisionObject* colObj = m_collisionObjects[i]; | btCollisionObject* colObj = m_collisionObjects[i]; | ||||
| if ((colObj->getCollisionFlags() & btCollisionObject::CF_DISABLE_VISUALIZE_OBJECT)==0) | if ((colObj->getCollisionFlags() & btCollisionObject::CF_DISABLE_VISUALIZE_OBJECT)==0) | ||||
| { | { | ||||
| if (getDebugDrawer() && (getDebugDrawer()->getDebugMode() & btIDebugDraw::DBG_DrawWireframe)) | if (getDebugDrawer() && (getDebugDrawer()->getDebugMode() & btIDebugDraw::DBG_DrawWireframe)) | ||||
| { | { | ||||
| btVector3 color(btScalar(1.),btScalar(1.),btScalar(1.)); | btVector3 color(btScalar(0.4),btScalar(0.4),btScalar(0.4)); | ||||
| switch(colObj->getActivationState()) | switch(colObj->getActivationState()) | ||||
| { | { | ||||
| case ACTIVE_TAG: | case ACTIVE_TAG: | ||||
| color = btVector3(btScalar(1.),btScalar(1.),btScalar(1.)); break; | color = defaultColors.m_activeObject; break; | ||||
| case ISLAND_SLEEPING: | case ISLAND_SLEEPING: | ||||
| color = btVector3(btScalar(0.),btScalar(1.),btScalar(0.));break; | color = defaultColors.m_deactivatedObject;break; | ||||
| case WANTS_DEACTIVATION: | case WANTS_DEACTIVATION: | ||||
| color = btVector3(btScalar(0.),btScalar(1.),btScalar(1.));break; | color = defaultColors.m_wantsDeactivationObject;break; | ||||
| case DISABLE_DEACTIVATION: | case DISABLE_DEACTIVATION: | ||||
| color = btVector3(btScalar(1.),btScalar(0.),btScalar(0.));break; | color = defaultColors.m_disabledDeactivationObject;break; | ||||
| case DISABLE_SIMULATION: | case DISABLE_SIMULATION: | ||||
| color = btVector3(btScalar(1.),btScalar(1.),btScalar(0.));break; | color = defaultColors.m_disabledSimulationObject;break; | ||||
| default: | default: | ||||
| { | { | ||||
| color = btVector3(btScalar(1),btScalar(0.),btScalar(0.)); | color = btVector3(btScalar(.3),btScalar(0.3),btScalar(0.3)); | ||||
| } | } | ||||
| }; | }; | ||||
| debugDrawObject(colObj->getWorldTransform(),colObj->getCollisionShape(),color); | debugDrawObject(colObj->getWorldTransform(),colObj->getCollisionShape(),color); | ||||
| } | } | ||||
| if (m_debugDrawer && (m_debugDrawer->getDebugMode() & btIDebugDraw::DBG_DrawAabb)) | if (m_debugDrawer && (m_debugDrawer->getDebugMode() & btIDebugDraw::DBG_DrawAabb)) | ||||
| { | { | ||||
| btVector3 minAabb,maxAabb; | btVector3 minAabb,maxAabb; | ||||
| btVector3 colorvec(1,0,0); | btVector3 colorvec = defaultColors.m_aabb; | ||||
| colObj->getCollisionShape()->getAabb(colObj->getWorldTransform(), minAabb,maxAabb); | colObj->getCollisionShape()->getAabb(colObj->getWorldTransform(), minAabb,maxAabb); | ||||
| btVector3 contactThreshold(gContactBreakingThreshold,gContactBreakingThreshold,gContactBreakingThreshold); | btVector3 contactThreshold(gContactBreakingThreshold,gContactBreakingThreshold,gContactBreakingThreshold); | ||||
| minAabb -= contactThreshold; | minAabb -= contactThreshold; | ||||
| maxAabb += contactThreshold; | maxAabb += contactThreshold; | ||||
| btVector3 minAabb2,maxAabb2; | btVector3 minAabb2,maxAabb2; | ||||
| if(getDispatchInfo().m_useContinuous && colObj->getInternalType()==btCollisionObject::CO_RIGID_BODY && !colObj->isStaticOrKinematicObject()) | if(getDispatchInfo().m_useContinuous && colObj->getInternalType()==btCollisionObject::CO_RIGID_BODY && !colObj->isStaticOrKinematicObject()) | ||||
| { | { | ||||
| colObj->getCollisionShape()->getAabb(colObj->getInterpolationWorldTransform(),minAabb2,maxAabb2); | colObj->getCollisionShape()->getAabb(colObj->getInterpolationWorldTransform(),minAabb2,maxAabb2); | ||||
| minAabb2 -= contactThreshold; | minAabb2 -= contactThreshold; | ||||
| maxAabb2 += contactThreshold; | maxAabb2 += contactThreshold; | ||||
| minAabb.setMin(minAabb2); | minAabb.setMin(minAabb2); | ||||
| maxAabb.setMax(maxAabb2); | maxAabb.setMax(maxAabb2); | ||||
| } | } | ||||
| m_debugDrawer->drawAabb(minAabb,maxAabb,colorvec); | m_debugDrawer->drawAabb(minAabb,maxAabb,colorvec); | ||||
| } | } | ||||
| } | } | ||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| void btCollisionWorld::serializeCollisionObjects(btSerializer* serializer) | void btCollisionWorld::serializeCollisionObjects(btSerializer* serializer) | ||||
| { | { | ||||
| int i; | int i; | ||||
| //serialize all collision objects | |||||
| for (i=0;i<m_collisionObjects.size();i++) | |||||
| { | |||||
| btCollisionObject* colObj = m_collisionObjects[i]; | |||||
| if (colObj->getInternalType() == btCollisionObject::CO_COLLISION_OBJECT) | |||||
| { | |||||
| colObj->serializeSingleObject(serializer); | |||||
| } | |||||
| } | |||||
| ///keep track of shapes already serialized | ///keep track of shapes already serialized | ||||
| btHashMap<btHashPtr,btCollisionShape*> serializedShapes; | btHashMap<btHashPtr,btCollisionShape*> serializedShapes; | ||||
| for (i=0;i<m_collisionObjects.size();i++) | for (i=0;i<m_collisionObjects.size();i++) | ||||
| { | { | ||||
| btCollisionObject* colObj = m_collisionObjects[i]; | btCollisionObject* colObj = m_collisionObjects[i]; | ||||
| btCollisionShape* shape = colObj->getCollisionShape(); | btCollisionShape* shape = colObj->getCollisionShape(); | ||||
| if (!serializedShapes.find(shape)) | if (!serializedShapes.find(shape)) | ||||
| { | { | ||||
| serializedShapes.insert(shape,shape); | serializedShapes.insert(shape,shape); | ||||
| shape->serializeSingleShape(serializer); | shape->serializeSingleShape(serializer); | ||||
| } | } | ||||
| } | } | ||||
| //serialize all collision objects | |||||
| for (i=0;i<m_collisionObjects.size();i++) | |||||
| { | |||||
| btCollisionObject* colObj = m_collisionObjects[i]; | |||||
| if ((colObj->getInternalType() == btCollisionObject::CO_COLLISION_OBJECT) || (colObj->getInternalType() == btCollisionObject::CO_FEATHERSTONE_LINK)) | |||||
| { | |||||
| colObj->serializeSingleObject(serializer); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| void btCollisionWorld::serialize(btSerializer* serializer) | void btCollisionWorld::serialize(btSerializer* serializer) | ||||
| { | { | ||||
| serializer->startSerialization(); | serializer->startSerialization(); | ||||
| serializeCollisionObjects(serializer); | serializeCollisionObjects(serializer); | ||||
| serializer->finishSerialization(); | serializer->finishSerialization(); | ||||
| } | } | ||||