Bullet Collision Detection & Physics Library
btMultiBodyFixedConstraint.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 
23 
24 #define BTMBFIXEDCONSTRAINT_DIM 6
25 
26 btMultiBodyFixedConstraint::btMultiBodyFixedConstraint(btMultiBody* body, int link, btRigidBody* bodyB, const btVector3& pivotInA, const btVector3& pivotInB, const btMatrix3x3& frameInA, const btMatrix3x3& frameInB)
27  :btMultiBodyConstraint(body,0,link,-1,BTMBFIXEDCONSTRAINT_DIM,false),
28  m_rigidBodyA(0),
29  m_rigidBodyB(bodyB),
30  m_pivotInA(pivotInA),
31  m_pivotInB(pivotInB),
32  m_frameInA(frameInA),
33  m_frameInB(frameInB)
34 {
35  m_data.resize(BTMBFIXEDCONSTRAINT_DIM);//at least store the applied impulses
36 }
37 
38 btMultiBodyFixedConstraint::btMultiBodyFixedConstraint(btMultiBody* bodyA, int linkA, btMultiBody* bodyB, int linkB, const btVector3& pivotInA, const btVector3& pivotInB, const btMatrix3x3& frameInA, const btMatrix3x3& frameInB)
39  :btMultiBodyConstraint(bodyA,bodyB,linkA,linkB,BTMBFIXEDCONSTRAINT_DIM,false),
40  m_rigidBodyA(0),
41  m_rigidBodyB(0),
42  m_pivotInA(pivotInA),
43  m_pivotInB(pivotInB),
44  m_frameInA(frameInA),
45  m_frameInB(frameInB)
46 {
47  m_data.resize(BTMBFIXEDCONSTRAINT_DIM);//at least store the applied impulses
48 }
49 
51 {
52  //not implemented yet
53  btAssert(0);
54 }
55 
57 {
58 }
59 
60 
62 {
63  if (m_rigidBodyA)
64  return m_rigidBodyA->getIslandTag();
65 
66  if (m_bodyA)
67  {
68  if (m_linkA < 0)
69  {
71  if (col)
72  return col->getIslandTag();
73  }
74  else
75  {
78  }
79  }
80  return -1;
81 }
82 
84 {
85  if (m_rigidBodyB)
86  return m_rigidBodyB->getIslandTag();
87  if (m_bodyB)
88  {
89  if (m_linkB < 0)
90  {
92  if (col)
93  return col->getIslandTag();
94  }
95  else
96  {
99  }
100  }
101  return -1;
102 }
103 
105 {
106  int numDim = BTMBFIXEDCONSTRAINT_DIM;
107  for (int i=0;i<numDim;i++)
108  {
109  btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
110  constraintRow.m_orgConstraint = this;
111  constraintRow.m_orgDofIndex = i;
112  constraintRow.m_relpos1CrossNormal.setValue(0,0,0);
113  constraintRow.m_contactNormal1.setValue(0,0,0);
114  constraintRow.m_relpos2CrossNormal.setValue(0,0,0);
115  constraintRow.m_contactNormal2.setValue(0,0,0);
116  constraintRow.m_angularComponentA.setValue(0,0,0);
117  constraintRow.m_angularComponentB.setValue(0,0,0);
118 
119  constraintRow.m_solverBodyIdA = data.m_fixedBodyId;
120  constraintRow.m_solverBodyIdB = data.m_fixedBodyId;
121 
122  // Convert local points back to world
123  btVector3 pivotAworld = m_pivotInA;
124  btMatrix3x3 frameAworld = m_frameInA;
125  if (m_rigidBodyA)
126  {
127 
128  constraintRow.m_solverBodyIdA = m_rigidBodyA->getCompanionId();
130  frameAworld = frameAworld.transpose()*btMatrix3x3(m_rigidBodyA->getOrientation());
131 
132  } else
133  {
134  if (m_bodyA) {
135  pivotAworld = m_bodyA->localPosToWorld(m_linkA, m_pivotInA);
136  frameAworld = m_bodyA->localFrameToWorld(m_linkA, frameAworld);
137  }
138  }
139  btVector3 pivotBworld = m_pivotInB;
140  btMatrix3x3 frameBworld = m_frameInB;
141  if (m_rigidBodyB)
142  {
143  constraintRow.m_solverBodyIdB = m_rigidBodyB->getCompanionId();
145  frameBworld = frameBworld.transpose()*btMatrix3x3(m_rigidBodyB->getOrientation());
146 
147  } else
148  {
149  if (m_bodyB) {
150  pivotBworld = m_bodyB->localPosToWorld(m_linkB, m_pivotInB);
151  frameBworld = m_bodyB->localFrameToWorld(m_linkB, frameBworld);
152  }
153  }
154 
155  btMatrix3x3 relRot = frameAworld.inverse()*frameBworld;
156  btVector3 angleDiff;
158 
159  btVector3 constraintNormalLin(0,0,0);
160  btVector3 constraintNormalAng(0,0,0);
161  btScalar posError = 0.0;
162  if (i < 3) {
163  constraintNormalLin[i] = 1;
164  posError = (pivotAworld-pivotBworld).dot(constraintNormalLin);
165  fillMultiBodyConstraint(constraintRow, data, 0, 0, constraintNormalAng,
166  constraintNormalLin, pivotAworld, pivotBworld,
167  posError,
168  infoGlobal,
170  );
171  }
172  else { //i>=3
173  constraintNormalAng = frameAworld.getColumn(i%3);
174  posError = angleDiff[i%3];
175  fillMultiBodyConstraint(constraintRow, data, 0, 0, constraintNormalAng,
176  constraintNormalLin, pivotAworld, pivotBworld,
177  posError,
178  infoGlobal,
180  );
181  }
182  }
183 }
184 
186 {
187  btTransform tr;
188  tr.setIdentity();
189 
190  if (m_rigidBodyA)
191  {
193  tr.setOrigin(pivot);
194  drawer->drawTransform(tr, 0.1);
195  }
196  if (m_bodyA)
197  {
199  tr.setOrigin(pivotAworld);
200  drawer->drawTransform(tr, 0.1);
201  }
202  if (m_rigidBodyB)
203  {
204  // that ideally should draw the same frame
206  tr.setOrigin(pivot);
207  drawer->drawTransform(tr, 0.1);
208  }
209  if (m_bodyB)
210  {
212  tr.setOrigin(pivotBworld);
213  drawer->drawTransform(tr, 0.1);
214  }
215 }
btMatrix3x3 inverse() const
Return the inverse of the matrix.
Definition: btMatrix3x3.h:1075
void setOrigin(const btVector3 &origin)
Set the translational element.
Definition: btTransform.h:150
static bool matrixToEulerXYZ(const btMatrix3x3 &mat, btVector3 &xyz)
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...
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:652
btMultiBodyConstraint * m_orgConstraint
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:172
btMatrix3x3 localFrameToWorld(int i, const btMatrix3x3 &mat) const
#define btAssert(x)
Definition: btScalar.h:131
btVector3 getColumn(int i) const
Get a column of the matrix as a vector.
Definition: btMatrix3x3.h:134
virtual void createConstraintRows(btMultiBodyConstraintArray &constraintRows, btMultiBodyJacobianData &data, const btContactSolverInfo &infoGlobal)
const btTransform & getCenterOfMassTransform() const
Definition: btRigidBody.h:359
btQuaternion getOrientation() const
#define BTMBFIXEDCONSTRAINT_DIM
This file was written by Erwin Coumans.
The btIDebugDraw interface class allows hooking up a debug renderer to visually debug simulations...
Definition: btIDebugDraw.h:29
The btRigidBody is the main class for rigid body objects.
Definition: btRigidBody.h:62
btAlignedObjectArray< btScalar > m_data
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
int getCompanionId() const
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
int getIslandTag() const
virtual void drawTransform(const btTransform &transform, btScalar orthoLen)
Definition: btIDebugDraw.h:166
void resize(int newsize, const T &fillData=T())
btMatrix3x3 transpose() const
Return the transpose of the matrix.
Definition: btMatrix3x3.h:1030
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:48
btScalar dot(const btQuaternion &q1, const btQuaternion &q2)
Calculate the dot product between two quaternions.
Definition: btQuaternion.h:898
const btMultiBodyLinkCollider * getBaseCollider() const
Definition: btMultiBody.h:134
btMultiBodyFixedConstraint(btMultiBody *body, int link, btRigidBody *bodyB, const btVector3 &pivotInA, const btVector3 &pivotInB, const btMatrix3x3 &frameInA, const btMatrix3x3 &frameInB)
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)
btVector3 localPosToWorld(int i, const btVector3 &vec) const
virtual void debugDraw(class btIDebugDraw *drawer)
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292