From 177b6f5ce2f396eb9ededeeb2236fbf67e602e22 Mon Sep 17 00:00:00 2001 From: ejcoumans Date: Sat, 28 Jul 2007 19:20:45 +0000 Subject: [PATCH] Added btUniformScalingShape (including support for debug rendering etc) This allows to re-use a convex shape, while each instance can re-scale it (with a uniform scalar factor) --- Demos/OpenGL/DemoApplication.cpp | 10 ++- Demos/OpenGL/GL_ShapeDrawer.cpp | 19 ++++++ Demos/RagdollDemo/RagdollDemo.cpp | 8 +++ .../BroadphaseCollision/btBroadphaseProxy.h | 1 + .../CollisionShapes/btUniformScalingShape.cpp | 53 +++++++++++++++ .../CollisionShapes/btUniformScalingShape.h | 64 +++++++++++++++++++ 6 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/BulletCollision/CollisionShapes/btUniformScalingShape.cpp create mode 100644 src/BulletCollision/CollisionShapes/btUniformScalingShape.h diff --git a/Demos/OpenGL/DemoApplication.cpp b/Demos/OpenGL/DemoApplication.cpp index a80ee0bb8..99fc31f6f 100644 --- a/Demos/OpenGL/DemoApplication.cpp +++ b/Demos/OpenGL/DemoApplication.cpp @@ -23,6 +23,7 @@ subject to the following restrictions: #include "BulletCollision/CollisionShapes/btBoxShape.h" #include "BulletCollision/CollisionShapes/btSphereShape.h" #include "BulletCollision/CollisionShapes/btCompoundShape.h" +#include "BulletCollision/CollisionShapes/btUniformScalingShape.h" #include "GL_ShapeDrawer.h" #include "LinearMath/btQuickprof.h" @@ -449,8 +450,13 @@ void DemoApplication::shootBox(const btVector3& destination) startTransform.setIdentity(); btVector3 camPos = getCameraPosition(); startTransform.setOrigin(camPos); - //btCollisionShape* boxShape = new btSphereShape(1); - btCollisionShape* boxShape = new btBoxShape(btVector3(1.f,1.f,1.f)); +//#define TEST_UNIFORM_SCALING_SHAPE 1 +#ifdef TEST_UNIFORM_SCALING_SHAPE + btConvexShape* childShape = new btBoxShape(btVector3(1.f,1.f,1.f)); + btUniformScalingShape* boxShape = new btUniformScalingShape(childShape,0.5f); +#else + btCollisionShape* boxShape = new btSphereShape(1); +#endif// btRigidBody* body = this->localCreateRigidBody(mass, startTransform,boxShape); btVector3 linVel(destination[0]-camPos[0],destination[1]-camPos[1],destination[2]-camPos[2]); diff --git a/Demos/OpenGL/GL_ShapeDrawer.cpp b/Demos/OpenGL/GL_ShapeDrawer.cpp index d9e1b17e3..922d86c4e 100644 --- a/Demos/OpenGL/GL_ShapeDrawer.cpp +++ b/Demos/OpenGL/GL_ShapeDrawer.cpp @@ -38,6 +38,8 @@ subject to the following restrictions: #include "BulletCollision/CollisionShapes/btCompoundShape.h" #include "BulletCollision/CollisionShapes/btCapsuleShape.h" #include "BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h" +#include "BulletCollision/CollisionShapes/btUniformScalingShape.h" + #include "LinearMath/btIDebugDraw.h" @@ -313,6 +315,23 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons glPushMatrix(); btglMultMatrix(m); + if (shape->getShapeType() == UNIFORM_SCALING_SHAPE_PROXYTYPE) + { + const btUniformScalingShape* scalingShape = static_cast(shape); + const btConvexShape* convexShape = scalingShape->getChildShape(); + float scalingFactor = (float)scalingShape->getUniformScalingFactor(); + { + btScalar tmpScaling[4][4]={scalingFactor,0,0,0, + 0,scalingFactor,0,0, + 0,0,scalingFactor,0, + 0,0,0,1}; + + drawOpenGL( (btScalar*)tmpScaling,convexShape,color,debugMode); + } + glPopMatrix(); + return; + } + if (shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) { const btCompoundShape* compoundShape = static_cast(shape); diff --git a/Demos/RagdollDemo/RagdollDemo.cpp b/Demos/RagdollDemo/RagdollDemo.cpp index 27698a902..54de175d0 100644 --- a/Demos/RagdollDemo/RagdollDemo.cpp +++ b/Demos/RagdollDemo/RagdollDemo.cpp @@ -21,7 +21,13 @@ Written by: Marten Svanfeldt #include "GlutStuff.h" #include "GL_ShapeDrawer.h" +#include "LinearMath/btIDebugDraw.h" + +#include "GLDebugDrawer.h" #include "RagdollDemo.h" + +GLDebugDrawer debugDrawer; + #define M_PI 3.14159265358979323846 #define M_PI_2 1.57079632679489661923 #define M_PI_4 0.785398163397448309616 @@ -311,6 +317,8 @@ void RagdollDemo::initPhysics() m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver); + m_dynamicsWorld->setDebugDrawer(&debugDrawer); + // Setup a big ground box { btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.))); diff --git a/src/BulletCollision/BroadphaseCollision/btBroadphaseProxy.h b/src/BulletCollision/BroadphaseCollision/btBroadphaseProxy.h index c5650e522..c00da7c21 100644 --- a/src/BulletCollision/BroadphaseCollision/btBroadphaseProxy.h +++ b/src/BulletCollision/BroadphaseCollision/btBroadphaseProxy.h @@ -38,6 +38,7 @@ IMPLICIT_CONVEX_SHAPES_START_HERE, CONE_SHAPE_PROXYTYPE, CONVEX_SHAPE_PROXYTYPE, CYLINDER_SHAPE_PROXYTYPE, + UNIFORM_SCALING_SHAPE_PROXYTYPE, MINKOWSKI_SUM_SHAPE_PROXYTYPE, MINKOWSKI_DIFFERENCE_SHAPE_PROXYTYPE, //concave shapes diff --git a/src/BulletCollision/CollisionShapes/btUniformScalingShape.cpp b/src/BulletCollision/CollisionShapes/btUniformScalingShape.cpp new file mode 100644 index 000000000..65089e6b1 --- /dev/null +++ b/src/BulletCollision/CollisionShapes/btUniformScalingShape.cpp @@ -0,0 +1,53 @@ +/* +Bullet Continuous Collision Detection and Physics Library +Copyright (c) 2003-2007 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 "btUniformScalingShape.h" + +btUniformScalingShape::btUniformScalingShape( btConvexShape* convexChildShape,btScalar uniformScalingFactor): +m_childConvexShape(convexChildShape), +m_uniformScalingFactor(uniformScalingFactor) +{ +} + +btUniformScalingShape::~btUniformScalingShape() +{ +} + + +btVector3 btUniformScalingShape::localGetSupportingVertexWithoutMargin(const btVector3& vec)const +{ + btVector3 tmpVertex; + tmpVertex = m_childConvexShape->localGetSupportingVertexWithoutMargin(vec); + return tmpVertex*m_uniformScalingFactor; +} + +void btUniformScalingShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const +{ + m_childConvexShape->batchedUnitVectorGetSupportingVertexWithoutMargin(vectors,supportVerticesOut,numVectors); + int i; + for (i=0;icalculateLocalInertia(mass,tmpInertia); + inertia = tmpInertia * m_uniformScalingFactor; +} diff --git a/src/BulletCollision/CollisionShapes/btUniformScalingShape.h b/src/BulletCollision/CollisionShapes/btUniformScalingShape.h new file mode 100644 index 000000000..87692f101 --- /dev/null +++ b/src/BulletCollision/CollisionShapes/btUniformScalingShape.h @@ -0,0 +1,64 @@ +/* +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_UNIFORM_SCALING_SHAPE_H +#define BT_UNIFORM_SCALING_SHAPE_H + +#include "btConvexShape.h" +#include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" // for the types + +class btUniformScalingShape : public btConvexShape +{ + btConvexShape* m_childConvexShape; + + btScalar m_uniformScalingFactor; + + public: + + btUniformScalingShape( btConvexShape* convexChildShape, btScalar uniformScalingFactor); + + virtual ~btUniformScalingShape(); + + 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); + + btScalar getUniformScalingFactor() const + { + return m_uniformScalingFactor; + } + + btConvexShape* getChildShape() + { + return m_childConvexShape; + } + + const btConvexShape* getChildShape() const + { + return m_childConvexShape; + } + + virtual char* getName()const + { + return "UniformScalingShape"; + } + + virtual int getShapeType() const { return UNIFORM_SCALING_SHAPE_PROXYTYPE; } + +}; + +#endif //BT_UNIFORM_SCALING_SHAPE_H