diff --git a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp index 97fe210b3..9f8e98d43 100644 --- a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp +++ b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp @@ -440,6 +440,9 @@ int maxNumOutstandingTasks = 4; btDiscreteDynamicsWorld* world = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); m_dynamicsWorld = world; + ///SOLVER_RANDMIZE_ORDER makes cylinder stacking a bit more stable + world->getSolverInfo().m_solverMode |= SOLVER_RANDMIZE_ORDER; + #ifdef USER_DEFINED_FRICTION_MODEL //user defined friction model is not supported in 'cache friendly' solver yet, so switch to old solver diff --git a/Demos/Gpu2dDemo/BasicDemo.cpp b/Demos/Gpu2dDemo/BasicDemo.cpp index 2b7d7b1cb..9672ae814 100644 --- a/Demos/Gpu2dDemo/BasicDemo.cpp +++ b/Demos/Gpu2dDemo/BasicDemo.cpp @@ -32,8 +32,8 @@ subject to the following restrictions: #define OECAKE_LOADER 1 #ifdef _DEBUG - #define LARGE_DEMO 0 -// #define LARGE_DEMO 1 +// #define LARGE_DEMO 0 + #define LARGE_DEMO 1 #else #define LARGE_DEMO 1 #endif @@ -323,10 +323,8 @@ void BasicDemo::initPhysics() // Re-using the same collision is better for memory usage and performance - //btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,0.1));//SCALING*1)); -// btCollisionShape* colShape = new btBox2dShape(btVector3(SCALING*.7,SCALING*.7,0.1));//SCALING*1)); -#if 0 +#if 1 #define SPRADIUS btScalar(SCALING*0.1f) #define SPRPOS btScalar(SCALING*0.05f) static btVector3 sSphPos[8]; diff --git a/Demos/Gpu2dDemo/BasicDemo.h b/Demos/Gpu2dDemo/BasicDemo.h index 642a36e05..eee848240 100644 --- a/Demos/Gpu2dDemo/BasicDemo.h +++ b/Demos/Gpu2dDemo/BasicDemo.h @@ -66,6 +66,9 @@ class BasicDemo : public GlutDemoApplication void exitPhysics(); + ///don't shoot a box in this demo ;-) + virtual void shootBox(const btVector3& dest) {} + virtual void clientMoveAndDisplay(); virtual void displayCallback(); diff --git a/Demos/OpenGL/DemoApplication.h b/Demos/OpenGL/DemoApplication.h index da50bc8f0..595934a27 100644 --- a/Demos/OpenGL/DemoApplication.h +++ b/Demos/OpenGL/DemoApplication.h @@ -187,7 +187,7 @@ public: ///Demo functions virtual void setShootBoxShape (); - void shootBox(const btVector3& destination); + virtual void shootBox(const btVector3& destination); btVector3 getRayTo(int x,int y); diff --git a/Demos/OpenGL/GLDebugFont.cpp b/Demos/OpenGL/GLDebugFont.cpp index b90eabc09..d1c8c60f1 100644 --- a/Demos/OpenGL/GLDebugFont.cpp +++ b/Demos/OpenGL/GLDebugFont.cpp @@ -20,13 +20,16 @@ subject to the following restrictions: extern unsigned char sFontData[]; static GLuint sTexture = -1; -static int sScreenWidth = 0; -static int sScreenHeight = 0; +static int sScreenWidth = -1; +static int sScreenHeight = -1; void GLDebugResetFont(int screenWidth,int screenHeight) { + if ((sScreenWidth == screenWidth) && (sScreenHeight == screenHeight)) + return; + sScreenWidth = screenWidth; sScreenHeight = screenHeight; diff --git a/Demos/OpenGL/GL_ShapeDrawer.cpp b/Demos/OpenGL/GL_ShapeDrawer.cpp index e90cca0dc..ed32ad987 100644 --- a/Demos/OpenGL/GL_ShapeDrawer.cpp +++ b/Demos/OpenGL/GL_ShapeDrawer.cpp @@ -34,6 +34,9 @@ subject to the following restrictions: #include "BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h" #include "BulletCollision/CollisionShapes/btUniformScalingShape.h" #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h" +#include "BulletCollision/CollisionShapes/btMultiSphereShape.h" + + /// #include "BulletCollision/CollisionShapes/btShapeHull.h" @@ -269,6 +272,34 @@ public: } }; + +void GL_ShapeDrawer::drawSphere(btScalar r, int lats, int longs) +{ + int i, j; + for(i = 0; i <= lats; i++) { + btScalar lat0 = SIMD_PI * (-btScalar(0.5) + (btScalar) (i - 1) / lats); + btScalar z0 = sin(lat0); + btScalar zr0 = cos(lat0); + + btScalar lat1 = SIMD_PI * (-btScalar(0.5) + (btScalar) i / lats); + btScalar z1 = sin(lat1); + btScalar zr1 = cos(lat1); + + glBegin(GL_QUAD_STRIP); + for(j = 0; j <= longs; j++) { + btScalar lng = 2 * SIMD_PI * (btScalar) (j - 1) / longs; + btScalar x = cos(lng); + btScalar y = sin(lng); + + glNormal3f(x * zr0, y * zr0, z0); + glVertex3f(x * zr0, y * zr0, z0); + glNormal3f(x * zr1, y * zr1, z1); + glVertex3f(x * zr1, y * zr1, z1); + } + glEnd(); + } +} + void GL_ShapeDrawer::drawCylinder(float radius,float halfHeight, int upAxis) { @@ -538,30 +569,66 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons ///you can comment out any of the specific cases, and use the default ///the benefit of 'default' is that it approximates the actual collision shape including collision margin - int shapetype=m_textureenabled?MAX_BROADPHASE_COLLISION_TYPES:shape->getShapeType(); + //int shapetype=m_textureenabled?MAX_BROADPHASE_COLLISION_TYPES:shape->getShapeType(); + int shapetype=shape->getShapeType(); switch (shapetype) { -#if 0 - case BOX_SHAPE_PROXYTYPE: - { - const btBoxShape* boxShape = static_cast(shape); - btVector3 halfExtent = boxShape->getHalfExtentsWithMargin(); - glScaled(2*halfExtent[0], 2*halfExtent[1], 2*halfExtent[2]); - glutSolidCube(1.0); - useWireframeFallback = false; - break; - } - - case SPHERE_SHAPE_PROXYTYPE: + case SPHERE_SHAPE_PROXYTYPE: { const btSphereShape* sphereShape = static_cast(shape); float radius = sphereShape->getMargin();//radius doesn't include the margin, so draw with margin - glutSolidSphere(radius,10,10); + drawSphere(radius,10,10); useWireframeFallback = false; break; } + case BOX_SHAPE_PROXYTYPE: + { + const btBoxShape* boxShape = static_cast(shape); + btVector3 halfExtent = boxShape->getHalfExtentsWithMargin(); + + static int indices[36] = { + 0,1,2, + 3,2,1, + 4,0,6, + 6,0,2, + 5,1,4, + 4,1,0, + 7,3,1, + 7,1,5, + 5,4,7, + 7,4,6, + 7,2,3, + 7,6,2}; + + static btVector3 vertices[8]={ btVector3(1,1,1),btVector3(-1,1,1), btVector3(1,-1,1), btVector3(-1,-1,1), btVector3(1,1,-1), btVector3(-1,1,-1), btVector3(1,-1,-1), btVector3(-1,-1,-1)}; + glBegin (GL_TRIANGLES); + int si=36; + for (int i=0;i(shape); @@ -630,6 +697,26 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons break; } + case MULTI_SPHERE_SHAPE_PROXYTYPE: + { + const btMultiSphereShape* multiSphereShape = static_cast(shape); + + btTransform childTransform; + childTransform.setIdentity(); + + + for (int i = multiSphereShape->getSphereCount()-1; i>=0;i--) + { + btSphereShape sc(multiSphereShape->getSphereRadius(i)); + childTransform.setOrigin(multiSphereShape->getSpherePosition(i)); + btScalar childMat[16]; + childTransform.getOpenGLMatrix(childMat); + drawOpenGL(childMat,&sc,color,debugMode,worldBoundsMin,worldBoundsMax); + } + + break; + } + default: { diff --git a/Demos/OpenGL/GL_ShapeDrawer.h b/Demos/OpenGL/GL_ShapeDrawer.h index 6b7b3e222..c61507a6a 100644 --- a/Demos/OpenGL/GL_ShapeDrawer.h +++ b/Demos/OpenGL/GL_ShapeDrawer.h @@ -58,6 +58,7 @@ public: } static void drawCylinder(float radius,float halfHeight, int upAxis); + void drawSphere(btScalar r, int lats, int longs); static void drawCoordSystem(); }; diff --git a/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.cpp b/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.cpp index f231717af..1a349a30b 100644 --- a/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.cpp +++ b/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.cpp @@ -1,6 +1,6 @@ /* Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2007 Erwin Coumans http://continuousphysics.com/Bullet/ +Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org 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. @@ -12,6 +12,7 @@ subject to the following restrictions: 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. */ + ///btDbvtBroadphase implementation by Nathanael Presson #include "btDbvtBroadphase.h" @@ -123,7 +124,7 @@ btDbvtBroadphase::btDbvtBroadphase(btOverlappingPairCache* paircache) m_deferedcollide = false; m_needcleanup = true; m_releasepaircache = (paircache!=0)?false:true; - m_prediction = 1/(btScalar)2; + m_prediction = 0; m_stageCurrent = 0; m_fixedleft = 0; m_fupdates = 1; @@ -249,6 +250,7 @@ void btDbvtBroadphase::rayTest(const btVector3& rayFrom,const btVector3& rayTo, } + // void btDbvtBroadphase::setAabb( btBroadphaseProxy* absproxy, const btVector3& aabbMin, @@ -571,7 +573,6 @@ void btDbvtBroadphase::resetPool(btDispatcher* dispatcher) m_deferedcollide = false; m_needcleanup = true; - m_prediction = 1/(btScalar)2; m_stageCurrent = 0; m_fixedleft = 0; m_fupdates = 1; diff --git a/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.h b/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.h index fe70bc39c..5615535c2 100644 --- a/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.h +++ b/src/BulletCollision/BroadphaseCollision/btDbvtBroadphase.h @@ -1,6 +1,6 @@ /* Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2007 Erwin Coumans http://continuousphysics.com/Bullet/ +Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org 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. @@ -12,6 +12,7 @@ subject to the following restrictions: 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. */ + ///btDbvtBroadphase implementation by Nathanael Presson #ifndef BT_DBVT_BROADPHASE_H #define BT_DBVT_BROADPHASE_H @@ -115,7 +116,15 @@ struct btDbvtBroadphase : btBroadphaseInterface void printStats(); static void benchmark(btBroadphaseInterface*); - + void setVelocityPrediction(btScalar prediction) + { + m_prediction = prediction; + } + btScalar getVelocityPrediction() const + { + return m_prediction; + } + void performDeferredRemoval(btDispatcher* dispatcher); ///reset broadphase internal structures, to ensure determinism/reproducability diff --git a/src/BulletCollision/CollisionShapes/btConvexHullShape.cpp b/src/BulletCollision/CollisionShapes/btConvexHullShape.cpp index 8a9c9cb53..d912e1c04 100644 --- a/src/BulletCollision/CollisionShapes/btConvexHullShape.cpp +++ b/src/BulletCollision/CollisionShapes/btConvexHullShape.cpp @@ -19,7 +19,7 @@ subject to the following restrictions: #include "LinearMath/btQuaternion.h" -btConvexHullShape ::btConvexHullShape (const btScalar* points,int numPoints,int stride) : btPolyhedralConvexShape () +btConvexHullShape ::btConvexHullShape (const btScalar* points,int numPoints,int stride) : btPolyhedralConvexAabbCachingShape () { m_shapeType = CONVEX_HULL_SHAPE_PROXYTYPE; m_unscaledPoints.resize(numPoints); diff --git a/src/BulletCollision/CollisionShapes/btConvexHullShape.h b/src/BulletCollision/CollisionShapes/btConvexHullShape.h index e42b5f321..2b0494d19 100644 --- a/src/BulletCollision/CollisionShapes/btConvexHullShape.h +++ b/src/BulletCollision/CollisionShapes/btConvexHullShape.h @@ -23,7 +23,7 @@ subject to the following restrictions: ///The btConvexHullShape implements an implicit convex hull of an array of vertices. ///Bullet provides a general and fast collision detector for convex shapes based on GJK and EPA using localGetSupportingVertex. -ATTRIBUTE_ALIGNED16(class) btConvexHullShape : public btPolyhedralConvexShape +ATTRIBUTE_ALIGNED16(class) btConvexHullShape : public btPolyhedralConvexAabbCachingShape { btAlignedObjectArray m_unscaledPoints; diff --git a/src/BulletCollision/CollisionShapes/btConvexInternalShape.cpp b/src/BulletCollision/CollisionShapes/btConvexInternalShape.cpp index 11fad2d05..2bc17e61d 100644 --- a/src/BulletCollision/CollisionShapes/btConvexInternalShape.cpp +++ b/src/BulletCollision/CollisionShapes/btConvexInternalShape.cpp @@ -35,6 +35,7 @@ void btConvexInternalShape::setLocalScaling(const btVector3& scaling) void btConvexInternalShape::getAabbSlow(const btTransform& trans,btVector3&minAabb,btVector3&maxAabb) const { + //use localGetSupportingVertexWithoutMargin? btScalar margin = getMargin(); for (int i=0;i<3;i++) { @@ -79,3 +80,71 @@ btVector3 btConvexInternalShape::localGetSupportingVertex(const btVector3& vec)c } +btConvexInternalAabbCachingShape::btConvexInternalAabbCachingShape() + : btConvexInternalShape(), +m_localAabbMin(1,1,1), +m_localAabbMax(-1,-1,-1), +m_isLocalAabbValid(false) +{ +} + + +void btConvexInternalAabbCachingShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const +{ + getNonvirtualAabb(trans,aabbMin,aabbMax,getMargin()); +} + +void btConvexInternalAabbCachingShape::setLocalScaling(const btVector3& scaling) +{ + btConvexInternalShape::setLocalScaling(scaling); + recalcLocalAabb(); +} + + +void btConvexInternalAabbCachingShape::recalcLocalAabb() +{ + m_isLocalAabbValid = true; + + #if 1 + static const btVector3 _directions[] = + { + btVector3( 1., 0., 0.), + btVector3( 0., 1., 0.), + btVector3( 0., 0., 1.), + btVector3( -1., 0., 0.), + btVector3( 0., -1., 0.), + btVector3( 0., 0., -1.) + }; + + btVector3 _supporting[] = + { + btVector3( 0., 0., 0.), + btVector3( 0., 0., 0.), + btVector3( 0., 0., 0.), + btVector3( 0., 0., 0.), + btVector3( 0., 0., 0.), + btVector3( 0., 0., 0.) + }; + + batchedUnitVectorGetSupportingVertexWithoutMargin(_directions, _supporting, 6); + + for ( int i = 0; i < 3; ++i ) + { + m_localAabbMax[i] = _supporting[i][i] + m_collisionMargin; + m_localAabbMin[i] = _supporting[i + 3][i] - m_collisionMargin; + } + + #else + + for (int i=0;i<3;i++) + { + btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.)); + vec[i] = btScalar(1.); + btVector3 tmp = localGetSupportingVertex(vec); + m_localAabbMax[i] = tmp[i]+m_collisionMargin; + vec[i] = btScalar(-1.); + tmp = localGetSupportingVertex(vec); + m_localAabbMin[i] = tmp[i]-m_collisionMargin; + } + #endif +} diff --git a/src/BulletCollision/CollisionShapes/btConvexInternalShape.h b/src/BulletCollision/CollisionShapes/btConvexInternalShape.h index ab3f91c46..3865d8e4b 100644 --- a/src/BulletCollision/CollisionShapes/btConvexInternalShape.h +++ b/src/BulletCollision/CollisionShapes/btConvexInternalShape.h @@ -17,6 +17,7 @@ subject to the following restrictions: #define BT_CONVEX_INTERNAL_SHAPE_H #include "btConvexShape.h" +#include "LinearMath/btAabbUtil2.h" ///The btConvexInternalShape is an internal base class, shared by most convex shape implementations. class btConvexInternalShape : public btConvexShape @@ -104,4 +105,47 @@ public: }; +///btConvexInternalAabbCachingShape adds local aabb caching for convex shapes, to avoid expensive bounding box calculations +class btConvexInternalAabbCachingShape : public btConvexInternalShape +{ + btVector3 m_localAabbMin; + btVector3 m_localAabbMax; + bool m_isLocalAabbValid; + +protected: + + btConvexInternalAabbCachingShape(); + + void setCachedLocalAabb (const btVector3& aabbMin, const btVector3& aabbMax) + { + m_isLocalAabbValid = true; + m_localAabbMin = aabbMin; + m_localAabbMax = aabbMax; + } + + inline void getCachedLocalAabb (btVector3& aabbMin, btVector3& aabbMax) const + { + btAssert(m_isLocalAabbValid); + aabbMin = m_localAabbMin; + aabbMax = m_localAabbMax; + } + + inline void getNonvirtualAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax, btScalar margin) const + { + + //lazy evaluation of local aabb + btAssert(m_isLocalAabbValid); + btTransformAabb(m_localAabbMin,m_localAabbMax,margin,trans,aabbMin,aabbMax); + } + +public: + + virtual void setLocalScaling(const btVector3& scaling); + + virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const; + + void recalcLocalAabb(); + +}; + #endif //BT_CONVEX_INTERNAL_SHAPE_H diff --git a/src/BulletCollision/CollisionShapes/btConvexPointCloudShape.h b/src/BulletCollision/CollisionShapes/btConvexPointCloudShape.h index 44f6c13d0..2f68ee7ac 100644 --- a/src/BulletCollision/CollisionShapes/btConvexPointCloudShape.h +++ b/src/BulletCollision/CollisionShapes/btConvexPointCloudShape.h @@ -21,7 +21,7 @@ subject to the following restrictions: #include "LinearMath/btAlignedObjectArray.h" ///The btConvexPointCloudShape implements an implicit convex hull of an array of vertices. -ATTRIBUTE_ALIGNED16(class) btConvexPointCloudShape : public btPolyhedralConvexShape +ATTRIBUTE_ALIGNED16(class) btConvexPointCloudShape : public btPolyhedralConvexAabbCachingShape { btVector3* m_unscaledPoints; int m_numPoints; diff --git a/src/BulletCollision/CollisionShapes/btConvexShape.cpp b/src/BulletCollision/CollisionShapes/btConvexShape.cpp index 8246e94a8..d88f48f1f 100644 --- a/src/BulletCollision/CollisionShapes/btConvexShape.cpp +++ b/src/BulletCollision/CollisionShapes/btConvexShape.cpp @@ -360,7 +360,7 @@ void btConvexShape::getAabbNonVirtual (const btTransform& t, btVector3& aabbMin, case CONVEX_POINT_CLOUD_SHAPE_PROXYTYPE: case CONVEX_HULL_SHAPE_PROXYTYPE: { - btPolyhedralConvexShape* convexHullShape = (btPolyhedralConvexShape*)this; + btPolyhedralConvexAabbCachingShape* convexHullShape = (btPolyhedralConvexAabbCachingShape*)this; btScalar margin = convexHullShape->getMarginNonVirtual(); convexHullShape->getNonvirtualAabb (t, aabbMin, aabbMax, margin); } diff --git a/src/BulletCollision/CollisionShapes/btConvexTriangleMeshShape.cpp b/src/BulletCollision/CollisionShapes/btConvexTriangleMeshShape.cpp index b9bb0dfb6..38c288f06 100644 --- a/src/BulletCollision/CollisionShapes/btConvexTriangleMeshShape.cpp +++ b/src/BulletCollision/CollisionShapes/btConvexTriangleMeshShape.cpp @@ -21,7 +21,7 @@ subject to the following restrictions: btConvexTriangleMeshShape ::btConvexTriangleMeshShape (btStridingMeshInterface* meshInterface, bool calcAabb) -: btPolyhedralConvexShape(), m_stridingMesh(meshInterface) +: btPolyhedralConvexAabbCachingShape(), m_stridingMesh(meshInterface) { m_shapeType = CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE; if ( calcAabb ) diff --git a/src/BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h b/src/BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h index ae32595ff..f5167e74b 100644 --- a/src/BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h +++ b/src/BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h @@ -22,7 +22,7 @@ subject to the following restrictions: /// The btConvexTriangleMeshShape is a convex hull of a triangle mesh, but the performance is not as good as btConvexHullShape. /// A small benefit of this class is that it uses the btStridingMeshInterface, so you can avoid the duplication of the triangle mesh data. Nevertheless, most users should use the much better performing btConvexHullShape instead. -class btConvexTriangleMeshShape : public btPolyhedralConvexShape +class btConvexTriangleMeshShape : public btPolyhedralConvexAabbCachingShape { class btStridingMeshInterface* m_stridingMesh; diff --git a/src/BulletCollision/CollisionShapes/btCylinderShape.cpp b/src/BulletCollision/CollisionShapes/btCylinderShape.cpp index e7eb6d341..268809304 100644 --- a/src/BulletCollision/CollisionShapes/btCylinderShape.cpp +++ b/src/BulletCollision/CollisionShapes/btCylinderShape.cpp @@ -16,11 +16,12 @@ subject to the following restrictions: #include "btCylinderShape.h" btCylinderShape::btCylinderShape (const btVector3& halfExtents) -:btBoxShape(halfExtents), +:btConvexInternalShape(), m_upAxis(1) { + btVector3 margin(getMargin(),getMargin(),getMargin()); + m_implicitShapeDimensions = (halfExtents * m_localScaling) - margin; m_shapeType = CYLINDER_SHAPE_PROXYTYPE; - recalcLocalAabb(); } @@ -28,7 +29,7 @@ btCylinderShapeX::btCylinderShapeX (const btVector3& halfExtents) :btCylinderShape(halfExtents) { m_upAxis = 0; - recalcLocalAabb(); + } @@ -36,13 +37,27 @@ btCylinderShapeZ::btCylinderShapeZ (const btVector3& halfExtents) :btCylinderShape(halfExtents) { m_upAxis = 2; - recalcLocalAabb(); + } void btCylinderShape::getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const { - //skip the box 'getAabb' - btPolyhedralConvexShape::getAabb(t,aabbMin,aabbMax); + btTransformAabb(getHalfExtentsWithoutMargin(),getMargin(),t,aabbMin,aabbMax); +} + +void btCylinderShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const +{ + //approximation of box shape, todo: implement cylinder shape inertia before people notice ;-) + btVector3 halfExtents = getHalfExtentsWithMargin(); + + btScalar lx=btScalar(2.)*(halfExtents.x()); + btScalar ly=btScalar(2.)*(halfExtents.y()); + btScalar lz=btScalar(2.)*(halfExtents.z()); + + inertia.setValue(mass/(btScalar(12.0)) * (ly*ly + lz*lz), + mass/(btScalar(12.0)) * (lx*lx + lz*lz), + mass/(btScalar(12.0)) * (lx*lx + ly*ly)); + } diff --git a/src/BulletCollision/CollisionShapes/btCylinderShape.h b/src/BulletCollision/CollisionShapes/btCylinderShape.h index 62e6fcee5..a92f9fc44 100644 --- a/src/BulletCollision/CollisionShapes/btCylinderShape.h +++ b/src/BulletCollision/CollisionShapes/btCylinderShape.h @@ -21,7 +21,7 @@ subject to the following restrictions: #include "LinearMath/btVector3.h" /// The btCylinderShape class implements a cylinder shape primitive, centered around the origin. Its central axis aligned with the Y axis. btCylinderShapeX is aligned with the X axis and btCylinderShapeZ around the Z axis. -class btCylinderShape : public btBoxShape +class btCylinderShape : public btConvexInternalShape { @@ -30,15 +30,42 @@ protected: int m_upAxis; public: + + btVector3 getHalfExtentsWithMargin() const + { + btVector3 halfExtents = getHalfExtentsWithoutMargin(); + btVector3 margin(getMargin(),getMargin(),getMargin()); + halfExtents += margin; + return halfExtents; + } + + const btVector3& getHalfExtentsWithoutMargin() const + { + return m_implicitShapeDimensions;//changed in Bullet 2.63: assume the scaling and margin are included + } + btCylinderShape (const btVector3& halfExtents); - ///getAabb's default implementation is brute force, expected derived classes to implement a fast dedicated version void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const; + virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const; + virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec)const; virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const; + virtual void setMargin(btScalar collisionMargin) + { + //correct the m_implicitShapeDimensions for the margin + btVector3 oldMargin(getMargin(),getMargin(),getMargin()); + btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin; + + btConvexInternalShape::setMargin(collisionMargin); + btVector3 newMargin(getMargin(),getMargin(),getMargin()); + m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin; + + } + virtual btVector3 localGetSupportingVertex(const btVector3& vec) const { diff --git a/src/BulletCollision/CollisionShapes/btMultiSphereShape.cpp b/src/BulletCollision/CollisionShapes/btMultiSphereShape.cpp index a1539298a..c3b170a64 100644 --- a/src/BulletCollision/CollisionShapes/btMultiSphereShape.cpp +++ b/src/BulletCollision/CollisionShapes/btMultiSphereShape.cpp @@ -20,7 +20,7 @@ subject to the following restrictions: #include "LinearMath/btQuaternion.h" btMultiSphereShape::btMultiSphereShape (const btVector3& inertiaHalfExtents,const btVector3* positions,const btScalar* radi,int numSpheres) -:btConvexInternalShape (), m_inertiaHalfExtents(inertiaHalfExtents) +:btConvexInternalAabbCachingShape (), m_inertiaHalfExtents(inertiaHalfExtents) { m_shapeType = MULTI_SPHERE_SHAPE_PROXYTYPE; btScalar startMargin = btScalar(1e30); @@ -34,6 +34,7 @@ btMultiSphereShape::btMultiSphereShape (const btVector3& inertiaHalfExtents,cons } + recalcLocalAabb(); } @@ -149,4 +150,3 @@ void btMultiSphereShape::calculateLocalInertia(btScalar mass,btVector3& inertia) } - diff --git a/src/BulletCollision/CollisionShapes/btMultiSphereShape.h b/src/BulletCollision/CollisionShapes/btMultiSphereShape.h index 30e108306..ae8e5c26f 100644 --- a/src/BulletCollision/CollisionShapes/btMultiSphereShape.h +++ b/src/BulletCollision/CollisionShapes/btMultiSphereShape.h @@ -19,13 +19,16 @@ subject to the following restrictions: #include "btConvexInternalShape.h" #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" // for the types #include "LinearMath/btAlignedObjectArray.h" +#include "LinearMath/btAabbUtil2.h" ///The btMultiSphereShape represents the convex hull of a collection of spheres. You can create special capsules or other smooth volumes. ///It is possible to animate the spheres for deformation. -class btMultiSphereShape : public btConvexInternalShape +class btMultiSphereShape : public btConvexInternalAabbCachingShape { + + btAlignedObjectArray m_localPositionArray; btAlignedObjectArray m_radiArray; btVector3 m_inertiaHalfExtents; @@ -64,6 +67,7 @@ public: return "MultiSphere"; } + }; diff --git a/src/BulletCollision/CollisionShapes/btPolyhedralConvexShape.cpp b/src/BulletCollision/CollisionShapes/btPolyhedralConvexShape.cpp index d62500a15..bad4f8132 100644 --- a/src/BulletCollision/CollisionShapes/btPolyhedralConvexShape.cpp +++ b/src/BulletCollision/CollisionShapes/btPolyhedralConvexShape.cpp @@ -15,11 +15,7 @@ subject to the following restrictions: #include "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h" -btPolyhedralConvexShape::btPolyhedralConvexShape() :btConvexInternalShape(), -m_localAabbMin(1,1,1), -m_localAabbMax(-1,-1,-1), -m_isLocalAabbValid(false), -m_optionalHull(0) +btPolyhedralConvexShape::btPolyhedralConvexShape() :btConvexInternalShape() { } @@ -120,20 +116,26 @@ void btPolyhedralConvexShape::calculateLocalInertia(btScalar mass,btVector3& ine -void btPolyhedralConvexShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const -{ - getNonvirtualAabb(trans,aabbMin,aabbMax,getMargin()); -} - - - -void btPolyhedralConvexShape::setLocalScaling(const btVector3& scaling) +void btPolyhedralConvexAabbCachingShape::setLocalScaling(const btVector3& scaling) { btConvexInternalShape::setLocalScaling(scaling); recalcLocalAabb(); } -void btPolyhedralConvexShape::recalcLocalAabb() +btPolyhedralConvexAabbCachingShape::btPolyhedralConvexAabbCachingShape() +:btPolyhedralConvexShape(), +m_localAabbMin(1,1,1), +m_localAabbMax(-1,-1,-1), +m_isLocalAabbValid(false) +{ +} + +void btPolyhedralConvexAabbCachingShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const +{ + getNonvirtualAabb(trans,aabbMin,aabbMax,getMargin()); +} + +void btPolyhedralConvexAabbCachingShape::recalcLocalAabb() { m_isLocalAabbValid = true; @@ -181,5 +183,3 @@ void btPolyhedralConvexShape::recalcLocalAabb() #endif } - - diff --git a/src/BulletCollision/CollisionShapes/btPolyhedralConvexShape.h b/src/BulletCollision/CollisionShapes/btPolyhedralConvexShape.h index 77d620d3b..2c691b956 100644 --- a/src/BulletCollision/CollisionShapes/btPolyhedralConvexShape.h +++ b/src/BulletCollision/CollisionShapes/btPolyhedralConvexShape.h @@ -17,7 +17,6 @@ subject to the following restrictions: #define BU_SHAPE #include "LinearMath/btMatrix3x3.h" -#include "LinearMath/btAabbUtil2.h" #include "btConvexInternalShape.h" @@ -26,10 +25,7 @@ class btPolyhedralConvexShape : public btConvexInternalShape { protected: - btVector3 m_localAabbMin; - btVector3 m_localAabbMax; - bool m_isLocalAabbValid; - + public: btPolyhedralConvexShape(); @@ -38,10 +34,32 @@ public: virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec)const; virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const; - virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const; + + + virtual int getNumVertices() const = 0 ; + virtual int getNumEdges() const = 0; + virtual void getEdge(int i,btVector3& pa,btVector3& pb) const = 0; + virtual void getVertex(int i,btVector3& vtx) const = 0; + virtual int getNumPlanes() const = 0; + virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i ) const = 0; +// virtual int getIndex(int i) const = 0 ; + virtual bool isInside(const btVector3& pt,btScalar tolerance) const = 0; + +}; + + +///The btPolyhedralConvexAabbCachingShape adds aabb caching to the btPolyhedralConvexShape +class btPolyhedralConvexAabbCachingShape : public btPolyhedralConvexShape +{ + + btVector3 m_localAabbMin; + btVector3 m_localAabbMax; + bool m_isLocalAabbValid; + +protected: void setCachedLocalAabb (const btVector3& aabbMin, const btVector3& aabbMax) { @@ -57,6 +75,10 @@ public: aabbMax = m_localAabbMax; } +public: + + btPolyhedralConvexAabbCachingShape(); + inline void getNonvirtualAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax, btScalar margin) const { @@ -65,26 +87,12 @@ public: btTransformAabb(m_localAabbMin,m_localAabbMax,margin,trans,aabbMin,aabbMax); } - - virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const; - virtual void setLocalScaling(const btVector3& scaling); + virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const; + void recalcLocalAabb(); - virtual int getNumVertices() const = 0 ; - virtual int getNumEdges() const = 0; - virtual void getEdge(int i,btVector3& pa,btVector3& pb) const = 0; - virtual void getVertex(int i,btVector3& vtx) const = 0; - virtual int getNumPlanes() const = 0; - virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i ) const = 0; -// virtual int getIndex(int i) const = 0 ; - - virtual bool isInside(const btVector3& pt,btScalar tolerance) const = 0; - - /// optional Hull is for optional Separating Axis Test Hull collision detection, see Hull.cpp - class Hull* m_optionalHull; - }; #endif //BU_SHAPE diff --git a/src/BulletCollision/CollisionShapes/btTetrahedronShape.cpp b/src/BulletCollision/CollisionShapes/btTetrahedronShape.cpp index 22978dfeb..b7eb44381 100644 --- a/src/BulletCollision/CollisionShapes/btTetrahedronShape.cpp +++ b/src/BulletCollision/CollisionShapes/btTetrahedronShape.cpp @@ -16,20 +16,20 @@ subject to the following restrictions: #include "btTetrahedronShape.h" #include "LinearMath/btMatrix3x3.h" -btBU_Simplex1to4::btBU_Simplex1to4() : btPolyhedralConvexShape (), +btBU_Simplex1to4::btBU_Simplex1to4() : btPolyhedralConvexAabbCachingShape (), m_numVertices(0) { m_shapeType = TETRAHEDRAL_SHAPE_PROXYTYPE; } -btBU_Simplex1to4::btBU_Simplex1to4(const btVector3& pt0) : btPolyhedralConvexShape (), +btBU_Simplex1to4::btBU_Simplex1to4(const btVector3& pt0) : btPolyhedralConvexAabbCachingShape (), m_numVertices(0) { m_shapeType = TETRAHEDRAL_SHAPE_PROXYTYPE; addVertex(pt0); } -btBU_Simplex1to4::btBU_Simplex1to4(const btVector3& pt0,const btVector3& pt1) : btPolyhedralConvexShape (), +btBU_Simplex1to4::btBU_Simplex1to4(const btVector3& pt0,const btVector3& pt1) : btPolyhedralConvexAabbCachingShape (), m_numVertices(0) { m_shapeType = TETRAHEDRAL_SHAPE_PROXYTYPE; @@ -37,7 +37,7 @@ m_numVertices(0) addVertex(pt1); } -btBU_Simplex1to4::btBU_Simplex1to4(const btVector3& pt0,const btVector3& pt1,const btVector3& pt2) : btPolyhedralConvexShape (), +btBU_Simplex1to4::btBU_Simplex1to4(const btVector3& pt0,const btVector3& pt1,const btVector3& pt2) : btPolyhedralConvexAabbCachingShape (), m_numVertices(0) { m_shapeType = TETRAHEDRAL_SHAPE_PROXYTYPE; @@ -46,7 +46,7 @@ m_numVertices(0) addVertex(pt2); } -btBU_Simplex1to4::btBU_Simplex1to4(const btVector3& pt0,const btVector3& pt1,const btVector3& pt2,const btVector3& pt3) : btPolyhedralConvexShape (), +btBU_Simplex1to4::btBU_Simplex1to4(const btVector3& pt0,const btVector3& pt1,const btVector3& pt2,const btVector3& pt3) : btPolyhedralConvexAabbCachingShape (), m_numVertices(0) { m_shapeType = TETRAHEDRAL_SHAPE_PROXYTYPE; @@ -57,13 +57,31 @@ m_numVertices(0) } +void btBU_Simplex1to4::getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const +{ +#if 1 + btPolyhedralConvexAabbCachingShape::getAabb(t,aabbMin,aabbMax); +#else + aabbMin.setValue(1e30f,1e30f,1e30f); + aabbMax.setValue(-1e30f,-1e30f,-1e30f); + + //just transform the vertices in worldspace, and take their AABB + for (int i=0;i