1 | /* |
---|
2 | Bullet Continuous Collision Detection and Physics Library |
---|
3 | Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
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 | #include "LinearMath/btPoint3.h" |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | btConeShape::btConeShape (btScalar radius,btScalar height): btConvexInternalShape (), |
---|
22 | m_radius (radius), |
---|
23 | m_height(height) |
---|
24 | { |
---|
25 | m_shapeType = CONE_SHAPE_PROXYTYPE; |
---|
26 | setConeUpIndex(1); |
---|
27 | btVector3 halfExtents; |
---|
28 | m_sinAngle = (m_radius / btSqrt(m_radius * m_radius + m_height * m_height)); |
---|
29 | } |
---|
30 | |
---|
31 | btConeShapeZ::btConeShapeZ (btScalar radius,btScalar height): |
---|
32 | btConeShape(radius,height) |
---|
33 | { |
---|
34 | setConeUpIndex(2); |
---|
35 | } |
---|
36 | |
---|
37 | btConeShapeX::btConeShapeX (btScalar radius,btScalar height): |
---|
38 | btConeShape(radius,height) |
---|
39 | { |
---|
40 | setConeUpIndex(0); |
---|
41 | } |
---|
42 | |
---|
43 | ///choose upAxis index |
---|
44 | void btConeShape::setConeUpIndex(int upIndex) |
---|
45 | { |
---|
46 | switch (upIndex) |
---|
47 | { |
---|
48 | case 0: |
---|
49 | m_coneIndices[0] = 1; |
---|
50 | m_coneIndices[1] = 0; |
---|
51 | m_coneIndices[2] = 2; |
---|
52 | break; |
---|
53 | case 1: |
---|
54 | m_coneIndices[0] = 0; |
---|
55 | m_coneIndices[1] = 1; |
---|
56 | m_coneIndices[2] = 2; |
---|
57 | break; |
---|
58 | case 2: |
---|
59 | m_coneIndices[0] = 0; |
---|
60 | m_coneIndices[1] = 2; |
---|
61 | m_coneIndices[2] = 1; |
---|
62 | break; |
---|
63 | default: |
---|
64 | assert(0); |
---|
65 | }; |
---|
66 | } |
---|
67 | |
---|
68 | btVector3 btConeShape::coneLocalSupport(const btVector3& v) const |
---|
69 | { |
---|
70 | |
---|
71 | btScalar halfHeight = m_height * btScalar(0.5); |
---|
72 | |
---|
73 | if (v[m_coneIndices[1]] > v.length() * m_sinAngle) |
---|
74 | { |
---|
75 | btVector3 tmp; |
---|
76 | |
---|
77 | tmp[m_coneIndices[0]] = btScalar(0.); |
---|
78 | tmp[m_coneIndices[1]] = halfHeight; |
---|
79 | tmp[m_coneIndices[2]] = btScalar(0.); |
---|
80 | return tmp; |
---|
81 | } |
---|
82 | else { |
---|
83 | btScalar s = btSqrt(v[m_coneIndices[0]] * v[m_coneIndices[0]] + v[m_coneIndices[2]] * v[m_coneIndices[2]]); |
---|
84 | if (s > SIMD_EPSILON) { |
---|
85 | btScalar d = m_radius / s; |
---|
86 | btVector3 tmp; |
---|
87 | tmp[m_coneIndices[0]] = v[m_coneIndices[0]] * d; |
---|
88 | tmp[m_coneIndices[1]] = -halfHeight; |
---|
89 | tmp[m_coneIndices[2]] = v[m_coneIndices[2]] * d; |
---|
90 | return tmp; |
---|
91 | } |
---|
92 | else { |
---|
93 | btVector3 tmp; |
---|
94 | tmp[m_coneIndices[0]] = btScalar(0.); |
---|
95 | tmp[m_coneIndices[1]] = -halfHeight; |
---|
96 | tmp[m_coneIndices[2]] = btScalar(0.); |
---|
97 | return tmp; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | btVector3 btConeShape::localGetSupportingVertexWithoutMargin(const btVector3& vec) const |
---|
104 | { |
---|
105 | return coneLocalSupport(vec); |
---|
106 | } |
---|
107 | |
---|
108 | void btConeShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const |
---|
109 | { |
---|
110 | for (int i=0;i<numVectors;i++) |
---|
111 | { |
---|
112 | const btVector3& vec = vectors[i]; |
---|
113 | supportVerticesOut[i] = coneLocalSupport(vec); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | btVector3 btConeShape::localGetSupportingVertex(const btVector3& vec) const |
---|
119 | { |
---|
120 | btVector3 supVertex = coneLocalSupport(vec); |
---|
121 | if ( getMargin()!=btScalar(0.) ) |
---|
122 | { |
---|
123 | btVector3 vecnorm = vec; |
---|
124 | if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON)) |
---|
125 | { |
---|
126 | vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.)); |
---|
127 | } |
---|
128 | vecnorm.normalize(); |
---|
129 | supVertex+= getMargin() * vecnorm; |
---|
130 | } |
---|
131 | return supVertex; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|