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 "btMultiSphereShape.h" |
---|
17 | #include "BulletCollision/CollisionShapes/btCollisionMargin.h" |
---|
18 | #include "LinearMath/btQuaternion.h" |
---|
19 | |
---|
20 | btMultiSphereShape::btMultiSphereShape (const btVector3& inertiaHalfExtents,const btVector3* positions,const btScalar* radi,int numSpheres) |
---|
21 | :btConvexInternalShape (), m_inertiaHalfExtents(inertiaHalfExtents) |
---|
22 | { |
---|
23 | m_shapeType = MULTI_SPHERE_SHAPE_PROXYTYPE; |
---|
24 | btScalar startMargin = btScalar(1e30); |
---|
25 | |
---|
26 | m_numSpheres = numSpheres; |
---|
27 | for (int i=0;i<m_numSpheres;i++) |
---|
28 | { |
---|
29 | m_localPositions[i] = positions[i]; |
---|
30 | m_radi[i] = radi[i]; |
---|
31 | if (radi[i] < startMargin) |
---|
32 | startMargin = radi[i]; |
---|
33 | } |
---|
34 | setMargin(startMargin); |
---|
35 | |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | btVector3 btMultiSphereShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const |
---|
42 | { |
---|
43 | int i; |
---|
44 | btVector3 supVec(0,0,0); |
---|
45 | |
---|
46 | btScalar maxDot(btScalar(-1e30)); |
---|
47 | |
---|
48 | |
---|
49 | btVector3 vec = vec0; |
---|
50 | btScalar lenSqr = vec.length2(); |
---|
51 | if (lenSqr < (SIMD_EPSILON*SIMD_EPSILON)) |
---|
52 | { |
---|
53 | vec.setValue(1,0,0); |
---|
54 | } else |
---|
55 | { |
---|
56 | btScalar rlen = btScalar(1.) / btSqrt(lenSqr ); |
---|
57 | vec *= rlen; |
---|
58 | } |
---|
59 | |
---|
60 | btVector3 vtx; |
---|
61 | btScalar newDot; |
---|
62 | |
---|
63 | const btVector3* pos = &m_localPositions[0]; |
---|
64 | const btScalar* rad = &m_radi[0]; |
---|
65 | |
---|
66 | for (i=0;i<m_numSpheres;i++) |
---|
67 | { |
---|
68 | vtx = (*pos) +vec*m_localScaling*(*rad) - vec * getMargin(); |
---|
69 | pos++; |
---|
70 | rad++; |
---|
71 | newDot = vec.dot(vtx); |
---|
72 | if (newDot > maxDot) |
---|
73 | { |
---|
74 | maxDot = newDot; |
---|
75 | supVec = vtx; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | return supVec; |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | void btMultiSphereShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const |
---|
84 | { |
---|
85 | |
---|
86 | for (int j=0;j<numVectors;j++) |
---|
87 | { |
---|
88 | btScalar maxDot(btScalar(-1e30)); |
---|
89 | |
---|
90 | const btVector3& vec = vectors[j]; |
---|
91 | |
---|
92 | btVector3 vtx; |
---|
93 | btScalar newDot; |
---|
94 | |
---|
95 | const btVector3* pos = &m_localPositions[0]; |
---|
96 | const btScalar* rad = &m_radi[0]; |
---|
97 | |
---|
98 | for (int i=0;i<m_numSpheres;i++) |
---|
99 | { |
---|
100 | vtx = (*pos) +vec*m_localScaling*(*rad) - vec * getMargin(); |
---|
101 | pos++; |
---|
102 | rad++; |
---|
103 | newDot = vec.dot(vtx); |
---|
104 | if (newDot > maxDot) |
---|
105 | { |
---|
106 | maxDot = newDot; |
---|
107 | supportVerticesOut[j] = vtx; |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | void btMultiSphereShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const |
---|
121 | { |
---|
122 | //as an approximation, take the inertia of the box that bounds the spheres |
---|
123 | |
---|
124 | btTransform ident; |
---|
125 | ident.setIdentity(); |
---|
126 | // btVector3 aabbMin,aabbMax; |
---|
127 | |
---|
128 | // getAabb(ident,aabbMin,aabbMax); |
---|
129 | |
---|
130 | btVector3 halfExtents = m_inertiaHalfExtents;//(aabbMax - aabbMin)* btScalar(0.5); |
---|
131 | |
---|
132 | btScalar margin = CONVEX_DISTANCE_MARGIN; |
---|
133 | |
---|
134 | btScalar lx=btScalar(2.)*(halfExtents[0]+margin); |
---|
135 | btScalar ly=btScalar(2.)*(halfExtents[1]+margin); |
---|
136 | btScalar lz=btScalar(2.)*(halfExtents[2]+margin); |
---|
137 | const btScalar x2 = lx*lx; |
---|
138 | const btScalar y2 = ly*ly; |
---|
139 | const btScalar z2 = lz*lz; |
---|
140 | const btScalar scaledmass = mass * btScalar(.08333333); |
---|
141 | |
---|
142 | inertia[0] = scaledmass * (y2+z2); |
---|
143 | inertia[1] = scaledmass * (x2+z2); |
---|
144 | inertia[2] = scaledmass * (x2+y2); |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|