2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3 Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
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:
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.
16 #include "btGeneric6DofSpringConstraint.h"
17 #include "BulletDynamics/Dynamics/btRigidBody.h"
18 #include "LinearMath/btTransformUtil.h"
20 btGeneric6DofSpringConstraint::btGeneric6DofSpringConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB, bool useLinearReferenceFrameA)
21 : btGeneric6DofConstraint(rbA, rbB, frameInA, frameInB, useLinearReferenceFrameA)
26 btGeneric6DofSpringConstraint::btGeneric6DofSpringConstraint(btRigidBody& rbB, const btTransform& frameInB, bool useLinearReferenceFrameB)
27 : btGeneric6DofConstraint(rbB, frameInB, useLinearReferenceFrameB)
32 void btGeneric6DofSpringConstraint::init()
34 m_objectType = D6_SPRING_CONSTRAINT_TYPE;
36 for (int i = 0; i < 6; i++)
38 m_springEnabled[i] = false;
39 m_equilibriumPoint[i] = btScalar(0.f);
40 m_springStiffness[i] = btScalar(0.f);
41 m_springDamping[i] = btScalar(1.f);
45 void btGeneric6DofSpringConstraint::enableSpring(int index, bool onOff)
47 btAssert((index >= 0) && (index < 6));
48 m_springEnabled[index] = onOff;
51 m_linearLimits.m_enableMotor[index] = onOff;
55 m_angularLimits[index - 3].m_enableMotor = onOff;
59 void btGeneric6DofSpringConstraint::setStiffness(int index, btScalar stiffness)
61 btAssert((index >= 0) && (index < 6));
62 m_springStiffness[index] = stiffness;
65 void btGeneric6DofSpringConstraint::setDamping(int index, btScalar damping)
67 btAssert((index >= 0) && (index < 6));
68 m_springDamping[index] = damping;
71 void btGeneric6DofSpringConstraint::setEquilibriumPoint()
73 calculateTransforms();
76 for (i = 0; i < 3; i++)
78 m_equilibriumPoint[i] = m_calculatedLinearDiff[i];
80 for (i = 0; i < 3; i++)
82 m_equilibriumPoint[i + 3] = m_calculatedAxisAngleDiff[i];
86 void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index)
88 btAssert((index >= 0) && (index < 6));
89 calculateTransforms();
92 m_equilibriumPoint[index] = m_calculatedLinearDiff[index];
96 m_equilibriumPoint[index] = m_calculatedAxisAngleDiff[index - 3];
100 void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index, btScalar val)
102 btAssert((index >= 0) && (index < 6));
103 m_equilibriumPoint[index] = val;
106 void btGeneric6DofSpringConstraint::internalUpdateSprings(btConstraintInfo2* info)
108 // it is assumed that calculateTransforms() have been called before this call
110 //btVector3 relVel = m_rbB.getLinearVelocity() - m_rbA.getLinearVelocity();
111 for (i = 0; i < 3; i++)
113 if (m_springEnabled[i])
115 // get current position of constraint
116 btScalar currPos = m_calculatedLinearDiff[i];
117 // calculate difference
118 btScalar delta = currPos - m_equilibriumPoint[i];
119 // spring force is (delta * m_stiffness) according to Hooke's Law
120 btScalar force = delta * m_springStiffness[i];
121 btScalar velFactor = info->fps * m_springDamping[i] / btScalar(info->m_numIterations);
122 m_linearLimits.m_targetVelocity[i] = velFactor * force;
123 m_linearLimits.m_maxMotorForce[i] = btFabs(force);
126 for (i = 0; i < 3; i++)
128 if (m_springEnabled[i + 3])
130 // get current position of constraint
131 btScalar currPos = m_calculatedAxisAngleDiff[i];
132 // calculate difference
133 btScalar delta = currPos - m_equilibriumPoint[i + 3];
134 // spring force is (-delta * m_stiffness) according to Hooke's Law
135 btScalar force = -delta * m_springStiffness[i + 3];
136 btScalar velFactor = info->fps * m_springDamping[i + 3] / btScalar(info->m_numIterations);
137 m_angularLimits[i].m_targetVelocity = velFactor * force;
138 m_angularLimits[i].m_maxMotorForce = btFabs(force);
143 void btGeneric6DofSpringConstraint::getInfo2(btConstraintInfo2* info)
145 // this will be called by constraint solver at the constraint setup stage
146 // set current motor parameters
147 internalUpdateSprings(info);
148 // do the rest of job for constraint setup
149 btGeneric6DofConstraint::getInfo2(info);
152 void btGeneric6DofSpringConstraint::setAxis(const btVector3& axis1, const btVector3& axis2)
154 btVector3 zAxis = axis1.normalized();
155 btVector3 yAxis = axis2.normalized();
156 btVector3 xAxis = yAxis.cross(zAxis); // we want right coordinate system
158 btTransform frameInW;
159 frameInW.setIdentity();
160 frameInW.getBasis().setValue(xAxis[0], yAxis[0], zAxis[0],
161 xAxis[1], yAxis[1], zAxis[1],
162 xAxis[2], yAxis[2], zAxis[2]);
164 // now get constraint frame in local coordinate systems
165 m_frameInA = m_rbA.getCenterOfMassTransform().inverse() * frameInW;
166 m_frameInB = m_rbB.getCenterOfMassTransform().inverse() * frameInW;
168 calculateTransforms();