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 "btCylinderShape.h" |
---|
17 | |
---|
18 | btCylinderShape::btCylinderShape (const btVector3& halfExtents) |
---|
19 | :btConvexInternalShape(), |
---|
20 | m_upAxis(1) |
---|
21 | { |
---|
22 | btVector3 margin(getMargin(),getMargin(),getMargin()); |
---|
23 | m_implicitShapeDimensions = (halfExtents * m_localScaling) - margin; |
---|
24 | m_shapeType = CYLINDER_SHAPE_PROXYTYPE; |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | btCylinderShapeX::btCylinderShapeX (const btVector3& halfExtents) |
---|
29 | :btCylinderShape(halfExtents) |
---|
30 | { |
---|
31 | m_upAxis = 0; |
---|
32 | |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | btCylinderShapeZ::btCylinderShapeZ (const btVector3& halfExtents) |
---|
37 | :btCylinderShape(halfExtents) |
---|
38 | { |
---|
39 | m_upAxis = 2; |
---|
40 | |
---|
41 | } |
---|
42 | |
---|
43 | void btCylinderShape::getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const |
---|
44 | { |
---|
45 | btTransformAabb(getHalfExtentsWithoutMargin(),getMargin(),t,aabbMin,aabbMax); |
---|
46 | } |
---|
47 | |
---|
48 | void btCylinderShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const |
---|
49 | { |
---|
50 | //approximation of box shape, todo: implement cylinder shape inertia before people notice ;-) |
---|
51 | btVector3 halfExtents = getHalfExtentsWithMargin(); |
---|
52 | |
---|
53 | btScalar lx=btScalar(2.)*(halfExtents.x()); |
---|
54 | btScalar ly=btScalar(2.)*(halfExtents.y()); |
---|
55 | btScalar lz=btScalar(2.)*(halfExtents.z()); |
---|
56 | |
---|
57 | inertia.setValue(mass/(btScalar(12.0)) * (ly*ly + lz*lz), |
---|
58 | mass/(btScalar(12.0)) * (lx*lx + lz*lz), |
---|
59 | mass/(btScalar(12.0)) * (lx*lx + ly*ly)); |
---|
60 | |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | SIMD_FORCE_INLINE btVector3 CylinderLocalSupportX(const btVector3& halfExtents,const btVector3& v) |
---|
65 | { |
---|
66 | const int cylinderUpAxis = 0; |
---|
67 | const int XX = 1; |
---|
68 | const int YY = 0; |
---|
69 | const int ZZ = 2; |
---|
70 | |
---|
71 | //mapping depends on how cylinder local orientation is |
---|
72 | // extents of the cylinder is: X,Y is for radius, and Z for height |
---|
73 | |
---|
74 | |
---|
75 | btScalar radius = halfExtents[XX]; |
---|
76 | btScalar halfHeight = halfExtents[cylinderUpAxis]; |
---|
77 | |
---|
78 | |
---|
79 | btVector3 tmp; |
---|
80 | btScalar d ; |
---|
81 | |
---|
82 | btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]); |
---|
83 | if (s != btScalar(0.0)) |
---|
84 | { |
---|
85 | d = radius / s; |
---|
86 | tmp[XX] = v[XX] * d; |
---|
87 | tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight; |
---|
88 | tmp[ZZ] = v[ZZ] * d; |
---|
89 | return tmp; |
---|
90 | } |
---|
91 | else |
---|
92 | { |
---|
93 | tmp[XX] = radius; |
---|
94 | tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight; |
---|
95 | tmp[ZZ] = btScalar(0.0); |
---|
96 | return tmp; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | inline btVector3 CylinderLocalSupportY(const btVector3& halfExtents,const btVector3& v) |
---|
108 | { |
---|
109 | |
---|
110 | const int cylinderUpAxis = 1; |
---|
111 | const int XX = 0; |
---|
112 | const int YY = 1; |
---|
113 | const int ZZ = 2; |
---|
114 | |
---|
115 | |
---|
116 | btScalar radius = halfExtents[XX]; |
---|
117 | btScalar halfHeight = halfExtents[cylinderUpAxis]; |
---|
118 | |
---|
119 | |
---|
120 | btVector3 tmp; |
---|
121 | btScalar d ; |
---|
122 | |
---|
123 | btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]); |
---|
124 | if (s != btScalar(0.0)) |
---|
125 | { |
---|
126 | d = radius / s; |
---|
127 | tmp[XX] = v[XX] * d; |
---|
128 | tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight; |
---|
129 | tmp[ZZ] = v[ZZ] * d; |
---|
130 | return tmp; |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | tmp[XX] = radius; |
---|
135 | tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight; |
---|
136 | tmp[ZZ] = btScalar(0.0); |
---|
137 | return tmp; |
---|
138 | } |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | inline btVector3 CylinderLocalSupportZ(const btVector3& halfExtents,const btVector3& v) |
---|
143 | { |
---|
144 | const int cylinderUpAxis = 2; |
---|
145 | const int XX = 0; |
---|
146 | const int YY = 2; |
---|
147 | const int ZZ = 1; |
---|
148 | |
---|
149 | //mapping depends on how cylinder local orientation is |
---|
150 | // extents of the cylinder is: X,Y is for radius, and Z for height |
---|
151 | |
---|
152 | |
---|
153 | btScalar radius = halfExtents[XX]; |
---|
154 | btScalar halfHeight = halfExtents[cylinderUpAxis]; |
---|
155 | |
---|
156 | |
---|
157 | btVector3 tmp; |
---|
158 | btScalar d ; |
---|
159 | |
---|
160 | btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]); |
---|
161 | if (s != btScalar(0.0)) |
---|
162 | { |
---|
163 | d = radius / s; |
---|
164 | tmp[XX] = v[XX] * d; |
---|
165 | tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight; |
---|
166 | tmp[ZZ] = v[ZZ] * d; |
---|
167 | return tmp; |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | tmp[XX] = radius; |
---|
172 | tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight; |
---|
173 | tmp[ZZ] = btScalar(0.0); |
---|
174 | return tmp; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | } |
---|
179 | |
---|
180 | btVector3 btCylinderShapeX::localGetSupportingVertexWithoutMargin(const btVector3& vec)const |
---|
181 | { |
---|
182 | return CylinderLocalSupportX(getHalfExtentsWithoutMargin(),vec); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | btVector3 btCylinderShapeZ::localGetSupportingVertexWithoutMargin(const btVector3& vec)const |
---|
187 | { |
---|
188 | return CylinderLocalSupportZ(getHalfExtentsWithoutMargin(),vec); |
---|
189 | } |
---|
190 | btVector3 btCylinderShape::localGetSupportingVertexWithoutMargin(const btVector3& vec)const |
---|
191 | { |
---|
192 | return CylinderLocalSupportY(getHalfExtentsWithoutMargin(),vec); |
---|
193 | } |
---|
194 | |
---|
195 | void btCylinderShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const |
---|
196 | { |
---|
197 | for (int i=0;i<numVectors;i++) |
---|
198 | { |
---|
199 | supportVerticesOut[i] = CylinderLocalSupportY(getHalfExtentsWithoutMargin(),vectors[i]); |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | void btCylinderShapeZ::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const |
---|
204 | { |
---|
205 | for (int i=0;i<numVectors;i++) |
---|
206 | { |
---|
207 | supportVerticesOut[i] = CylinderLocalSupportZ(getHalfExtentsWithoutMargin(),vectors[i]); |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | void btCylinderShapeX::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const |
---|
215 | { |
---|
216 | for (int i=0;i<numVectors;i++) |
---|
217 | { |
---|
218 | supportVerticesOut[i] = CylinderLocalSupportX(getHalfExtentsWithoutMargin(),vectors[i]); |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | |
---|