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