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
btConeShape(btScalar radius,btScalar height)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
btConeShapeZ(btScalar radius,btScalar height)30 btConeShapeZ::btConeShapeZ (btScalar radius,btScalar height):
31 btConeShape(radius,height)
32 {
33 setConeUpIndex(2);
34 }
35
btConeShapeX(btScalar radius,btScalar height)36 btConeShapeX::btConeShapeX (btScalar radius,btScalar height):
37 btConeShape(radius,height)
38 {
39 setConeUpIndex(0);
40 }
41
42 ///choose upAxis index
setConeUpIndex(int upIndex)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
coneLocalSupport(const btVector3 & v) const71 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
localGetSupportingVertexWithoutMargin(const btVector3 & vec) const106 btVector3 btConeShape::localGetSupportingVertexWithoutMargin(const btVector3& vec) const
107 {
108 return coneLocalSupport(vec);
109 }
110
batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 * vectors,btVector3 * supportVerticesOut,int numVectors) const111 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
localGetSupportingVertex(const btVector3 & vec) const121 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
setLocalScaling(const btVector3 & scaling)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 }