[1963] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
| 3 | Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
| 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 | |
---|
| 17 | #include "btPoint2PointConstraint.h" |
---|
| 18 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 19 | #include <new> |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | btPoint2PointConstraint::btPoint2PointConstraint() |
---|
[2882] | 24 | :btTypedConstraint(POINT2POINT_CONSTRAINT_TYPE), |
---|
| 25 | m_useSolveConstraintObsolete(false) |
---|
[1963] | 26 | { |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | btPoint2PointConstraint::btPoint2PointConstraint(btRigidBody& rbA,btRigidBody& rbB, const btVector3& pivotInA,const btVector3& pivotInB) |
---|
[2882] | 30 | :btTypedConstraint(POINT2POINT_CONSTRAINT_TYPE,rbA,rbB),m_pivotInA(pivotInA),m_pivotInB(pivotInB), |
---|
| 31 | m_useSolveConstraintObsolete(false) |
---|
[1963] | 32 | { |
---|
| 33 | |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | btPoint2PointConstraint::btPoint2PointConstraint(btRigidBody& rbA,const btVector3& pivotInA) |
---|
[2882] | 38 | :btTypedConstraint(POINT2POINT_CONSTRAINT_TYPE,rbA),m_pivotInA(pivotInA),m_pivotInB(rbA.getCenterOfMassTransform()(pivotInA)), |
---|
| 39 | m_useSolveConstraintObsolete(false) |
---|
[1963] | 40 | { |
---|
| 41 | |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void btPoint2PointConstraint::buildJacobian() |
---|
| 45 | { |
---|
[2882] | 46 | ///we need it for both methods |
---|
| 47 | { |
---|
| 48 | m_appliedImpulse = btScalar(0.); |
---|
[1963] | 49 | |
---|
[2882] | 50 | btVector3 normal(0,0,0); |
---|
[1963] | 51 | |
---|
[2882] | 52 | for (int i=0;i<3;i++) |
---|
| 53 | { |
---|
| 54 | normal[i] = 1; |
---|
| 55 | new (&m_jac[i]) btJacobianEntry( |
---|
[1963] | 56 | m_rbA.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 57 | m_rbB.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 58 | m_rbA.getCenterOfMassTransform()*m_pivotInA - m_rbA.getCenterOfMassPosition(), |
---|
| 59 | m_rbB.getCenterOfMassTransform()*m_pivotInB - m_rbB.getCenterOfMassPosition(), |
---|
| 60 | normal, |
---|
| 61 | m_rbA.getInvInertiaDiagLocal(), |
---|
| 62 | m_rbA.getInvMass(), |
---|
| 63 | m_rbB.getInvInertiaDiagLocal(), |
---|
| 64 | m_rbB.getInvMass()); |
---|
| 65 | normal[i] = 0; |
---|
[2882] | 66 | } |
---|
[1963] | 67 | } |
---|
| 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
[2882] | 71 | |
---|
| 72 | void btPoint2PointConstraint::getInfo1 (btConstraintInfo1* info) |
---|
[1963] | 73 | { |
---|
[2882] | 74 | if (m_useSolveConstraintObsolete) |
---|
| 75 | { |
---|
| 76 | info->m_numConstraintRows = 0; |
---|
| 77 | info->nub = 0; |
---|
| 78 | } else |
---|
| 79 | { |
---|
| 80 | info->m_numConstraintRows = 3; |
---|
| 81 | info->nub = 3; |
---|
| 82 | } |
---|
| 83 | } |
---|
[1963] | 84 | |
---|
[2882] | 85 | void btPoint2PointConstraint::getInfo2 (btConstraintInfo2* info) |
---|
| 86 | { |
---|
| 87 | btAssert(!m_useSolveConstraintObsolete); |
---|
[1963] | 88 | |
---|
[2882] | 89 | //retrieve matrices |
---|
| 90 | btTransform body0_trans; |
---|
| 91 | body0_trans = m_rbA.getCenterOfMassTransform(); |
---|
| 92 | btTransform body1_trans; |
---|
| 93 | body1_trans = m_rbB.getCenterOfMassTransform(); |
---|
| 94 | |
---|
| 95 | // anchor points in global coordinates with respect to body PORs. |
---|
| 96 | |
---|
| 97 | // set jacobian |
---|
| 98 | info->m_J1linearAxis[0] = 1; |
---|
| 99 | info->m_J1linearAxis[info->rowskip+1] = 1; |
---|
| 100 | info->m_J1linearAxis[2*info->rowskip+2] = 1; |
---|
| 101 | |
---|
| 102 | btVector3 a1 = body0_trans.getBasis()*getPivotInA(); |
---|
| 103 | { |
---|
| 104 | btVector3* angular0 = (btVector3*)(info->m_J1angularAxis); |
---|
| 105 | btVector3* angular1 = (btVector3*)(info->m_J1angularAxis+info->rowskip); |
---|
| 106 | btVector3* angular2 = (btVector3*)(info->m_J1angularAxis+2*info->rowskip); |
---|
| 107 | btVector3 a1neg = -a1; |
---|
| 108 | a1neg.getSkewSymmetricMatrix(angular0,angular1,angular2); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /*info->m_J2linearAxis[0] = -1; |
---|
| 112 | info->m_J2linearAxis[s+1] = -1; |
---|
| 113 | info->m_J2linearAxis[2*s+2] = -1; |
---|
| 114 | */ |
---|
[1963] | 115 | |
---|
[2882] | 116 | btVector3 a2 = body1_trans.getBasis()*getPivotInB(); |
---|
| 117 | |
---|
| 118 | { |
---|
| 119 | btVector3 a2n = -a2; |
---|
| 120 | btVector3* angular0 = (btVector3*)(info->m_J2angularAxis); |
---|
| 121 | btVector3* angular1 = (btVector3*)(info->m_J2angularAxis+info->rowskip); |
---|
| 122 | btVector3* angular2 = (btVector3*)(info->m_J2angularAxis+2*info->rowskip); |
---|
| 123 | a2.getSkewSymmetricMatrix(angular0,angular1,angular2); |
---|
| 124 | } |
---|
| 125 | |
---|
[1963] | 126 | |
---|
| 127 | |
---|
[2882] | 128 | // set right hand side |
---|
| 129 | btScalar k = info->fps * info->erp; |
---|
| 130 | int j; |
---|
[1963] | 131 | |
---|
[2882] | 132 | for (j=0; j<3; j++) |
---|
| 133 | { |
---|
| 134 | info->m_constraintError[j*info->rowskip] = k * (a2[j] + body1_trans.getOrigin()[j] - a1[j] - body0_trans.getOrigin()[j]); |
---|
| 135 | //printf("info->m_constraintError[%d]=%f\n",j,info->m_constraintError[j]); |
---|
| 136 | } |
---|
[1963] | 137 | |
---|
[2882] | 138 | btScalar impulseClamp = m_setting.m_impulseClamp;// |
---|
| 139 | for (j=0; j<3; j++) |
---|
| 140 | { |
---|
| 141 | if (m_setting.m_impulseClamp > 0) |
---|
| 142 | { |
---|
| 143 | info->m_lowerLimit[j*info->rowskip] = -impulseClamp; |
---|
| 144 | info->m_upperLimit[j*info->rowskip] = impulseClamp; |
---|
| 145 | } |
---|
| 146 | } |
---|
[1963] | 147 | |
---|
[2882] | 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | void btPoint2PointConstraint::solveConstraintObsolete(btSolverBody& bodyA,btSolverBody& bodyB,btScalar timeStep) |
---|
| 152 | { |
---|
| 153 | if (m_useSolveConstraintObsolete) |
---|
| 154 | { |
---|
| 155 | btVector3 pivotAInW = m_rbA.getCenterOfMassTransform()*m_pivotInA; |
---|
| 156 | btVector3 pivotBInW = m_rbB.getCenterOfMassTransform()*m_pivotInB; |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | btVector3 normal(0,0,0); |
---|
[1963] | 160 | |
---|
| 161 | |
---|
[2882] | 162 | // btVector3 angvelA = m_rbA.getCenterOfMassTransform().getBasis().transpose() * m_rbA.getAngularVelocity(); |
---|
| 163 | // btVector3 angvelB = m_rbB.getCenterOfMassTransform().getBasis().transpose() * m_rbB.getAngularVelocity(); |
---|
[1963] | 164 | |
---|
[2882] | 165 | for (int i=0;i<3;i++) |
---|
| 166 | { |
---|
| 167 | normal[i] = 1; |
---|
| 168 | btScalar jacDiagABInv = btScalar(1.) / m_jac[i].getDiagonal(); |
---|
| 169 | |
---|
| 170 | btVector3 rel_pos1 = pivotAInW - m_rbA.getCenterOfMassPosition(); |
---|
| 171 | btVector3 rel_pos2 = pivotBInW - m_rbB.getCenterOfMassPosition(); |
---|
| 172 | //this jacobian entry could be re-used for all iterations |
---|
| 173 | |
---|
| 174 | btVector3 vel1,vel2; |
---|
| 175 | bodyA.getVelocityInLocalPointObsolete(rel_pos1,vel1); |
---|
| 176 | bodyB.getVelocityInLocalPointObsolete(rel_pos2,vel2); |
---|
| 177 | btVector3 vel = vel1 - vel2; |
---|
| 178 | |
---|
| 179 | btScalar rel_vel; |
---|
| 180 | rel_vel = normal.dot(vel); |
---|
| 181 | |
---|
| 182 | /* |
---|
| 183 | //velocity error (first order error) |
---|
| 184 | btScalar rel_vel = m_jac[i].getRelativeVelocity(m_rbA.getLinearVelocity(),angvelA, |
---|
| 185 | m_rbB.getLinearVelocity(),angvelB); |
---|
| 186 | */ |
---|
[1963] | 187 | |
---|
[2882] | 188 | //positional error (zeroth order error) |
---|
| 189 | btScalar depth = -(pivotAInW - pivotBInW).dot(normal); //this is the error projected on the normal |
---|
| 190 | |
---|
| 191 | btScalar deltaImpulse = depth*m_setting.m_tau/timeStep * jacDiagABInv - m_setting.m_damping * rel_vel * jacDiagABInv; |
---|
| 192 | |
---|
| 193 | btScalar impulseClamp = m_setting.m_impulseClamp; |
---|
| 194 | |
---|
| 195 | const btScalar sum = btScalar(m_appliedImpulse) + deltaImpulse; |
---|
| 196 | if (sum < -impulseClamp) |
---|
| 197 | { |
---|
| 198 | deltaImpulse = -impulseClamp-m_appliedImpulse; |
---|
| 199 | m_appliedImpulse = -impulseClamp; |
---|
| 200 | } |
---|
| 201 | else if (sum > impulseClamp) |
---|
| 202 | { |
---|
| 203 | deltaImpulse = impulseClamp-m_appliedImpulse; |
---|
| 204 | m_appliedImpulse = impulseClamp; |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | { |
---|
| 208 | m_appliedImpulse = sum; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | btVector3 impulse_vector = normal * deltaImpulse; |
---|
| 213 | |
---|
| 214 | btVector3 ftorqueAxis1 = rel_pos1.cross(normal); |
---|
| 215 | btVector3 ftorqueAxis2 = rel_pos2.cross(normal); |
---|
| 216 | bodyA.applyImpulse(normal*m_rbA.getInvMass(), m_rbA.getInvInertiaTensorWorld()*ftorqueAxis1,deltaImpulse); |
---|
| 217 | bodyB.applyImpulse(normal*m_rbB.getInvMass(), m_rbB.getInvInertiaTensorWorld()*ftorqueAxis2,-deltaImpulse); |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | normal[i] = 0; |
---|
| 221 | } |
---|
[1963] | 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | void btPoint2PointConstraint::updateRHS(btScalar timeStep) |
---|
| 226 | { |
---|
| 227 | (void)timeStep; |
---|
| 228 | |
---|
| 229 | } |
---|
| 230 | |
---|