added btCapsuleShapeX and btCapsuleShapeZ, for X and Z upaxis capsules. The default btCapsuleShape stays Y-axis, for backwards compatibility.

This commit is contained in:
ejcoumans
2008-02-07 00:34:06 +00:00
parent d88ba6063e
commit f22d338959
2 changed files with 154 additions and 65 deletions

View File

@@ -50,7 +50,9 @@ btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height)
{ {
btVector3 pos(0,getHalfHeight(),0); btVector3 pos(0,0,0);
pos[getUpAxis()] = getHalfHeight();
vtx = pos +vec*m_localScaling*(radius) - vec * getMargin(); vtx = pos +vec*m_localScaling*(radius) - vec * getMargin();
newDot = vec.dot(vtx); newDot = vec.dot(vtx);
if (newDot > maxDot) if (newDot > maxDot)
@@ -60,7 +62,9 @@ btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height)
} }
} }
{ {
btVector3 pos(0,-getHalfHeight(),0); btVector3 pos(0,0,0);
pos[getUpAxis()] = -getHalfHeight();
vtx = pos +vec*m_localScaling*(radius) - vec * getMargin(); vtx = pos +vec*m_localScaling*(radius) - vec * getMargin();
newDot = vec.dot(vtx); newDot = vec.dot(vtx);
if (newDot > maxDot) if (newDot > maxDot)
@@ -88,7 +92,8 @@ btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height)
btVector3 vtx; btVector3 vtx;
btScalar newDot; btScalar newDot;
{ {
btVector3 pos(0,getHalfHeight(),0); btVector3 pos(0,0,0);
pos[getUpAxis()] = getHalfHeight();
vtx = pos +vec*m_localScaling*(radius) - vec * getMargin(); vtx = pos +vec*m_localScaling*(radius) - vec * getMargin();
newDot = vec.dot(vtx); newDot = vec.dot(vtx);
if (newDot > maxDot) if (newDot > maxDot)
@@ -98,7 +103,8 @@ btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height)
} }
} }
{ {
btVector3 pos(0,-getHalfHeight(),0); btVector3 pos(0,0,0);
pos[getUpAxis()] = -getHalfHeight();
vtx = pos +vec*m_localScaling*(radius) - vec * getMargin(); vtx = pos +vec*m_localScaling*(radius) - vec * getMargin();
newDot = vec.dot(vtx); newDot = vec.dot(vtx);
if (newDot > maxDot) if (newDot > maxDot)
@@ -122,7 +128,8 @@ void btCapsuleShape::calculateLocalInertia(btScalar mass,btVector3& inertia) con
btScalar radius = getRadius(); btScalar radius = getRadius();
btVector3 halfExtents(radius,radius+getHalfHeight(),radius); btVector3 halfExtents(radius,radius,radius);
halfExtents[getUpAxis()]+=getHalfHeight();
btScalar margin = CONVEX_DISTANCE_MARGIN; btScalar margin = CONVEX_DISTANCE_MARGIN;
@@ -140,6 +147,20 @@ void btCapsuleShape::calculateLocalInertia(btScalar mass,btVector3& inertia) con
} }
btCapsuleShapeX::btCapsuleShapeX(btScalar radius,btScalar height)
{
m_implicitShapeDimensions.setValue(0.5f*height, radius,radius);
}
btCapsuleShapeZ::btCapsuleShapeZ(btScalar radius,btScalar height)
{
m_implicitShapeDimensions.setValue(radius,radius,0.5f*height);
}

View File

@@ -1,60 +1,128 @@
/* /*
Bullet Continuous Collision Detection and Physics Library Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty. 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. 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, Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely, including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions: 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. 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. 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. 3. This notice may not be removed or altered from any source distribution.
*/ */
#ifndef BT_CAPSULE_SHAPE_H #ifndef BT_CAPSULE_SHAPE_H
#define BT_CAPSULE_SHAPE_H #define BT_CAPSULE_SHAPE_H
#include "btConvexInternalShape.h" #include "btConvexInternalShape.h"
#include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" // for the types #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" // for the types
///btCapsuleShape represents a capsule around the Y axis ///btCapsuleShape represents a capsule around the Y axis
///A more general solution that can represent capsules is the btMultiSphereShape ///A more general solution that can represent capsules is the btMultiSphereShape
class btCapsuleShape : public btConvexInternalShape ///the total height is height+2*radius, so the height is just the height between the center of each 'sphere' of the capsule caps.
{ class btCapsuleShape : public btConvexInternalShape
{
public: protected:
btCapsuleShape(btScalar radius,btScalar height); ///only used for btCapsuleShapeZ and btCapsuleShapeX subclasses.
btCapsuleShape() {};
///CollisionShape Interface
virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const; public:
btCapsuleShape(btScalar radius,btScalar height);
/// btConvexShape Interface
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec)const; ///CollisionShape Interface
virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const;
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const;
/// btConvexShape Interface
virtual int getShapeType() const { return CAPSULE_SHAPE_PROXYTYPE; } virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec)const;
virtual const char* getName()const virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const;
{
return "CapsuleShape"; virtual int getShapeType() const { return CAPSULE_SHAPE_PROXYTYPE; }
}
virtual const char* getName()const
btScalar getRadius() const {
{ return "CapsuleShape";
return m_implicitShapeDimensions.getX(); }
}
virtual int getUpAxis() const
btScalar getHalfHeight() const {
{ return 1;
return m_implicitShapeDimensions.getY(); }
}
virtual btScalar getRadius() const
}; {
return m_implicitShapeDimensions.getX();
}
#endif //BT_CAPSULE_SHAPE_H virtual btScalar getHalfHeight() const
{
return m_implicitShapeDimensions.getY();
}
};
///btCapsuleShapeX represents a capsule around the Z axis
///the total height is height+2*radius, so the height is just the height between the center of each 'sphere' of the capsule caps.
class btCapsuleShapeX : public btCapsuleShape
{
public:
btCapsuleShapeX(btScalar radius,btScalar height);
virtual int getUpAxis() const
{
return 0;
}
//debugging
virtual const char* getName()const
{
return "CapsuleX";
}
virtual btScalar getRadius() const
{
return m_implicitShapeDimensions.getY();
}
virtual btScalar getHalfHeight() const
{
return m_implicitShapeDimensions.getX();
}
};
///btCapsuleShapeZ represents a capsule around the Z axis
///the total height is height+2*radius, so the height is just the height between the center of each 'sphere' of the capsule caps.
class btCapsuleShapeZ : public btCapsuleShape
{
public:
btCapsuleShapeZ(btScalar radius,btScalar height);
virtual int getUpAxis() const
{
return 2;
}
//debugging
virtual const char* getName()const
{
return "CapsuleZ";
}
virtual btScalar getRadius() const
{
return m_implicitShapeDimensions.getX();
}
virtual btScalar getHalfHeight() const
{
return m_implicitShapeDimensions.getZ();
}
};
#endif //BT_CAPSULE_SHAPE_H