Bullet Collision Detection & Physics Library
btMultiBodyJointLimitConstraint.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2013 Erwin Coumans http://bulletphysics.org
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 
17 
19 #include "btMultiBody.h"
22 
23 
24 
26  //:btMultiBodyConstraint(body,0,link,-1,2,true),
27  :btMultiBodyConstraint(body,body,link,body->getLink(link).m_parent,2,true),
28  m_lowerBound(lower),
29  m_upperBound(upper)
30 {
31 
32 }
33 
35 {
36  // the data.m_jacobians never change, so may as well
37  // initialize them here
38 
40 
41  unsigned int offset = 6 + m_bodyA->getLink(m_linkA).m_dofOffset;
42 
43  // row 0: the lower bound
44  jacobianA(0)[offset] = 1;
45  // row 1: the upper bound
46  //jacobianA(1)[offset] = -1;
47  jacobianB(1)[offset] = -1;
48 
50 }
51 
53 {
54 }
55 
56 
58 {
59 
60  if (m_bodyA)
61  {
62  if (m_linkA < 0)
63  {
65  if (col)
66  return col->getIslandTag();
67  }
68  else
69  {
72  }
73  }
74  return -1;
75 }
76 
78 {
79  if (m_bodyB)
80  {
81  if (m_linkB < 0)
82  {
84  if (col)
85  return col->getIslandTag();
86  }
87  else
88  {
91  }
92  }
93  return -1;
94 }
95 
96 
99  const btContactSolverInfo& infoGlobal)
100 {
101 
102  // only positions need to be updated -- data.m_jacobians and force
103  // directions were set in the ctor and never change.
104 
106  {
108  }
109 
110 
111  // row 0: the lower bound
112  setPosition(0, m_bodyA->getJointPos(m_linkA) - m_lowerBound); //multidof: this is joint-type dependent
113 
114  // row 1: the upper bound
116 
117  for (int row=0;row<getNumRows();row++)
118  {
119  btScalar penetration = getPosition(row);
120 
121  //todo: consider adding some safety threshold here
122  if (penetration>0)
123  {
124  continue;
125  }
126  btScalar direction = row? -1 : 1;
127 
128  btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
129  constraintRow.m_orgConstraint = this;
130  constraintRow.m_orgDofIndex = row;
131 
132  constraintRow.m_multiBodyA = m_bodyA;
133  constraintRow.m_multiBodyB = m_bodyB;
134  const btScalar posError = 0; //why assume it's zero?
135  const btVector3 dummy(0, 0, 0);
136 
137  btScalar rel_vel = fillMultiBodyConstraint(constraintRow,data,jacobianA(row),jacobianB(row),dummy,dummy,dummy,dummy,posError,infoGlobal,0,m_maxAppliedImpulse);
138 
139  {
140  //expect either prismatic or revolute joint type for now
143  {
145  {
146  constraintRow.m_contactNormal1.setZero();
147  constraintRow.m_contactNormal2.setZero();
149  constraintRow.m_relpos1CrossNormal=revoluteAxisInWorld;
150  constraintRow.m_relpos2CrossNormal=-revoluteAxisInWorld;
151 
152  break;
153  }
155  {
157  constraintRow.m_contactNormal1=prismaticAxisInWorld;
158  constraintRow.m_contactNormal2=-prismaticAxisInWorld;
159  constraintRow.m_relpos1CrossNormal.setZero();
160  constraintRow.m_relpos2CrossNormal.setZero();
161 
162  break;
163  }
164  default:
165  {
166  btAssert(0);
167  }
168  };
169 
170  }
171 
172  {
173 
174  btScalar positionalError = 0.f;
175  btScalar velocityError = - rel_vel;// * damping;
176  btScalar erp = infoGlobal.m_erp2;
177  if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
178  {
179  erp = infoGlobal.m_erp;
180  }
181  if (penetration>0)
182  {
183  positionalError = 0;
184  velocityError = -penetration / infoGlobal.m_timeStep;
185  } else
186  {
187  positionalError = -penetration * erp/infoGlobal.m_timeStep;
188  }
189 
190  btScalar penetrationImpulse = positionalError*constraintRow.m_jacDiagABInv;
191  btScalar velocityImpulse = velocityError *constraintRow.m_jacDiagABInv;
192  if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
193  {
194  //combine position and velocity into rhs
195  constraintRow.m_rhs = penetrationImpulse+velocityImpulse;
196  constraintRow.m_rhsPenetration = 0.f;
197 
198  } else
199  {
200  //split position and velocity into rhs and m_rhsPenetration
201  constraintRow.m_rhs = velocityImpulse;
202  constraintRow.m_rhsPenetration = penetrationImpulse;
203  }
204  }
205  }
206 
207 }
208 
209 
210 
211 
btScalar * jacobianB(int row)
virtual void createConstraintRows(btMultiBodyConstraintArray &constraintRows, btMultiBodyJacobianData &data, const btContactSolverInfo &infoGlobal)
const btMultibodyLink & getLink(int index) const
Definition: btMultiBody.h:119
1D constraint along a normal axis between bodyA and bodyB. It can be combined to solve contact and fr...
btQuaternion getRotation() const
Return a quaternion representing the rotation.
Definition: btTransform.h:122
btMultiBodyConstraint * m_orgConstraint
#define btAssert(x)
Definition: btScalar.h:131
btScalar * jacobianA(int row)
btScalar getJointPos(int i) const
btVector3 quatRotate(const btQuaternion &rotation, const btVector3 &v)
Definition: btQuaternion.h:937
btScalar getPosition(int row) const
void setZero()
Definition: btVector3.h:683
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btMultiBodyJointLimitConstraint(btMultiBody *body, int link, btScalar lower, btScalar upper)
This file was written by Erwin Coumans.
int getIslandTag() const
void setPosition(int row, btScalar pos)
const btMultiBodyLinkCollider * getBaseCollider() const
Definition: btMultiBody.h:134
btScalar fillMultiBodyConstraint(btMultiBodySolverConstraint &solverConstraint, btMultiBodyJacobianData &data, btScalar *jacOrgA, btScalar *jacOrgB, const btVector3 &constraintNormalAng, const btVector3 &constraintNormalLin, const btVector3 &posAworld, const btVector3 &posBworld, btScalar posError, const btContactSolverInfo &infoGlobal, btScalar lowerLimit, btScalar upperLimit, bool angConstraint=false, btScalar relaxation=1.f, bool isFriction=false, btScalar desiredVelocity=0, btScalar cfmSlip=0)
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292