Bullet Collision Detection & Physics Library: btGeneric6DofSpringConstraint.cpp Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btGeneric6DofSpringConstraint.cpp
Go to the documentation of this file.
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
21 btGeneric6DofSpringConstraint::btGeneric6DofSpringConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB ,bool useLinearReferenceFrameA)
22 : btGeneric6DofConstraint(rbA, rbB, frameInA, frameInB, useLinearReferenceFrameA)
23 {
24 init();
25 }
26
27
28 btGeneric6DofSpringConstraint::btGeneric6DofSpringConstraint(btRigidBody& rbB, const btTransform& frameInB, bool useLinearReferenceFrameB)
29 : btGeneric6DofConstraint(rbB, frameInB, useLinearReferenceFrameB)
30 {
31 init();
32 }
33
34
35 void btGeneric6DofSpringConstraint::init()
36 {
37 m_objectType = D6_SPRING_CONSTRAINT_TYPE;
38
39 for(int i = 0; i < 6; i++)
40 {
41 m_springEnabled[i] = false;
42 m_equilibriumPoint[i] = btScalar(0.f);
43 m_springStiffness[i] = btScalar(0.f);
44 m_springDamping[i] = btScalar(1.f);
45 }
46 }
47
48
49 void btGeneric6DofSpringConstraint::enableSpring(int index, bool onOff)
50 {
51 btAssert((index >= 0) && (index < 6));
52 m_springEnabled[index] = onOff;
53 if(index < 3)
54 {
55 m_linearLimits.m_enableMotor[index] = onOff;
56 }
57 else
58 {
59 m_angularLimits[index - 3].m_enableMotor = onOff;
60 }
61 }
62
63
64
65 void btGeneric6DofSpringConstraint::setStiffness(int index, btScalar stiffness)
66 {
67 btAssert((index >= 0) && (index < 6));
68 m_springStiffness[index] = stiffness;
69 }
70
71
72 void btGeneric6DofSpringConstraint::setDamping(int index, btScalar damping)
73 {
74 btAssert((index >= 0) && (index < 6));
75 m_springDamping[index] = damping;
76 }
77
78
79 void btGeneric6DofSpringConstraint::setEquilibriumPoint()
80 {
81 calculateTransforms();
82 int i;
83
84 for( i = 0; i < 3; i++)
85 {
86 m_equilibriumPoint[i] = m_calculatedLinearDiff[i];
87 }
88 for(i = 0; i < 3; i++)
89 {
90 m_equilibriumPoint[i + 3] = m_calculatedAxisAngleDiff[i];
91 }
92 }
93
94
95
96 void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index)
97 {
98 btAssert((index >= 0) && (index < 6));
99 calculateTransforms();
100 if(index < 3)
101 {
102 m_equilibriumPoint[index] = m_calculatedLinearDiff[index];
103 }
104 else
105 {
106 m_equilibriumPoint[index] = m_calculatedAxisAngleDiff[index - 3];
107 }
108 }
109
110 void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index, btScalar val)
111 {
112 btAssert((index >= 0) && (index < 6));
113 m_equilibriumPoint[index] = val;
114 }
115
116
117 void btGeneric6DofSpringConstraint::internalUpdateSprings(btConstraintInfo2* info)
118 {
119 // it is assumed that calculateTransforms() have been called before this call
120 int i;
121 //btVector3 relVel = m_rbB.getLinearVelocity() - m_rbA.getLinearVelocity();
122 for(i = 0; i < 3; i++)
123 {
124 if(m_springEnabled[i])
125 {
126 // get current position of constraint
127 btScalar currPos = m_calculatedLinearDiff[i];
128 // calculate difference
129 btScalar delta = currPos - m_equilibriumPoint[i];
130 // spring force is (delta * m_stiffness) according to Hooke's Law
131 btScalar force = delta * m_springStiffness[i];
132 btScalar velFactor = info->fps * m_springDamping[i] / btScalar(info->m_numIterations);
133 m_linearLimits.m_targetVelocity[i] = velFactor * force;
134 m_linearLimits.m_maxMotorForce[i] = btFabs(force) / info->fps;
135 }
136 }
137 for(i = 0; i < 3; i++)
138 {
139 if(m_springEnabled[i + 3])
140 {
141 // get current position of constraint
142 btScalar currPos = m_calculatedAxisAngleDiff[i];
143 // calculate difference
144 btScalar delta = currPos - m_equilibriumPoint[i+3];
145 // spring force is (-delta * m_stiffness) according to Hooke's Law
146 btScalar force = -delta * m_springStiffness[i+3];
147 btScalar velFactor = info->fps * m_springDamping[i+3] / btScalar(info->m_numIterations);
148 m_angularLimits[i].m_targetVelocity = velFactor * force;
149 m_angularLimits[i].m_maxMotorForce = btFabs(force) / info->fps;
150 }
151 }
152 }
153
154
155 void btGeneric6DofSpringConstraint::getInfo2(btConstraintInfo2* info)
156 {
157 // this will be called by constraint solver at the constraint setup stage
158 // set current motor parameters
159 internalUpdateSprings(info);
160 // do the rest of job for constraint setup
161 btGeneric6DofConstraint::getInfo2(info);
162 }
163
164
165 void btGeneric6DofSpringConstraint::setAxis(const btVector3& axis1,const btVector3& axis2)
166 {
167 btVector3 zAxis = axis1.normalized();
168 btVector3 yAxis = axis2.normalized();
169 btVector3 xAxis = yAxis.cross(zAxis); // we want right coordinate system
170
171 btTransform frameInW;
172 frameInW.setIdentity();
173 frameInW.getBasis().setValue( xAxis[0], yAxis[0], zAxis[0],
174 xAxis[1], yAxis[1], zAxis[1],
175 xAxis[2], yAxis[2], zAxis[2]);
176
177 // now get constraint frame in local coordinate systems
178 m_frameInA = m_rbA.getCenterOfMassTransform().inverse() * frameInW;
179 m_frameInB = m_rbB.getCenterOfMassTransform().inverse() * frameInW;
180
181 calculateTransforms();
182 }
183
184
185
btTransformUtil.h
btRotationalLimitMotor::m_enableMotor
bool m_enableMotor
Definition: btGeneric6DofConstraint.h:64
btTypedConstraint::m_rbA
btRigidBody & m_rbA
Definition: btTypedConstraint.h:102
btTypedConstraint::btConstraintInfo2
Definition: btTypedConstraint.h:126
btGeneric6DofSpringConstraint::internalUpdateSprings
void internalUpdateSprings(btConstraintInfo2 *info)
Definition: btGeneric6DofSpringConstraint.cpp:117
btGeneric6DofSpringConstraint::btGeneric6DofSpringConstraint
btGeneric6DofSpringConstraint(btRigidBody &rbA, btRigidBody &rbB, const btTransform &frameInA, const btTransform &frameInB, bool useLinearReferenceFrameA)
Definition: btGeneric6DofSpringConstraint.cpp:21
btGeneric6DofConstraint
btGeneric6DofConstraint between two rigidbodies each with a pivotpoint that descibes the axis locatio...
Definition: btGeneric6DofConstraint.h:279
btTranslationalLimitMotor::m_targetVelocity
btVector3 m_targetVelocity
target motor velocity
Definition: btGeneric6DofConstraint.h:156
btTransform::setIdentity
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:172
btAssert
#define btAssert(x)
Definition: btScalar.h:113
btGeneric6DofSpringConstraint::setStiffness
void setStiffness(int index, btScalar stiffness)
Definition: btGeneric6DofSpringConstraint.cpp:65
btRigidBody.h
btTranslationalLimitMotor::m_enableMotor
bool m_enableMotor[3]
Definition: btGeneric6DofConstraint.h:155
btGeneric6DofSpringConstraint::m_springDamping
btScalar m_springDamping[6]
Definition: btGeneric6DofSpringConstraint.h:50
btGeneric6DofSpringConstraint::m_equilibriumPoint
btScalar m_equilibriumPoint[6]
Definition: btGeneric6DofSpringConstraint.h:48
btGeneric6DofConstraint::calculateTransforms
void calculateTransforms()
Definition: btGeneric6DofConstraint.cpp:384
btGeneric6DofSpringConstraint::getInfo2
virtual void getInfo2(btConstraintInfo2 *info)
internal method used by the constraint solver, don&#39;t use them directly
Definition: btGeneric6DofSpringConstraint.cpp:155
btGeneric6DofConstraint::m_calculatedAxisAngleDiff
btVector3 m_calculatedAxisAngleDiff
Definition: btGeneric6DofConstraint.h:313
btGeneric6DofSpringConstraint::m_springStiffness
btScalar m_springStiffness[6]
Definition: btGeneric6DofSpringConstraint.h:49
btRigidBody::getCenterOfMassTransform
const btTransform & getCenterOfMassTransform() const
Definition: btRigidBody.h:356
btGeneric6DofSpringConstraint::enableSpring
void enableSpring(int index, bool onOff)
Definition: btGeneric6DofSpringConstraint.cpp:49
btGeneric6DofSpringConstraint::setEquilibriumPoint
void setEquilibriumPoint()
Definition: btGeneric6DofSpringConstraint.cpp:79
btGeneric6DofSpringConstraint::setDamping
void setDamping(int index, btScalar damping)
Definition: btGeneric6DofSpringConstraint.cpp:72
btGeneric6DofConstraint::m_linearLimits
btTranslationalLimitMotor m_linearLimits
Linear_Limit_parameters.
Definition: btGeneric6DofConstraint.h:297
btVector3::cross
btVector3 cross(const btVector3 &v) const
Return the cross product between this and another vector.
Definition: btVector3.h:377
btRotationalLimitMotor::m_targetVelocity
btScalar m_targetVelocity
target motor velocity
Definition: btGeneric6DofConstraint.h:55
btTransform::getBasis
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:112
btMatrix3x3::setValue
void setValue(const btScalar &xx, const btScalar &xy, const btScalar &xz, const btScalar &yx, const btScalar &yy, const btScalar &yz, const btScalar &zx, const btScalar &zy, const btScalar &zz)
Set the values of the matrix explicitly (row major)
Definition: btMatrix3x3.h:198
btRigidBody
The btRigidBody is the main class for rigid body objects.
Definition: btRigidBody.h:62
D6_SPRING_CONSTRAINT_TYPE
Definition: btTypedConstraint.h:44
btTransform::inverse
btTransform inverse() const
Return the inverse of this transform.
Definition: btTransform.h:188
btGeneric6DofSpringConstraint::init
void init()
Definition: btGeneric6DofSpringConstraint.cpp:35
btGeneric6DofConstraint::m_calculatedLinearDiff
btVector3 m_calculatedLinearDiff
Definition: btGeneric6DofConstraint.h:315
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btGeneric6DofConstraint::m_frameInA
btTransform m_frameInA
relative_frames
Definition: btGeneric6DofConstraint.h:285
btGeneric6DofConstraint::getInfo2
virtual void getInfo2(btConstraintInfo2 *info)
internal method used by the constraint solver, don&#39;t use them directly
Definition: btGeneric6DofConstraint.cpp:569
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btGeneric6DofConstraint::m_angularLimits
btRotationalLimitMotor m_angularLimits[3]
hinge_parameters
Definition: btGeneric6DofConstraint.h:303
btTypedConstraint::m_rbB
btRigidBody & m_rbB
Definition: btTypedConstraint.h:103
btVector3::normalized
btVector3 normalized() const
Return a normalized version of this vector.
Definition: btVector3.h:952
btGeneric6DofConstraint::m_frameInB
btTransform m_frameInB
the constraint space w.r.t body B
Definition: btGeneric6DofConstraint.h:286
btGeneric6DofSpringConstraint.h
btGeneric6DofSpringConstraint::m_springEnabled
bool m_springEnabled[6]
Definition: btGeneric6DofSpringConstraint.h:47
btTypedConstraint::btConstraintInfo2::fps
btScalar fps
Definition: btTypedConstraint.h:129
btTranslationalLimitMotor::m_maxMotorForce
btVector3 m_maxMotorForce
max force on motor
Definition: btGeneric6DofConstraint.h:157
btGeneric6DofSpringConstraint::setAxis
virtual void setAxis(const btVector3 &axis1, const btVector3 &axis2)
Definition: btGeneric6DofSpringConstraint.cpp:165
btRotationalLimitMotor::m_maxMotorForce
btScalar m_maxMotorForce
max force on motor
Definition: btGeneric6DofConstraint.h:56
btTypedConstraint::btConstraintInfo2::m_numIterations
int m_numIterations
Definition: btTypedConstraint.h:154
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:278
btTypedObject::m_objectType
int m_objectType
Definition: btScalar.h:750
btFabs
btScalar btFabs(btScalar x)
Definition: btScalar.h:449