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 
25  //:btMultiBodyConstraint(body,0,link,-1,2,true),
26  :btMultiBodyConstraint(body,body,link,link,2,true),
27  m_lowerBound(lower),
28  m_upperBound(upper)
29 {
30 
31 }
32 
34 {
35  // the data.m_jacobians never change, so may as well
36  // initialize them here
37 
39 
40  unsigned int offset = 6 + (m_bodyA->isMultiDof() ? m_bodyA->getLink(m_linkA).m_dofOffset : m_linkA);
41 
42  // row 0: the lower bound
43  jacobianA(0)[offset] = 1;
44  // row 1: the upper bound
45  //jacobianA(1)[offset] = -1;
46 
47  jacobianB(1)[offset] = -1;
48 
50 }
51 
53 {
54 }
55 
57 {
58  if(m_bodyA)
59  {
61  if (col)
62  return col->getIslandTag();
63  for (int i=0;i<m_bodyA->getNumLinks();i++)
64  {
65  if (m_bodyA->getLink(i).m_collider)
66  return m_bodyA->getLink(i).m_collider->getIslandTag();
67  }
68  }
69  return -1;
70 }
71 
73 {
74  if(m_bodyB)
75  {
77  if (col)
78  return col->getIslandTag();
79 
80  for (int i=0;i<m_bodyB->getNumLinks();i++)
81  {
82  col = m_bodyB->getLink(i).m_collider;
83  if (col)
84  return col->getIslandTag();
85  }
86  }
87  return -1;
88 }
89 
90 
93  const btContactSolverInfo& infoGlobal)
94 {
95  // only positions need to be updated -- data.m_jacobians and force
96  // directions were set in the ctor and never change.
97 
99  {
101  }
102 
103 
104  // row 0: the lower bound
105  setPosition(0, m_bodyA->getJointPos(m_linkA) - m_lowerBound); //multidof: this is joint-type dependent
106 
107  // row 1: the upper bound
109 
110  for (int row=0;row<getNumRows();row++)
111  {
112  btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
113  constraintRow.m_multiBodyA = m_bodyA;
114  constraintRow.m_multiBodyB = m_bodyB;
115  const btScalar posError = 0; //why assume it's zero?
116  const btVector3 dummy(0, 0, 0);
117 
118  btScalar rel_vel = fillMultiBodyConstraint(constraintRow,data,jacobianA(row),jacobianB(row),dummy,dummy,dummy,posError,infoGlobal,0,m_maxAppliedImpulse);
119  {
120  btScalar penetration = getPosition(row);
121  btScalar positionalError = 0.f;
122  btScalar velocityError = - rel_vel;// * damping;
123  btScalar erp = infoGlobal.m_erp2;
124  if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
125  {
126  erp = infoGlobal.m_erp;
127  }
128  if (penetration>0)
129  {
130  positionalError = 0;
131  velocityError = -penetration / infoGlobal.m_timeStep;
132  } else
133  {
134  positionalError = -penetration * erp/infoGlobal.m_timeStep;
135  }
136 
137  btScalar penetrationImpulse = positionalError*constraintRow.m_jacDiagABInv;
138  btScalar velocityImpulse = velocityError *constraintRow.m_jacDiagABInv;
139  if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
140  {
141  //combine position and velocity into rhs
142  constraintRow.m_rhs = penetrationImpulse+velocityImpulse;
143  constraintRow.m_rhsPenetration = 0.f;
144 
145  } else
146  {
147  //split position and velocity into rhs and m_rhsPenetration
148  constraintRow.m_rhs = velocityImpulse;
149  constraintRow.m_rhsPenetration = penetrationImpulse;
150  }
151  }
152  }
153 
154 }
155 
156 
157 
158 
btScalar fillMultiBodyConstraint(btMultiBodySolverConstraint &solverConstraint, btMultiBodyJacobianData &data, btScalar *jacOrgA, btScalar *jacOrgB, const btVector3 &contactNormalOnB, const btVector3 &posAworld, const btVector3 &posBworld, btScalar posError, const btContactSolverInfo &infoGlobal, btScalar lowerLimit, btScalar upperLimit, btScalar relaxation=1.f, bool isFriction=false, btScalar desiredVelocity=0, btScalar cfmSlip=0)
btScalar * jacobianB(int row)
virtual void createConstraintRows(btMultiBodyConstraintArray &constraintRows, btMultiBodyJacobianData &data, const btContactSolverInfo &infoGlobal)
const btMultibodyLink & getLink(int index) const
Definition: btMultiBody.h:109
1D constraint along a normal axis between bodyA and bodyB. It can be combined to solve contact and fr...
int getNumLinks() const
Definition: btMultiBody.h:145
btScalar * jacobianA(int row)
btScalar getJointPos(int i) const
btScalar getPosition(int row) const
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:124
bool isMultiDof()
Definition: btMultiBody.h:522
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:278