2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
17 #include "btCapsuleShape.h"
19 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
20 #include "LinearMath/btQuaternion.h"
22 btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height) : btConvexInternalShape ()
24 m_shapeType = CAPSULE_SHAPE_PROXYTYPE;
26 m_implicitShapeDimensions.setValue(radius,0.5f*height,radius);
30 btVector3 btCapsuleShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
33 btVector3 supVec(0,0,0);
35 btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
38 btScalar lenSqr = vec.length2();
39 if (lenSqr < btScalar(0.0001))
44 btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
51 btScalar radius = getRadius();
56 pos[getUpAxis()] = getHalfHeight();
58 vtx = pos +vec*(radius) - vec * getMargin();
59 newDot = vec.dot(vtx);
68 pos[getUpAxis()] = -getHalfHeight();
70 vtx = pos +vec*(radius) - vec * getMargin();
71 newDot = vec.dot(vtx);
83 void btCapsuleShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
87 btScalar radius = getRadius();
89 for (int j=0;j<numVectors;j++)
91 btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
92 const btVector3& vec = vectors[j];
98 pos[getUpAxis()] = getHalfHeight();
99 vtx = pos +vec*(radius) - vec * getMargin();
100 newDot = vec.dot(vtx);
104 supportVerticesOut[j] = vtx;
108 btVector3 pos(0,0,0);
109 pos[getUpAxis()] = -getHalfHeight();
110 vtx = pos +vec*(radius) - vec * getMargin();
111 newDot = vec.dot(vtx);
115 supportVerticesOut[j] = vtx;
123 void btCapsuleShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
125 //as an approximation, take the inertia of the box that bounds the spheres
131 btScalar radius = getRadius();
133 btVector3 halfExtents(radius,radius,radius);
134 halfExtents[getUpAxis()]+=getHalfHeight();
136 btScalar margin = CONVEX_DISTANCE_MARGIN;
138 btScalar lx=btScalar(2.)*(halfExtents[0]+margin);
139 btScalar ly=btScalar(2.)*(halfExtents[1]+margin);
140 btScalar lz=btScalar(2.)*(halfExtents[2]+margin);
141 const btScalar x2 = lx*lx;
142 const btScalar y2 = ly*ly;
143 const btScalar z2 = lz*lz;
144 const btScalar scaledmass = mass * btScalar(.08333333);
146 inertia[0] = scaledmass * (y2+z2);
147 inertia[1] = scaledmass * (x2+z2);
148 inertia[2] = scaledmass * (x2+y2);
152 btCapsuleShapeX::btCapsuleShapeX(btScalar radius,btScalar height)
155 m_implicitShapeDimensions.setValue(0.5f*height, radius,radius);
163 btCapsuleShapeZ::btCapsuleShapeZ(btScalar radius,btScalar height)
166 m_implicitShapeDimensions.setValue(radius,radius,0.5f*height);