Blender V4.3
btGeneric6DofSpringConstraint.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
19
20btGeneric6DofSpringConstraint::btGeneric6DofSpringConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB, bool useLinearReferenceFrameA)
21 : btGeneric6DofConstraint(rbA, rbB, frameInA, frameInB, useLinearReferenceFrameA)
22{
23 init();
24}
25
26btGeneric6DofSpringConstraint::btGeneric6DofSpringConstraint(btRigidBody& rbB, const btTransform& frameInB, bool useLinearReferenceFrameB)
27 : btGeneric6DofConstraint(rbB, frameInB, useLinearReferenceFrameB)
28{
29 init();
30}
31
32void btGeneric6DofSpringConstraint::init()
33{
34 m_objectType = D6_SPRING_CONSTRAINT_TYPE;
35
36 for (int i = 0; i < 6; i++)
37 {
38 m_springEnabled[i] = false;
41 m_springDamping[i] = btScalar(1.f);
42 }
43}
44
45void btGeneric6DofSpringConstraint::enableSpring(int index, bool onOff)
46{
47 btAssert((index >= 0) && (index < 6));
48 m_springEnabled[index] = onOff;
49 if (index < 3)
50 {
51 m_linearLimits.m_enableMotor[index] = onOff;
52 }
53 else
54 {
55 m_angularLimits[index - 3].m_enableMotor = onOff;
56 }
57}
58
59void btGeneric6DofSpringConstraint::setStiffness(int index, btScalar stiffness)
60{
61 btAssert((index >= 0) && (index < 6));
62 m_springStiffness[index] = stiffness;
63}
64
65void btGeneric6DofSpringConstraint::setDamping(int index, btScalar damping)
66{
67 btAssert((index >= 0) && (index < 6));
68 m_springDamping[index] = damping;
69}
70
71void btGeneric6DofSpringConstraint::setEquilibriumPoint()
72{
74 int i;
75
76 for (i = 0; i < 3; i++)
77 {
79 }
80 for (i = 0; i < 3; i++)
81 {
83 }
84}
85
86void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index)
87{
88 btAssert((index >= 0) && (index < 6));
90 if (index < 3)
91 {
93 }
94 else
95 {
97 }
98}
99
100void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index, btScalar val)
101{
102 btAssert((index >= 0) && (index < 6));
103 m_equilibriumPoint[index] = val;
104}
105
106void btGeneric6DofSpringConstraint::internalUpdateSprings(btConstraintInfo2* info)
107{
108 // it is assumed that calculateTransforms() have been called before this call
109 int i;
110 //btVector3 relVel = m_rbB.getLinearVelocity() - m_rbA.getLinearVelocity();
111 for (i = 0; i < 3; i++)
112 {
113 if (m_springEnabled[i])
114 {
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;
124 }
125 }
126 for (i = 0; i < 3; i++)
127 {
128 if (m_springEnabled[i + 3])
129 {
130 // get current position of constraint
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;
139 }
140 }
141}
142
143void btGeneric6DofSpringConstraint::getInfo2(btConstraintInfo2* info)
144{
145 // this will be called by constraint solver at the constraint setup stage
146 // set current motor parameters
148 // do the rest of job for constraint setup
149 btGeneric6DofConstraint::getInfo2(info);
150}
151
152void btGeneric6DofSpringConstraint::setAxis(const btVector3& axis1, const btVector3& axis2)
153{
154 btVector3 zAxis = axis1.normalized();
155 btVector3 yAxis = axis2.normalized();
156 btVector3 xAxis = yAxis.cross(zAxis); // we want right coordinate system
157
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]);
163
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;
167
169}
void init()
btFixedConstraint btRigidBody & rbB
btFixedConstraint btRigidBody const btTransform & frameInA
btFixedConstraint btRigidBody const btTransform const btTransform & frameInB
btVector3 m_calculatedLinearDiff
btRotationalLimitMotor m_angularLimits[3]
btGeneric6DofConstraint(btRigidBody &rbA, btRigidBody &rbB, const btTransform &frameInA, const btTransform &frameInB, bool useLinearReferenceFrameA)
btTranslationalLimitMotor m_linearLimits
btTransform m_frameInB
btVector3 m_calculatedAxisAngleDiff
void calculateTransforms()
btScalar m_equilibriumPoint[6]
btScalar m_springDamping[6]
void internalUpdateSprings(btConstraintInfo2 *info)
btScalar m_springStiffness[6]
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
SIMD_FORCE_INLINE btScalar btFabs(btScalar x)
Definition btScalar.h:497
#define btAssert(x)
Definition btScalar.h:295
btTransform m_frameInA
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition btTransform.h:30
btRigidBody & m_rbA
btRigidBody & m_rbB
@ D6_SPRING_CONSTRAINT_TYPE
const btTransform & getCenterOfMassTransform() const
btScalar m_targetVelocity
target motor velocity
btScalar m_maxMotorForce
max force on motor
btVector3 m_maxMotorForce
max force on motor
btVector3 m_targetVelocity
target motor velocity