Bullet Collision Detection & Physics Library
btMultiBodyPoint2Point.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 
18 #include "btMultiBodyPoint2Point.h"
22 
23 #ifndef BTMBP2PCONSTRAINT_BLOCK_ANGULAR_MOTION_TEST
24  #define BTMBP2PCONSTRAINT_DIM 3
25 #else
26  #define BTMBP2PCONSTRAINT_DIM 6
27 #endif
28 
29 btMultiBodyPoint2Point::btMultiBodyPoint2Point(btMultiBody* body, int link, btRigidBody* bodyB, const btVector3& pivotInA, const btVector3& pivotInB)
30  :btMultiBodyConstraint(body,0,link,-1,BTMBP2PCONSTRAINT_DIM,false),
31  m_rigidBodyA(0),
32  m_rigidBodyB(bodyB),
33  m_pivotInA(pivotInA),
34  m_pivotInB(pivotInB)
35 {
36  m_data.resize(BTMBP2PCONSTRAINT_DIM);//at least store the applied impulses
37 }
38 
39 btMultiBodyPoint2Point::btMultiBodyPoint2Point(btMultiBody* bodyA, int linkA, btMultiBody* bodyB, int linkB, const btVector3& pivotInA, const btVector3& pivotInB)
40  :btMultiBodyConstraint(bodyA,bodyB,linkA,linkB,BTMBP2PCONSTRAINT_DIM,false),
41  m_rigidBodyA(0),
42  m_rigidBodyB(0),
43  m_pivotInA(pivotInA),
44  m_pivotInB(pivotInB)
45 {
46  m_data.resize(BTMBP2PCONSTRAINT_DIM);//at least store the applied impulses
47 }
48 
50 {
51  //not implemented yet
52  btAssert(0);
53 }
54 
56 {
57 }
58 
59 
61 {
62  if (m_rigidBodyA)
63  return m_rigidBodyA->getIslandTag();
64 
65  if (m_bodyA)
66  {
67  if (m_linkA < 0)
68  {
70  if (col)
71  return col->getIslandTag();
72  }
73  else
74  {
77  }
78  }
79  return -1;
80 }
81 
83 {
84  if (m_rigidBodyB)
85  return m_rigidBodyB->getIslandTag();
86  if (m_bodyB)
87  {
88  if (m_linkB < 0)
89  {
91  if (col)
92  return col->getIslandTag();
93  }
94  else
95  {
98  }
99  }
100  return -1;
101 }
102 
103 
104 
107  const btContactSolverInfo& infoGlobal)
108 {
109 
110 // int i=1;
111 int numDim = BTMBP2PCONSTRAINT_DIM;
112  for (int i=0;i<numDim;i++)
113  {
114 
115  btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
116  //memset(&constraintRow,0xffffffff,sizeof(btMultiBodySolverConstraint));
117  constraintRow.m_orgConstraint = this;
118  constraintRow.m_orgDofIndex = i;
119  constraintRow.m_relpos1CrossNormal.setValue(0,0,0);
120  constraintRow.m_contactNormal1.setValue(0,0,0);
121  constraintRow.m_relpos2CrossNormal.setValue(0,0,0);
122  constraintRow.m_contactNormal2.setValue(0,0,0);
123  constraintRow.m_angularComponentA.setValue(0,0,0);
124  constraintRow.m_angularComponentB.setValue(0,0,0);
125 
126  constraintRow.m_solverBodyIdA = data.m_fixedBodyId;
127  constraintRow.m_solverBodyIdB = data.m_fixedBodyId;
128 
129  btVector3 contactNormalOnB(0,0,0);
130 #ifndef BTMBP2PCONSTRAINT_BLOCK_ANGULAR_MOTION_TEST
131  contactNormalOnB[i] = -1;
132 #else
133  contactNormalOnB[i%3] = -1;
134 #endif
135 
136 
137  // Convert local points back to world
138  btVector3 pivotAworld = m_pivotInA;
139  if (m_rigidBodyA)
140  {
141 
142  constraintRow.m_solverBodyIdA = m_rigidBodyA->getCompanionId();
144  } else
145  {
146  if (m_bodyA)
147  pivotAworld = m_bodyA->localPosToWorld(m_linkA, m_pivotInA);
148  }
149  btVector3 pivotBworld = m_pivotInB;
150  if (m_rigidBodyB)
151  {
152  constraintRow.m_solverBodyIdB = m_rigidBodyB->getCompanionId();
154  } else
155  {
156  if (m_bodyB)
157  pivotBworld = m_bodyB->localPosToWorld(m_linkB, m_pivotInB);
158 
159  }
160 
161  btScalar posError = i < 3 ? (pivotAworld-pivotBworld).dot(contactNormalOnB) : 0;
162 
163 #ifndef BTMBP2PCONSTRAINT_BLOCK_ANGULAR_MOTION_TEST
164 
165 
166  fillMultiBodyConstraint(constraintRow, data, 0, 0, btVector3(0,0,0),
167  contactNormalOnB, pivotAworld, pivotBworld, //sucks but let it be this way "for the time being"
168  posError,
169  infoGlobal,
171  );
172  //@todo: support the case of btMultiBody versus btRigidBody,
173  //see btPoint2PointConstraint::getInfo2NonVirtual
174 #else
175  const btVector3 dummy(0, 0, 0);
176 
177  btAssert(m_bodyA->isMultiDof());
178 
179  btScalar* jac1 = jacobianA(i);
180  const btVector3 &normalAng = i >= 3 ? contactNormalOnB : dummy;
181  const btVector3 &normalLin = i < 3 ? contactNormalOnB : dummy;
182 
183  m_bodyA->filConstraintJacobianMultiDof(m_linkA, pivotAworld, normalAng, normalLin, jac1, data.scratch_r, data.scratch_v, data.scratch_m);
184 
185  fillMultiBodyConstraint(constraintRow, data, jac1, 0,
186  dummy, dummy, dummy, //sucks but let it be this way "for the time being"
187  posError,
188  infoGlobal,
190  );
191 #endif
192  }
193 }
194 
196 {
197  btTransform tr;
198  tr.setIdentity();
199 
200  if (m_rigidBodyA)
201  {
203  tr.setOrigin(pivot);
204  drawer->drawTransform(tr, 0.1);
205  }
206  if (m_bodyA)
207  {
209  tr.setOrigin(pivotAworld);
210  drawer->drawTransform(tr, 0.1);
211  }
212  if (m_rigidBodyB)
213  {
214  // that ideally should draw the same frame
216  tr.setOrigin(pivot);
217  drawer->drawTransform(tr, 0.1);
218  }
219  if (m_bodyB)
220  {
222  tr.setOrigin(pivotBworld);
223  drawer->drawTransform(tr, 0.1);
224  }
225 }
void setOrigin(const btVector3 &origin)
Set the translational element.
Definition: btTransform.h:150
const btMultibodyLink & getLink(int index) const
Definition: btMultiBody.h:119
virtual void debugDraw(class btIDebugDraw *drawer)
1D constraint along a normal axis between bodyA and bodyB. It can be combined to solve contact and fr...
btAlignedObjectArray< btScalar > scratch_r
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
virtual int getIslandIdA() const
#define btAssert(x)
Definition: btScalar.h:131
btScalar * jacobianA(int row)
btMultiBodyPoint2Point(btMultiBody *body, int link, btRigidBody *bodyB, const btVector3 &pivotInA, const btVector3 &pivotInB)
btAlignedObjectArray< btMatrix3x3 > scratch_m
const btTransform & getCenterOfMassTransform() const
Definition: btRigidBody.h:359
virtual void createConstraintRows(btMultiBodyConstraintArray &constraintRows, btMultiBodyJacobianData &data, const btContactSolverInfo &infoGlobal)
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
btAlignedObjectArray< btVector3 > scratch_v
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
int getIslandTag() const
#define BTMBP2PCONSTRAINT_DIM
This file was written by Erwin Coumans.
virtual void drawTransform(const btTransform &transform, btScalar orthoLen)
Definition: btIDebugDraw.h:166
void resize(int newsize, const T &fillData=T())
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
virtual int getIslandIdB() const
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
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292