Bullet Collision Detection & Physics Library
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
19
21{
22 m_collisionMargin = radius;
24 m_upAxis = 1;
25 m_implicitShapeDimensions.setValue(radius, 0.5f * height, radius);
26}
27
29{
30 btVector3 supVec(0, 0, 0);
31
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
77void btCapsuleShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
78{
79 for (int j = 0; j < numVectors; j++)
80 {
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
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
137{
138 m_collisionMargin = radius;
139 m_upAxis = 0;
140 m_implicitShapeDimensions.setValue(0.5f * height, radius, radius);
141}
142
144{
145 m_collisionMargin = radius;
146 m_upAxis = 2;
147 m_implicitShapeDimensions.setValue(radius, radius, 0.5f * height);
148}
@ CAPSULE_SHAPE_PROXYTYPE
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define BT_LARGE_FLOAT
Definition: btScalar.h:316
btScalar btSqrt(btScalar y)
Definition: btScalar.h:466
btCapsuleShapeX(btScalar radius, btScalar height)
btCapsuleShapeZ(btScalar radius, btScalar height)
btCapsuleShape()
only used for btCapsuleShapeZ and btCapsuleShapeX subclasses.
btScalar getRadius() const
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
btConvexShape Interface
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
CollisionShape Interface.
int getUpAxis() const
btScalar getHalfHeight() const
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
The btConvexInternalShape is an internal base class, shared by most convex shape implementations.
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:167
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:229
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:251