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.
16 #include "btCapsuleShape.h"
18 #include "LinearMath/btQuaternion.h"
20 btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height) : btConvexInternalShape()
22 m_collisionMargin = radius;
23 m_shapeType = CAPSULE_SHAPE_PROXYTYPE;
25 m_implicitShapeDimensions.setValue(radius, 0.5f * height, radius);
28 btVector3 btCapsuleShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0) const
30 btVector3 supVec(0, 0, 0);
32 btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
35 btScalar lenSqr = vec.length2();
36 if (lenSqr < btScalar(0.0001))
38 vec.setValue(1, 0, 0);
42 btScalar rlen = btScalar(1.) / btSqrt(lenSqr);
50 btVector3 pos(0, 0, 0);
51 pos[getUpAxis()] = getHalfHeight();
54 newDot = vec.dot(vtx);
62 btVector3 pos(0, 0, 0);
63 pos[getUpAxis()] = -getHalfHeight();
66 newDot = vec.dot(vtx);
77 void btCapsuleShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
79 for (int j = 0; j < numVectors; j++)
81 btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
82 const btVector3& vec = vectors[j];
87 btVector3 pos(0, 0, 0);
88 pos[getUpAxis()] = getHalfHeight();
90 newDot = vec.dot(vtx);
94 supportVerticesOut[j] = vtx;
98 btVector3 pos(0, 0, 0);
99 pos[getUpAxis()] = -getHalfHeight();
101 newDot = vec.dot(vtx);
105 supportVerticesOut[j] = vtx;
111 void btCapsuleShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
113 //as an approximation, take the inertia of the box that bounds the spheres
118 btScalar radius = getRadius();
120 btVector3 halfExtents(radius, radius, radius);
121 halfExtents[getUpAxis()] += getHalfHeight();
123 btScalar lx = btScalar(2.) * (halfExtents[0]);
124 btScalar ly = btScalar(2.) * (halfExtents[1]);
125 btScalar lz = btScalar(2.) * (halfExtents[2]);
126 const btScalar x2 = lx * lx;
127 const btScalar y2 = ly * ly;
128 const btScalar z2 = lz * lz;
129 const btScalar scaledmass = mass * btScalar(.08333333);
131 inertia[0] = scaledmass * (y2 + z2);
132 inertia[1] = scaledmass * (x2 + z2);
133 inertia[2] = scaledmass * (x2 + y2);
136 btCapsuleShapeX::btCapsuleShapeX(btScalar radius, btScalar height)
138 m_collisionMargin = radius;
140 m_implicitShapeDimensions.setValue(0.5f * height, radius, radius);
143 btCapsuleShapeZ::btCapsuleShapeZ(btScalar radius, btScalar height)
145 m_collisionMargin = radius;
147 m_implicitShapeDimensions.setValue(radius, radius, 0.5f * height);