2 Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans http://continuousphysics.com/Bullet/
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
10 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
16 #ifndef SIMD_TRANSFORM_UTIL_H
17 #define SIMD_TRANSFORM_UTIL_H
19 #include "SimdTransform.h"
20 #define ANGULAR_MOTION_TRESHOLD 0.5f*SIMD_HALF_PI
24 #define SIMDSQRT12 SimdScalar(0.7071067811865475244008443621048490)
26 #define SimdRecipSqrt(x) ((float)(1.0f/SimdSqrt(float(x)))) /* reciprocal square root */
29 inline void SimdPlaneSpace1 (const SimdVector3& n, SimdVector3& p, SimdVector3& q)
31 if (SimdFabs(n[2]) > SIMDSQRT12) {
32 // choose p in y-z plane
33 SimdScalar a = n[1]*n[1] + n[2]*n[2];
34 SimdScalar k = SimdRecipSqrt (a);
44 // choose p in x-y plane
45 SimdScalar a = n[0]*n[0] + n[1]*n[1];
46 SimdScalar k = SimdRecipSqrt (a);
59 /// Utils related to temporal transforms
60 class SimdTransformUtil
65 static void IntegrateTransform(const SimdTransform& curTrans,const SimdVector3& linvel,const SimdVector3& angvel,SimdScalar timeStep,SimdTransform& predictedTransform)
67 predictedTransform.setOrigin(curTrans.getOrigin() + linvel * timeStep);
68 // #define QUATERNION_DERIVATIVE
69 #ifdef QUATERNION_DERIVATIVE
70 SimdQuaternion orn = curTrans.getRotation();
71 orn += (angvel * orn) * (timeStep * 0.5f);
76 SimdScalar fAngle = angvel.length();
77 //limit the angular motion
78 if (fAngle*timeStep > ANGULAR_MOTION_TRESHOLD)
80 fAngle = ANGULAR_MOTION_TRESHOLD / timeStep;
83 if ( fAngle < 0.001f )
85 // use Taylor's expansions of sync function
86 axis = angvel*( 0.5f*timeStep-(timeStep*timeStep*timeStep)*(0.020833333333f)*fAngle*fAngle );
90 // sync(fAngle) = sin(c*fAngle)/t
91 axis = angvel*( SimdSin(0.5f*fAngle*timeStep)/fAngle );
93 SimdQuaternion dorn (axis.x(),axis.y(),axis.z(),SimdCos( fAngle*timeStep*0.5f ));
94 SimdQuaternion orn0 = curTrans.getRotation();
96 SimdQuaternion predictedOrn = dorn * orn0;
98 predictedTransform.setRotation(predictedOrn);
101 static void CalculateVelocity(const SimdTransform& transform0,const SimdTransform& transform1,SimdScalar timeStep,SimdVector3& linVel,SimdVector3& angVel)
103 linVel = (transform1.getOrigin() - transform0.getOrigin()) / timeStep;
104 #ifdef USE_QUATERNION_DIFF
105 SimdQuaternion orn0 = transform0.getRotation();
106 SimdQuaternion orn1a = transform1.getRotation();
107 SimdQuaternion orn1 = orn0.farthest(orn1a);
108 SimdQuaternion dorn = orn1 * orn0.inverse();
110 SimdMatrix3x3 dmat = transform1.getBasis() * transform0.getBasis().inverse();
112 dmat.getRotation(dorn);
113 #endif//USE_QUATERNION_DIFF
117 angle = dorn.getAngle();
118 axis = SimdVector3(dorn.x(),dorn.y(),dorn.z());
120 //check for axis length
121 SimdScalar len = axis.length2();
122 if (len < SIMD_EPSILON*SIMD_EPSILON)
123 axis = SimdVector3(1.f,0.f,0.f);
125 axis /= SimdSqrt(len);
128 angVel = axis * angle / timeStep;
135 #endif //SIMD_TRANSFORM_UTIL_H