Bullet Collision Detection & Physics Library: btCapsuleShape.cpp Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btCapsuleShape.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#include "btCapsuleShape.h"
17
18#include "LinearMath/btQuaternion.h"
19
20 btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height) : btConvexInternalShape()
21{
22 m_collisionMargin = radius;
23 m_shapeType = CAPSULE_SHAPE_PROXYTYPE;
24 m_upAxis = 1;
25 m_implicitShapeDimensions.setValue(radius, 0.5f * height, radius);
26}
27
28 btVector3 btCapsuleShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0) const
29{
30 btVector3 supVec(0, 0, 0);
31
32 btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
33
34 btVector3 vec = vec0;
35 btScalar lenSqr = vec.length2();
36 if (lenSqr < btScalar(0.0001))
37 {
38 vec.setValue(1, 0, 0);
39 }
40 else
41 {
42 btScalar rlen = btScalar(1.) / btSqrt(lenSqr);
43 vec *= rlen;
44 }
45
46 btVector3 vtx;
47 btScalar newDot;
48
49 {
50 btVector3 pos(0, 0, 0);
51 pos[getUpAxis()] = getHalfHeight();
52
53 vtx = pos;
54 newDot = vec.dot(vtx);
55 if (newDot > maxDot)
56 {
57 maxDot = newDot;
58 supVec = vtx;
59 }
60 }
61 {
62 btVector3 pos(0, 0, 0);
63 pos[getUpAxis()] = -getHalfHeight();
64
65 vtx = pos;
66 newDot = vec.dot(vtx);
67 if (newDot > maxDot)
68 {
69 maxDot = newDot;
70 supVec = vtx;
71 }
72 }
73
74 return supVec;
75}
76
77 void btCapsuleShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
78{
79 for (int j = 0; j < numVectors; j++)
80 {
81 btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
82 const btVector3& vec = vectors[j];
83
84 btVector3 vtx;
85 btScalar newDot;
86 {
87 btVector3 pos(0, 0, 0);
88 pos[getUpAxis()] = getHalfHeight();
89 vtx = pos;
90 newDot = vec.dot(vtx);
91 if (newDot > maxDot)
92 {
93 maxDot = newDot;
94 supportVerticesOut[j] = vtx;
95 }
96 }
97 {
98 btVector3 pos(0, 0, 0);
99 pos[getUpAxis()] = -getHalfHeight();
100 vtx = pos;
101 newDot = vec.dot(vtx);
102 if (newDot > maxDot)
103 {
104 maxDot = newDot;
105 supportVerticesOut[j] = vtx;
106 }
107 }
108 }
109}
110
111 void btCapsuleShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
112{
113 //as an approximation, take the inertia of the box that bounds the spheres
114
115 btTransform ident;
116 ident.setIdentity();
117
118 btScalar radius = getRadius();
119
120 btVector3 halfExtents(radius, radius, radius);
121 halfExtents[getUpAxis()] += getHalfHeight();
122
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);
130
131 inertia[0] = scaledmass * (y2 + z2);
132 inertia[1] = scaledmass * (x2 + z2);
133 inertia[2] = scaledmass * (x2 + y2);
134}
135
136btCapsuleShapeX::btCapsuleShapeX(btScalar radius, btScalar height)
137{
138 m_collisionMargin = radius;
139 m_upAxis = 0;
140 m_implicitShapeDimensions.setValue(0.5f * height, radius, radius);
141}
142
143btCapsuleShapeZ::btCapsuleShapeZ(btScalar radius, btScalar height)
144{
145 m_collisionMargin = radius;
146 m_upAxis = 2;
147 m_implicitShapeDimensions.setValue(radius, radius, 0.5f * height);
148}
CAPSULE_SHAPE_PROXYTYPE
@ CAPSULE_SHAPE_PROXYTYPE
Definition: btBroadphaseProxy.h:41
btCapsuleShape.h
btQuaternion.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
BT_LARGE_FLOAT
#define BT_LARGE_FLOAT
Definition: btScalar.h:316
btSqrt
btScalar btSqrt(btScalar y)
Definition: btScalar.h:466
btCapsuleShapeX::btCapsuleShapeX
btCapsuleShapeX(btScalar radius, btScalar height)
Definition: btCapsuleShape.cpp:136
btCapsuleShapeZ::btCapsuleShapeZ
btCapsuleShapeZ(btScalar radius, btScalar height)
Definition: btCapsuleShape.cpp:143
btCapsuleShape::btCapsuleShape
btCapsuleShape()
only used for btCapsuleShapeZ and btCapsuleShapeX subclasses.
Definition: btCapsuleShape.h:33
btCapsuleShape::getRadius
btScalar getRadius() const
Definition: btCapsuleShape.h:76
btCapsuleShape::localGetSupportingVertexWithoutMargin
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
btConvexShape Interface
Definition: btCapsuleShape.cpp:28
btCapsuleShape::calculateLocalInertia
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
CollisionShape Interface.
Definition: btCapsuleShape.cpp:111
btCapsuleShape::m_upAxis
int m_upAxis
Definition: btCapsuleShape.h:29
btCapsuleShape::getUpAxis
int getUpAxis() const
Definition: btCapsuleShape.h:71
btCapsuleShape::getHalfHeight
btScalar getHalfHeight() const
Definition: btCapsuleShape.h:82
btCapsuleShape::batchedUnitVectorGetSupportingVertexWithoutMargin
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
Definition: btCapsuleShape.cpp:77
btCollisionShape::m_shapeType
int m_shapeType
Definition: btCollisionShape.h:30
btConvexInternalShape
The btConvexInternalShape is an internal base class, shared by most convex shape implementations.
Definition: btConvexInternalShape.h:30
btConvexInternalShape::m_collisionMargin
btScalar m_collisionMargin
Definition: btConvexInternalShape.h:37
btConvexInternalShape::m_implicitShapeDimensions
btVector3 m_implicitShapeDimensions
Definition: btConvexInternalShape.h:35
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
btTransform::setIdentity
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:167
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:229
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
btVector3::length2
btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:251