1 /*
2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3 Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
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 "btGeneric6DofSpringConstraint.h"
17 #include "BulletDynamics/Dynamics/btRigidBody.h"
18 #include "LinearMath/btTransformUtil.h"
19
20
btGeneric6DofSpringConstraint(btRigidBody & rbA,btRigidBody & rbB,const btTransform & frameInA,const btTransform & frameInB,bool useLinearReferenceFrameA)21 btGeneric6DofSpringConstraint::btGeneric6DofSpringConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB ,bool useLinearReferenceFrameA)
22 : btGeneric6DofConstraint(rbA, rbB, frameInA, frameInB, useLinearReferenceFrameA)
23 {
24 for(int i = 0; i < 6; i++)
25 {
26 m_springEnabled[i] = false;
27 m_equilibriumPoint[i] = btScalar(0.f);
28 m_springStiffness[i] = btScalar(0.f);
29 m_springDamping[i] = btScalar(1.f);
30 }
31 }
32
33
enableSpring(int index,bool onOff)34 void btGeneric6DofSpringConstraint::enableSpring(int index, bool onOff)
35 {
36 btAssert((index >= 0) && (index < 6));
37 m_springEnabled[index] = onOff;
38 if(index < 3)
39 {
40 m_linearLimits.m_enableMotor[index] = onOff;
41 }
42 else
43 {
44 m_angularLimits[index - 3].m_enableMotor = onOff;
45 }
46 }
47
48
49
setStiffness(int index,btScalar stiffness)50 void btGeneric6DofSpringConstraint::setStiffness(int index, btScalar stiffness)
51 {
52 btAssert((index >= 0) && (index < 6));
53 m_springStiffness[index] = stiffness;
54 }
55
56
setDamping(int index,btScalar damping)57 void btGeneric6DofSpringConstraint::setDamping(int index, btScalar damping)
58 {
59 btAssert((index >= 0) && (index < 6));
60 m_springDamping[index] = damping;
61 }
62
63
setEquilibriumPoint()64 void btGeneric6DofSpringConstraint::setEquilibriumPoint()
65 {
66 calculateTransforms();
67 int i;
68
69 for( i = 0; i < 3; i++)
70 {
71 m_equilibriumPoint[i] = m_calculatedLinearDiff[i];
72 }
73 for(i = 0; i < 3; i++)
74 {
75 m_equilibriumPoint[i + 3] = m_calculatedAxisAngleDiff[i];
76 }
77 }
78
79
80
setEquilibriumPoint(int index)81 void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index)
82 {
83 btAssert((index >= 0) && (index < 6));
84 calculateTransforms();
85 if(index < 3)
86 {
87 m_equilibriumPoint[index] = m_calculatedLinearDiff[index];
88 }
89 else
90 {
91 m_equilibriumPoint[index] = m_calculatedAxisAngleDiff[index - 3];
92 }
93 }
94
95
96
internalUpdateSprings(btConstraintInfo2 * info)97 void btGeneric6DofSpringConstraint::internalUpdateSprings(btConstraintInfo2* info)
98 {
99 // it is assumed that calculateTransforms() have been called before this call
100 int i;
101 btVector3 relVel = m_rbB.getLinearVelocity() - m_rbA.getLinearVelocity();
102 for(i = 0; i < 3; i++)
103 {
104 if(m_springEnabled[i])
105 {
106 // get current position of constraint
107 btScalar currPos = m_calculatedLinearDiff[i];
108 // calculate difference
109 btScalar delta = currPos - m_equilibriumPoint[i];
110 // spring force is (delta * m_stiffness) according to Hooke's Law
111 btScalar force = delta * m_springStiffness[i];
112 btScalar velFactor = info->fps * m_springDamping[i] / btScalar(info->m_numIterations);
113 m_linearLimits.m_targetVelocity[i] = velFactor * force;
114 m_linearLimits.m_maxMotorForce[i] = btFabs(force) / info->fps;
115 }
116 }
117 for(i = 0; i < 3; i++)
118 {
119 if(m_springEnabled[i + 3])
120 {
121 // get current position of constraint
122 btScalar currPos = m_calculatedAxisAngleDiff[i];
123 // calculate difference
124 btScalar delta = currPos - m_equilibriumPoint[i+3];
125 // spring force is (-delta * m_stiffness) according to Hooke's Law
126 btScalar force = -delta * m_springStiffness[i+3];
127 btScalar velFactor = info->fps * m_springDamping[i+3] / btScalar(info->m_numIterations);
128 m_angularLimits[i].m_targetVelocity = velFactor * force;
129 m_angularLimits[i].m_maxMotorForce = btFabs(force) / info->fps;
130 }
131 }
132 }
133
134
getInfo2(btConstraintInfo2 * info)135 void btGeneric6DofSpringConstraint::getInfo2(btConstraintInfo2* info)
136 {
137 // this will be called by constraint solver at the constraint setup stage
138 // set current motor parameters
139 internalUpdateSprings(info);
140 // do the rest of job for constraint setup
141 btGeneric6DofConstraint::getInfo2(info);
142 }
143
144
145
146
147