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 "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h" |
---|
17 | |
---|
18 | btPolyhedralConvexShape::btPolyhedralConvexShape() |
---|
19 | :btConvexInternalShape(), |
---|
20 | m_localAabbMin(1,1,1), |
---|
21 | m_localAabbMax(-1,-1,-1), |
---|
22 | m_isLocalAabbValid(false), |
---|
23 | m_optionalHull(0) |
---|
24 | { |
---|
25 | |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | btVector3 btPolyhedralConvexShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const |
---|
31 | { |
---|
32 | int i; |
---|
33 | btVector3 supVec(0,0,0); |
---|
34 | |
---|
35 | btScalar maxDot(btScalar(-1e30)); |
---|
36 | |
---|
37 | btVector3 vec = vec0; |
---|
38 | btScalar lenSqr = vec.length2(); |
---|
39 | if (lenSqr < btScalar(0.0001)) |
---|
40 | { |
---|
41 | vec.setValue(1,0,0); |
---|
42 | } else |
---|
43 | { |
---|
44 | btScalar rlen = btScalar(1.) / btSqrt(lenSqr ); |
---|
45 | vec *= rlen; |
---|
46 | } |
---|
47 | |
---|
48 | btVector3 vtx; |
---|
49 | btScalar newDot; |
---|
50 | |
---|
51 | for (i=0;i<getNumVertices();i++) |
---|
52 | { |
---|
53 | getVertex(i,vtx); |
---|
54 | newDot = vec.dot(vtx); |
---|
55 | if (newDot > maxDot) |
---|
56 | { |
---|
57 | maxDot = newDot; |
---|
58 | supVec = vtx; |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | return supVec; |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | void btPolyhedralConvexShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const |
---|
67 | { |
---|
68 | int i; |
---|
69 | |
---|
70 | btVector3 vtx; |
---|
71 | btScalar newDot; |
---|
72 | |
---|
73 | for (i=0;i<numVectors;i++) |
---|
74 | { |
---|
75 | supportVerticesOut[i][3] = btScalar(-1e30); |
---|
76 | } |
---|
77 | |
---|
78 | for (int j=0;j<numVectors;j++) |
---|
79 | { |
---|
80 | |
---|
81 | const btVector3& vec = vectors[j]; |
---|
82 | |
---|
83 | for (i=0;i<getNumVertices();i++) |
---|
84 | { |
---|
85 | getVertex(i,vtx); |
---|
86 | newDot = vec.dot(vtx); |
---|
87 | if (newDot > supportVerticesOut[j][3]) |
---|
88 | { |
---|
89 | //WARNING: don't swap next lines, the w component would get overwritten! |
---|
90 | supportVerticesOut[j] = vtx; |
---|
91 | supportVerticesOut[j][3] = newDot; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | void btPolyhedralConvexShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const |
---|
100 | { |
---|
101 | //not yet, return box inertia |
---|
102 | |
---|
103 | btScalar margin = getMargin(); |
---|
104 | |
---|
105 | btTransform ident; |
---|
106 | ident.setIdentity(); |
---|
107 | btVector3 aabbMin,aabbMax; |
---|
108 | getAabb(ident,aabbMin,aabbMax); |
---|
109 | btVector3 halfExtents = (aabbMax-aabbMin)*btScalar(0.5); |
---|
110 | |
---|
111 | btScalar lx=btScalar(2.)*(halfExtents.x()+margin); |
---|
112 | btScalar ly=btScalar(2.)*(halfExtents.y()+margin); |
---|
113 | btScalar lz=btScalar(2.)*(halfExtents.z()+margin); |
---|
114 | const btScalar x2 = lx*lx; |
---|
115 | const btScalar y2 = ly*ly; |
---|
116 | const btScalar z2 = lz*lz; |
---|
117 | const btScalar scaledmass = mass * btScalar(0.08333333); |
---|
118 | |
---|
119 | inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2)); |
---|
120 | |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | void btPolyhedralConvexShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const |
---|
126 | { |
---|
127 | getNonvirtualAabb(trans,aabbMin,aabbMax,getMargin()); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | void btPolyhedralConvexShape::setLocalScaling(const btVector3& scaling) |
---|
133 | { |
---|
134 | btConvexInternalShape::setLocalScaling(scaling); |
---|
135 | recalcLocalAabb(); |
---|
136 | } |
---|
137 | |
---|
138 | void btPolyhedralConvexShape::recalcLocalAabb() |
---|
139 | { |
---|
140 | m_isLocalAabbValid = true; |
---|
141 | |
---|
142 | #if 1 |
---|
143 | static const btVector3 _directions[] = |
---|
144 | { |
---|
145 | btVector3( 1., 0., 0.), |
---|
146 | btVector3( 0., 1., 0.), |
---|
147 | btVector3( 0., 0., 1.), |
---|
148 | btVector3( -1., 0., 0.), |
---|
149 | btVector3( 0., -1., 0.), |
---|
150 | btVector3( 0., 0., -1.) |
---|
151 | }; |
---|
152 | |
---|
153 | btVector3 _supporting[] = |
---|
154 | { |
---|
155 | btVector3( 0., 0., 0.), |
---|
156 | btVector3( 0., 0., 0.), |
---|
157 | btVector3( 0., 0., 0.), |
---|
158 | btVector3( 0., 0., 0.), |
---|
159 | btVector3( 0., 0., 0.), |
---|
160 | btVector3( 0., 0., 0.) |
---|
161 | }; |
---|
162 | |
---|
163 | batchedUnitVectorGetSupportingVertexWithoutMargin(_directions, _supporting, 6); |
---|
164 | |
---|
165 | for ( int i = 0; i < 3; ++i ) |
---|
166 | { |
---|
167 | m_localAabbMax[i] = _supporting[i][i] + m_collisionMargin; |
---|
168 | m_localAabbMin[i] = _supporting[i + 3][i] - m_collisionMargin; |
---|
169 | } |
---|
170 | |
---|
171 | #else |
---|
172 | |
---|
173 | for (int i=0;i<3;i++) |
---|
174 | { |
---|
175 | btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.)); |
---|
176 | vec[i] = btScalar(1.); |
---|
177 | btVector3 tmp = localGetSupportingVertex(vec); |
---|
178 | m_localAabbMax[i] = tmp[i]+m_collisionMargin; |
---|
179 | vec[i] = btScalar(-1.); |
---|
180 | tmp = localGetSupportingVertex(vec); |
---|
181 | m_localAabbMin[i] = tmp[i]-m_collisionMargin; |
---|
182 | } |
---|
183 | #endif |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | |
---|