added anisotropic friction support for Bullet. Both for static and dynamic objects
[blender-staging.git] / extern / bullet2 / src / BulletDynamics / Dynamics / btRigidBody.cpp
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 "btRigidBody.h"
17 #include "BulletCollision/CollisionShapes/btConvexShape.h"
18 #include "LinearMath/btMinMax.h"
19 #include "LinearMath/btTransformUtil.h"
20 #include "LinearMath/btMotionState.h"
21 #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
22
23 //'temporarily' global variables
24 btScalar        gDeactivationTime = btScalar(2.);
25 bool    gDisableDeactivation = false;
26 static int uniqueId = 0;
27
28
29 btRigidBody::btRigidBody(const btRigidBody::btRigidBodyConstructionInfo& constructionInfo)
30 {
31         setupRigidBody(constructionInfo);
32 }
33
34 btRigidBody::btRigidBody(btScalar mass, btMotionState *motionState, btCollisionShape *collisionShape, const btVector3 &localInertia)
35 {
36         btRigidBodyConstructionInfo cinfo(mass,motionState,collisionShape,localInertia);
37         setupRigidBody(cinfo);
38 }
39
40 void    btRigidBody::setupRigidBody(const btRigidBody::btRigidBodyConstructionInfo& constructionInfo)
41 {
42
43         m_internalType=CO_RIGID_BODY;
44
45         m_linearVelocity.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0));
46         m_angularVelocity.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
47         m_angularFactor = btScalar(1.);
48         m_anisotropicFriction.setValue(1.f,1.f,1.f);
49         m_gravity.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0));
50         m_totalForce.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0));
51         m_totalTorque.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0)),
52         m_linearDamping = btScalar(0.);
53         m_angularDamping = btScalar(0.5);
54         m_linearSleepingThreshold = constructionInfo.m_linearSleepingThreshold;
55         m_angularSleepingThreshold = constructionInfo.m_angularSleepingThreshold;
56         m_optionalMotionState = constructionInfo.m_motionState;
57         m_contactSolverType = 0;
58         m_frictionSolverType = 0;
59         m_additionalDamping = constructionInfo.m_additionalDamping;
60         m_additionalDampingFactor = constructionInfo.m_additionalDampingFactor;
61         m_additionalLinearDampingThresholdSqr = constructionInfo.m_additionalLinearDampingThresholdSqr;
62         m_additionalAngularDampingThresholdSqr = constructionInfo.m_additionalAngularDampingThresholdSqr;
63         m_additionalAngularDampingFactor = constructionInfo.m_additionalAngularDampingFactor;
64
65         if (m_optionalMotionState)
66         {
67                 m_optionalMotionState->getWorldTransform(m_worldTransform);
68         } else
69         {
70                 m_worldTransform = constructionInfo.m_startWorldTransform;
71         }
72
73         m_interpolationWorldTransform = m_worldTransform;
74         m_interpolationLinearVelocity.setValue(0,0,0);
75         m_interpolationAngularVelocity.setValue(0,0,0);
76         
77         //moved to btCollisionObject
78         m_friction = constructionInfo.m_friction;
79         m_restitution = constructionInfo.m_restitution;
80
81         setCollisionShape( constructionInfo.m_collisionShape );
82         m_debugBodyId = uniqueId++;
83         
84         setMassProps(constructionInfo.m_mass, constructionInfo.m_localInertia);
85     setDamping(constructionInfo.m_linearDamping, constructionInfo.m_angularDamping);
86         updateInertiaTensor();
87
88 }
89
90
91 void btRigidBody::predictIntegratedTransform(btScalar timeStep,btTransform& predictedTransform) 
92 {
93         btTransformUtil::integrateTransform(m_worldTransform,m_linearVelocity,m_angularVelocity,timeStep,predictedTransform);
94 }
95
96 void                    btRigidBody::saveKinematicState(btScalar timeStep)
97 {
98         //todo: clamp to some (user definable) safe minimum timestep, to limit maximum angular/linear velocities
99         if (timeStep != btScalar(0.))
100         {
101                 //if we use motionstate to synchronize world transforms, get the new kinematic/animated world transform
102                 if (getMotionState())
103                         getMotionState()->getWorldTransform(m_worldTransform);
104                 btVector3 linVel,angVel;
105                 
106                 btTransformUtil::calculateVelocity(m_interpolationWorldTransform,m_worldTransform,timeStep,m_linearVelocity,m_angularVelocity);
107                 m_interpolationLinearVelocity = m_linearVelocity;
108                 m_interpolationAngularVelocity = m_angularVelocity;
109                 m_interpolationWorldTransform = m_worldTransform;
110                 //printf("angular = %f %f %f\n",m_angularVelocity.getX(),m_angularVelocity.getY(),m_angularVelocity.getZ());
111         }
112 }
113         
114 void    btRigidBody::getAabb(btVector3& aabbMin,btVector3& aabbMax) const
115 {
116         getCollisionShape()->getAabb(m_worldTransform,aabbMin,aabbMax);
117 }
118
119
120
121
122 void btRigidBody::setGravity(const btVector3& acceleration) 
123 {
124         if (m_inverseMass != btScalar(0.0))
125         {
126                 m_gravity = acceleration * (btScalar(1.0) / m_inverseMass);
127         }
128 }
129
130
131
132
133
134
135 void btRigidBody::setDamping(btScalar lin_damping, btScalar ang_damping)
136 {
137         m_linearDamping = GEN_clamped(lin_damping, (btScalar)btScalar(0.0), (btScalar)btScalar(1.0));
138         m_angularDamping = GEN_clamped(ang_damping, (btScalar)btScalar(0.0), (btScalar)btScalar(1.0));
139 }
140
141
142
143
144 ///applyDamping damps the velocity, using the given m_linearDamping and m_angularDamping
145 void                    btRigidBody::applyDamping(btScalar timeStep)
146 {
147         m_linearVelocity *= GEN_clamped((btScalar(1.) - timeStep * m_linearDamping), (btScalar)btScalar(0.0), (btScalar)btScalar(1.0));
148         m_angularVelocity *= GEN_clamped((btScalar(1.) - timeStep * m_angularDamping), (btScalar)btScalar(0.0), (btScalar)btScalar(1.0));
149
150         if (m_additionalDamping)
151         {
152                 //Additional damping can help avoiding lowpass jitter motion, help stability for ragdolls etc.
153                 //Such damping is undesirable, so once the overall simulation quality of the rigid body dynamics system has improved, this should become obsolete
154                 if ((m_angularVelocity.length2() < m_additionalAngularDampingThresholdSqr) &&
155                         (m_linearVelocity.length2() < m_additionalLinearDampingThresholdSqr))
156                 {
157                         m_angularVelocity *= m_additionalDampingFactor;
158                         m_linearVelocity *= m_additionalDampingFactor;
159                 }
160         
161
162                 btScalar speed = m_linearVelocity.length();
163                 if (speed < m_linearDamping)
164                 {
165                         btScalar dampVel = btScalar(0.005);
166                         if (speed > dampVel)
167                         {
168                                 btVector3 dir = m_linearVelocity.normalized();
169                                 m_linearVelocity -=  dir * dampVel;
170                         } else
171                         {
172                                 m_linearVelocity.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
173                         }
174                 }
175
176                 btScalar angSpeed = m_angularVelocity.length();
177                 if (angSpeed < m_angularDamping)
178                 {
179                         btScalar angDampVel = btScalar(0.005);
180                         if (angSpeed > angDampVel)
181                         {
182                                 btVector3 dir = m_angularVelocity.normalized();
183                                 m_angularVelocity -=  dir * angDampVel;
184                         } else
185                         {
186                                 m_angularVelocity.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
187                         }
188                 }
189         }
190 }
191
192
193 void btRigidBody::applyGravity()
194 {
195         if (isStaticOrKinematicObject())
196                 return;
197         
198         applyCentralForce(m_gravity);   
199
200 }
201
202 void btRigidBody::proceedToTransform(const btTransform& newTrans)
203 {
204         setCenterOfMassTransform( newTrans );
205 }
206         
207
208 void btRigidBody::setMassProps(btScalar mass, const btVector3& inertia)
209 {
210         if (mass == btScalar(0.))
211         {
212                 m_collisionFlags |= btCollisionObject::CF_STATIC_OBJECT;
213                 m_inverseMass = btScalar(0.);
214         } else
215         {
216                 m_collisionFlags &= (~btCollisionObject::CF_STATIC_OBJECT);
217                 m_inverseMass = btScalar(1.0) / mass;
218         }
219         
220         m_invInertiaLocal.setValue(inertia.x() != btScalar(0.0) ? btScalar(1.0) / inertia.x(): btScalar(0.0),
221                                    inertia.y() != btScalar(0.0) ? btScalar(1.0) / inertia.y(): btScalar(0.0),
222                                    inertia.z() != btScalar(0.0) ? btScalar(1.0) / inertia.z(): btScalar(0.0));
223
224 }
225
226         
227
228 void btRigidBody::updateInertiaTensor() 
229 {
230         m_invInertiaTensorWorld = m_worldTransform.getBasis().scaled(m_invInertiaLocal) * m_worldTransform.getBasis().transpose();
231 }
232
233
234 void btRigidBody::integrateVelocities(btScalar step) 
235 {
236         if (isStaticOrKinematicObject())
237                 return;
238
239         m_linearVelocity += m_totalForce * (m_inverseMass * step);
240         m_angularVelocity += m_invInertiaTensorWorld * m_totalTorque * step;
241
242 #define MAX_ANGVEL SIMD_HALF_PI
243         /// clamp angular velocity. collision calculations will fail on higher angular velocities       
244         btScalar angvel = m_angularVelocity.length();
245         if (angvel*step > MAX_ANGVEL)
246         {
247                 m_angularVelocity *= (MAX_ANGVEL/step) /angvel;
248         }
249
250 }
251
252 btQuaternion btRigidBody::getOrientation() const
253 {
254                 btQuaternion orn;
255                 m_worldTransform.getBasis().getRotation(orn);
256                 return orn;
257 }
258         
259         
260 void btRigidBody::setCenterOfMassTransform(const btTransform& xform)
261 {
262
263         if (isStaticOrKinematicObject())
264         {
265                 m_interpolationWorldTransform = m_worldTransform;
266         } else
267         {
268                 m_interpolationWorldTransform = xform;
269         }
270         m_interpolationLinearVelocity = getLinearVelocity();
271         m_interpolationAngularVelocity = getAngularVelocity();
272         m_worldTransform = xform;
273         updateInertiaTensor();
274 }
275
276
277 bool btRigidBody::checkCollideWithOverride(btCollisionObject* co)
278 {
279         btRigidBody* otherRb = btRigidBody::upcast(co);
280         if (!otherRb)
281                 return true;
282
283         for (int i = 0; i < m_constraintRefs.size(); ++i)
284         {
285                 btTypedConstraint* c = m_constraintRefs[i];
286                 if (&c->getRigidBodyA() == otherRb || &c->getRigidBodyB() == otherRb)
287                         return false;
288         }
289
290         return true;
291 }
292
293 void btRigidBody::addConstraintRef(btTypedConstraint* c)
294 {
295         int index = m_constraintRefs.findLinearSearch(c);
296         if (index == m_constraintRefs.size())
297                 m_constraintRefs.push_back(c); 
298
299         m_checkCollideWith = true;
300 }
301
302 void btRigidBody::removeConstraintRef(btTypedConstraint* c)
303 {
304         m_constraintRefs.remove(c);
305         m_checkCollideWith = m_constraintRefs.size() > 0;
306 }