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

Bullet Collision Detection & Physics Library

btConeShape.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 #include "btConeShape.h"
17
18
19
20 btConeShape::btConeShape (btScalar radius,btScalar height): btConvexInternalShape (),
21 m_radius (radius),
22 m_height(height)
23 {
24 m_shapeType = CONE_SHAPE_PROXYTYPE;
25 setConeUpIndex(1);
26 btVector3 halfExtents;
27 m_sinAngle = (m_radius / btSqrt(m_radius * m_radius + m_height * m_height));
28 }
29
30 btConeShapeZ::btConeShapeZ (btScalar radius,btScalar height):
31 btConeShape(radius,height)
32 {
33 setConeUpIndex(2);
34 }
35
36 btConeShapeX::btConeShapeX (btScalar radius,btScalar height):
37 btConeShape(radius,height)
38 {
39 setConeUpIndex(0);
40 }
41
43 void btConeShape::setConeUpIndex(int upIndex)
44 {
45 switch (upIndex)
46 {
47 case 0:
48 m_coneIndices[0] = 1;
49 m_coneIndices[1] = 0;
50 m_coneIndices[2] = 2;
51 break;
52 case 1:
53 m_coneIndices[0] = 0;
54 m_coneIndices[1] = 1;
55 m_coneIndices[2] = 2;
56 break;
57 case 2:
58 m_coneIndices[0] = 0;
59 m_coneIndices[1] = 2;
60 m_coneIndices[2] = 1;
61 break;
62 default:
63 btAssert(0);
64 };
65
66 m_implicitShapeDimensions[m_coneIndices[0]] = m_radius;
67 m_implicitShapeDimensions[m_coneIndices[1]] = m_height;
68 m_implicitShapeDimensions[m_coneIndices[2]] = m_radius;
69 }
70
71 btVector3 btConeShape::coneLocalSupport(const btVector3& v) const
72 {
73
74 btScalar halfHeight = m_height * btScalar(0.5);
75
76 if (v[m_coneIndices[1]] > v.length() * m_sinAngle)
77 {
78 btVector3 tmp;
79
80 tmp[m_coneIndices[0]] = btScalar(0.);
81 tmp[m_coneIndices[1]] = halfHeight;
82 tmp[m_coneIndices[2]] = btScalar(0.);
83 return tmp;
84 }
85 else {
86 btScalar s = btSqrt(v[m_coneIndices[0]] * v[m_coneIndices[0]] + v[m_coneIndices[2]] * v[m_coneIndices[2]]);
87 if (s > SIMD_EPSILON) {
88 btScalar d = m_radius / s;
89 btVector3 tmp;
90 tmp[m_coneIndices[0]] = v[m_coneIndices[0]] * d;
91 tmp[m_coneIndices[1]] = -halfHeight;
92 tmp[m_coneIndices[2]] = v[m_coneIndices[2]] * d;
93 return tmp;
94 }
95 else {
96 btVector3 tmp;
97 tmp[m_coneIndices[0]] = btScalar(0.);
98 tmp[m_coneIndices[1]] = -halfHeight;
99 tmp[m_coneIndices[2]] = btScalar(0.);
100 return tmp;
101 }
102 }
103
104 }
105
106 btVector3 btConeShape::localGetSupportingVertexWithoutMargin(const btVector3& vec) const
107 {
108 return coneLocalSupport(vec);
109 }
110
111 void btConeShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
112 {
113 for (int i=0;i<numVectors;i++)
114 {
115 const btVector3& vec = vectors[i];
116 supportVerticesOut[i] = coneLocalSupport(vec);
117 }
118 }
119
120
121 btVector3 btConeShape::localGetSupportingVertex(const btVector3& vec) const
122 {
123 btVector3 supVertex = coneLocalSupport(vec);
124 if ( getMargin()!=btScalar(0.) )
125 {
126 btVector3 vecnorm = vec;
127 if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
128 {
129 vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
130 }
131 vecnorm.normalize();
132 supVertex+= getMargin() * vecnorm;
133 }
134 return supVertex;
135 }
136
137
138 void btConeShape::setLocalScaling(const btVector3& scaling)
139 {
140 int axis = m_coneIndices[1];
141 int r1 = m_coneIndices[0];
142 int r2 = m_coneIndices[2];
143 m_height *= scaling[axis] / m_localScaling[axis];
144 m_radius *= (scaling[r1] / m_localScaling[r1] + scaling[r2] / m_localScaling[r2]) / 2;
145 m_sinAngle = (m_radius / btSqrt(m_radius * m_radius + m_height * m_height));
146 btConvexInternalShape::setLocalScaling(scaling);
147 }
SIMD_EPSILON
#define SIMD_EPSILON
Definition: btScalar.h:521
CONE_SHAPE_PROXYTYPE
Definition: btBroadphaseProxy.h:43
btConeShape::btConeShape
btConeShape(btScalar radius, btScalar height)
Definition: btConeShape.cpp:20
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:652
btConvexInternalShape
The btConvexInternalShape is an internal base class, shared by most convex shape implementations.
Definition: btConvexInternalShape.h:29
btConeShape::m_coneIndices
int m_coneIndices[3]
Definition: btConeShape.h:30
btSqrt
btScalar btSqrt(btScalar y)
Definition: btScalar.h:444
btAssert
#define btAssert(x)
Definition: btScalar.h:131
btConeShape.h
btConeShape::localGetSupportingVertexWithoutMargin
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
Definition: btConeShape.cpp:106
btConvexInternalShape::m_localScaling
btVector3 m_localScaling
Definition: btConvexInternalShape.h:35
btConeShape::m_height
btScalar m_height
Definition: btConeShape.h:29
btConeShape::setConeUpIndex
void setConeUpIndex(int upIndex)
choose upAxis index
Definition: btConeShape.cpp:43
btConvexInternalShape::m_implicitShapeDimensions
btVector3 m_implicitShapeDimensions
Definition: btConvexInternalShape.h:37
btVector3::normalize
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: btVector3.h:309
btConeShape::m_radius
btScalar m_radius
Definition: btConeShape.h:28
btConeShape::coneLocalSupport
btVector3 coneLocalSupport(const btVector3 &v) const
Definition: btConeShape.cpp:71
btConeShape::batchedUnitVectorGetSupportingVertexWithoutMargin
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
Definition: btConeShape.cpp:111
btConeShape
The btConeShape implements a cone shape primitive, centered around the origin and aligned with the Y ...
Definition: btConeShape.h:23
btConvexInternalShape::getMargin
virtual btScalar getMargin() const
Definition: btConvexInternalShape.h:113
btConeShapeX::btConeShapeX
btConeShapeX(btScalar radius, btScalar height)
Definition: btConeShape.cpp:36
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btConeShape::setLocalScaling
virtual void setLocalScaling(const btVector3 &scaling)
Definition: btConeShape.cpp:138
btConvexInternalShape::setLocalScaling
virtual void setLocalScaling(const btVector3 &scaling)
Definition: btConvexInternalShape.cpp:28
btConeShape::localGetSupportingVertex
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
Definition: btConeShape.cpp:121
btCollisionShape::m_shapeType
int m_shapeType
Definition: btCollisionShape.h:30
btConeShapeZ::btConeShapeZ
btConeShapeZ(btScalar radius, btScalar height)
Definition: btConeShape.cpp:30
btConeShape::m_sinAngle
btScalar m_sinAngle
Definition: btConeShape.h:27
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
btVector3::length
btScalar length() const
Return the length of the vector.
Definition: btVector3.h:263