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

Bullet Collision Detection & Physics Library

btCapsuleShape.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
4
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:
10
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.
14 */
15
16
17 #include "btCapsuleShape.h"
18
19 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
20 #include "LinearMath/btQuaternion.h"
21
22 btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height) : btConvexInternalShape ()
23 {
24 m_shapeType = CAPSULE_SHAPE_PROXYTYPE;
25 m_upAxis = 1;
26 m_implicitShapeDimensions.setValue(radius,0.5f*height,radius);
27 }
28
29
30 btVector3 btCapsuleShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
31 {
32
33 btVector3 supVec(0,0,0);
34
35 btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
36
37 btVector3 vec = vec0;
38 btScalar lenSqr = vec.length2();
39 if (lenSqr < btScalar(0.0001))
40 {
41 vec.setValue(1,0,0);
42 } else
43 {
44 btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
45 vec *= rlen;
46 }
47
48 btVector3 vtx;
49 btScalar newDot;
50
51 btScalar radius = getRadius();
52
53
54 {
55 btVector3 pos(0,0,0);
56 pos[getUpAxis()] = getHalfHeight();
57
58 vtx = pos +vec*(radius) - vec * getMargin();
59 newDot = vec.dot(vtx);
60 if (newDot > maxDot)
61 {
62 maxDot = newDot;
63 supVec = vtx;
64 }
65 }
66 {
67 btVector3 pos(0,0,0);
68 pos[getUpAxis()] = -getHalfHeight();
69
70 vtx = pos +vec*(radius) - vec * getMargin();
71 newDot = vec.dot(vtx);
72 if (newDot > maxDot)
73 {
74 maxDot = newDot;
75 supVec = vtx;
76 }
77 }
78
79 return supVec;
80
81 }
82
83 void btCapsuleShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
84 {
85
86
87 btScalar radius = getRadius();
88
89 for (int j=0;j<numVectors;j++)
90 {
91 btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
92 const btVector3& vec = vectors[j];
93
94 btVector3 vtx;
95 btScalar newDot;
96 {
97 btVector3 pos(0,0,0);
98 pos[getUpAxis()] = getHalfHeight();
99 vtx = pos +vec*(radius) - vec * getMargin();
100 newDot = vec.dot(vtx);
101 if (newDot > maxDot)
102 {
103 maxDot = newDot;
104 supportVerticesOut[j] = vtx;
105 }
106 }
107 {
108 btVector3 pos(0,0,0);
109 pos[getUpAxis()] = -getHalfHeight();
110 vtx = pos +vec*(radius) - vec * getMargin();
111 newDot = vec.dot(vtx);
112 if (newDot > maxDot)
113 {
114 maxDot = newDot;
115 supportVerticesOut[j] = vtx;
116 }
117 }
118
119 }
120 }
121
122
123 void btCapsuleShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
124 {
125 //as an approximation, take the inertia of the box that bounds the spheres
126
127 btTransform ident;
128 ident.setIdentity();
129
130
131 btScalar radius = getRadius();
132
133 btVector3 halfExtents(radius,radius,radius);
134 halfExtents[getUpAxis()]+=getHalfHeight();
135
136 btScalar margin = CONVEX_DISTANCE_MARGIN;
137
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);
145
146 inertia[0] = scaledmass * (y2+z2);
147 inertia[1] = scaledmass * (x2+z2);
148 inertia[2] = scaledmass * (x2+y2);
149
150 }
151
152 btCapsuleShapeX::btCapsuleShapeX(btScalar radius,btScalar height)
153 {
154 m_upAxis = 0;
155 m_implicitShapeDimensions.setValue(0.5f*height, radius,radius);
156 }
157
158
159
160
161
162
163 btCapsuleShapeZ::btCapsuleShapeZ(btScalar radius,btScalar height)
164 {
165 m_upAxis = 2;
166 m_implicitShapeDimensions.setValue(radius,radius,0.5f*height);
167 }
168
169
170
171
btCapsuleShapeX::btCapsuleShapeX
btCapsuleShapeX(btScalar radius, btScalar height)
Definition: btCapsuleShape.cpp:152
BT_LARGE_FLOAT
#define BT_LARGE_FLOAT
Definition: btScalar.h:281
CONVEX_DISTANCE_MARGIN
#define CONVEX_DISTANCE_MARGIN
The CONVEX_DISTANCE_MARGIN is a default collision margin for convex collision shapes derived from btC...
Definition: btCollisionMargin.h:22
btCapsuleShape::getRadius
btScalar getRadius() const
Definition: btCapsuleShape.h:84
btCapsuleShape.h
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:650
btVector3::length2
btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:257
CAPSULE_SHAPE_PROXYTYPE
Definition: btBroadphaseProxy.h:42
btConvexInternalShape
The btConvexInternalShape is an internal base class, shared by most convex shape implementations.
Definition: btConvexInternalShape.h:29
btSqrt
btScalar btSqrt(btScalar y)
Definition: btScalar.h:419
btTransform::setIdentity
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:172
btCapsuleShape::m_upAxis
int m_upAxis
Definition: btCapsuleShape.h:29
btCollisionMargin.h
btCapsuleShape::getUpAxis
int getUpAxis() const
Definition: btCapsuleShape.h:79
btConvexInternalShape::m_implicitShapeDimensions
btVector3 m_implicitShapeDimensions
Definition: btConvexInternalShape.h:37
btCapsuleShapeZ::btCapsuleShapeZ
btCapsuleShapeZ(btScalar radius, btScalar height)
Definition: btCapsuleShape.cpp:163
btCapsuleShape::getHalfHeight
btScalar getHalfHeight() const
Definition: btCapsuleShape.h:90
btQuaternion.h
btCapsuleShape::btCapsuleShape
btCapsuleShape()
only used for btCapsuleShapeZ and btCapsuleShapeX subclasses.
Definition: btCapsuleShape.h:33
btConvexInternalShape::getMargin
virtual btScalar getMargin() const
Definition: btConvexInternalShape.h:113
btCapsuleShape::batchedUnitVectorGetSupportingVertexWithoutMargin
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
Definition: btCapsuleShape.cpp:83
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btCapsuleShape::calculateLocalInertia
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
CollisionShape Interface.
Definition: btCapsuleShape.cpp:123
btCapsuleShape::localGetSupportingVertexWithoutMargin
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
btConvexShape Interface
Definition: btCapsuleShape.cpp:30
btCollisionShape::m_shapeType
int m_shapeType
Definition: btCollisionShape.h:30
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:279