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)
This commit is contained in:
ejcoumans
2007-07-28 19:20:45 +00:00
parent 0df4cedc3e
commit 177b6f5ce2
6 changed files with 153 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ subject to the following restrictions:
#include "BulletCollision/CollisionShapes/btBoxShape.h" #include "BulletCollision/CollisionShapes/btBoxShape.h"
#include "BulletCollision/CollisionShapes/btSphereShape.h" #include "BulletCollision/CollisionShapes/btSphereShape.h"
#include "BulletCollision/CollisionShapes/btCompoundShape.h" #include "BulletCollision/CollisionShapes/btCompoundShape.h"
#include "BulletCollision/CollisionShapes/btUniformScalingShape.h"
#include "GL_ShapeDrawer.h" #include "GL_ShapeDrawer.h"
#include "LinearMath/btQuickprof.h" #include "LinearMath/btQuickprof.h"
@@ -449,8 +450,13 @@ void DemoApplication::shootBox(const btVector3& destination)
startTransform.setIdentity(); startTransform.setIdentity();
btVector3 camPos = getCameraPosition(); btVector3 camPos = getCameraPosition();
startTransform.setOrigin(camPos); startTransform.setOrigin(camPos);
//btCollisionShape* boxShape = new btSphereShape(1); //#define TEST_UNIFORM_SCALING_SHAPE 1
btCollisionShape* boxShape = new btBoxShape(btVector3(1.f,1.f,1.f)); #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); btRigidBody* body = this->localCreateRigidBody(mass, startTransform,boxShape);
btVector3 linVel(destination[0]-camPos[0],destination[1]-camPos[1],destination[2]-camPos[2]); btVector3 linVel(destination[0]-camPos[0],destination[1]-camPos[1],destination[2]-camPos[2]);

View File

@@ -38,6 +38,8 @@ subject to the following restrictions:
#include "BulletCollision/CollisionShapes/btCompoundShape.h" #include "BulletCollision/CollisionShapes/btCompoundShape.h"
#include "BulletCollision/CollisionShapes/btCapsuleShape.h" #include "BulletCollision/CollisionShapes/btCapsuleShape.h"
#include "BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h" #include "BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h"
#include "BulletCollision/CollisionShapes/btUniformScalingShape.h"
#include "LinearMath/btIDebugDraw.h" #include "LinearMath/btIDebugDraw.h"
@@ -313,6 +315,23 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons
glPushMatrix(); glPushMatrix();
btglMultMatrix(m); btglMultMatrix(m);
if (shape->getShapeType() == UNIFORM_SCALING_SHAPE_PROXYTYPE)
{
const btUniformScalingShape* scalingShape = static_cast<const btUniformScalingShape*>(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) if (shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE)
{ {
const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(shape); const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(shape);

View File

@@ -21,7 +21,13 @@ Written by: Marten Svanfeldt
#include "GlutStuff.h" #include "GlutStuff.h"
#include "GL_ShapeDrawer.h" #include "GL_ShapeDrawer.h"
#include "LinearMath/btIDebugDraw.h"
#include "GLDebugDrawer.h"
#include "RagdollDemo.h" #include "RagdollDemo.h"
GLDebugDrawer debugDrawer;
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
#define M_PI_2 1.57079632679489661923 #define M_PI_2 1.57079632679489661923
#define M_PI_4 0.785398163397448309616 #define M_PI_4 0.785398163397448309616
@@ -311,6 +317,8 @@ void RagdollDemo::initPhysics()
m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver); m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver);
m_dynamicsWorld->setDebugDrawer(&debugDrawer);
// Setup a big ground box // Setup a big ground box
{ {
btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.))); btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.)));

View File

@@ -38,6 +38,7 @@ IMPLICIT_CONVEX_SHAPES_START_HERE,
CONE_SHAPE_PROXYTYPE, CONE_SHAPE_PROXYTYPE,
CONVEX_SHAPE_PROXYTYPE, CONVEX_SHAPE_PROXYTYPE,
CYLINDER_SHAPE_PROXYTYPE, CYLINDER_SHAPE_PROXYTYPE,
UNIFORM_SCALING_SHAPE_PROXYTYPE,
MINKOWSKI_SUM_SHAPE_PROXYTYPE, MINKOWSKI_SUM_SHAPE_PROXYTYPE,
MINKOWSKI_DIFFERENCE_SHAPE_PROXYTYPE, MINKOWSKI_DIFFERENCE_SHAPE_PROXYTYPE,
//concave shapes //concave shapes

View File

@@ -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;i<numVectors;i++)
{
supportVerticesOut[i] = supportVerticesOut[i] * m_uniformScalingFactor;
}
}
void btUniformScalingShape::calculateLocalInertia(btScalar mass,btVector3& inertia)
{
///this linear upscaling is not realistic, but we don't deal with large mass ratios...
btVector3 tmpInertia;
m_childConvexShape->calculateLocalInertia(mass,tmpInertia);
inertia = tmpInertia * m_uniformScalingFactor;
}

View File

@@ -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