| 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() | 
|---|
| 24 | :btTypedConstraint(POINT2POINT_CONSTRAINT_TYPE) | 
|---|
| 25 | { | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | btPoint2PointConstraint::btPoint2PointConstraint(btRigidBody& rbA,btRigidBody& rbB, const btVector3& pivotInA,const btVector3& pivotInB) | 
|---|
| 29 | :btTypedConstraint(POINT2POINT_CONSTRAINT_TYPE,rbA,rbB),m_pivotInA(pivotInA),m_pivotInB(pivotInB) | 
|---|
| 30 | { | 
|---|
| 31 |  | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | btPoint2PointConstraint::btPoint2PointConstraint(btRigidBody& rbA,const btVector3& pivotInA) | 
|---|
| 36 | :btTypedConstraint(POINT2POINT_CONSTRAINT_TYPE,rbA),m_pivotInA(pivotInA),m_pivotInB(rbA.getCenterOfMassTransform()(pivotInA)) | 
|---|
| 37 | { | 
|---|
| 38 |          | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | void    btPoint2PointConstraint::buildJacobian() | 
|---|
| 42 | { | 
|---|
| 43 |         m_appliedImpulse = btScalar(0.); | 
|---|
| 44 |  | 
|---|
| 45 |         btVector3       normal(0,0,0); | 
|---|
| 46 |  | 
|---|
| 47 |         for (int i=0;i<3;i++) | 
|---|
| 48 |         { | 
|---|
| 49 |                 normal[i] = 1; | 
|---|
| 50 |                 new (&m_jac[i]) btJacobianEntry( | 
|---|
| 51 |                         m_rbA.getCenterOfMassTransform().getBasis().transpose(), | 
|---|
| 52 |                         m_rbB.getCenterOfMassTransform().getBasis().transpose(), | 
|---|
| 53 |                         m_rbA.getCenterOfMassTransform()*m_pivotInA - m_rbA.getCenterOfMassPosition(), | 
|---|
| 54 |                         m_rbB.getCenterOfMassTransform()*m_pivotInB - m_rbB.getCenterOfMassPosition(), | 
|---|
| 55 |                         normal, | 
|---|
| 56 |                         m_rbA.getInvInertiaDiagLocal(), | 
|---|
| 57 |                         m_rbA.getInvMass(), | 
|---|
| 58 |                         m_rbB.getInvInertiaDiagLocal(), | 
|---|
| 59 |                         m_rbB.getInvMass()); | 
|---|
| 60 |                 normal[i] = 0; | 
|---|
| 61 |         } | 
|---|
| 62 |  | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void    btPoint2PointConstraint::solveConstraint(btScalar       timeStep) | 
|---|
| 66 | { | 
|---|
| 67 |         btVector3 pivotAInW = m_rbA.getCenterOfMassTransform()*m_pivotInA; | 
|---|
| 68 |         btVector3 pivotBInW = m_rbB.getCenterOfMassTransform()*m_pivotInB; | 
|---|
| 69 |  | 
|---|
| 70 |  | 
|---|
| 71 |         btVector3 normal(0,0,0); | 
|---|
| 72 |          | 
|---|
| 73 |  | 
|---|
| 74 | //      btVector3 angvelA = m_rbA.getCenterOfMassTransform().getBasis().transpose() * m_rbA.getAngularVelocity(); | 
|---|
| 75 | //      btVector3 angvelB = m_rbB.getCenterOfMassTransform().getBasis().transpose() * m_rbB.getAngularVelocity(); | 
|---|
| 76 |  | 
|---|
| 77 |         for (int i=0;i<3;i++) | 
|---|
| 78 |         {                | 
|---|
| 79 |                 normal[i] = 1; | 
|---|
| 80 |                 btScalar jacDiagABInv = btScalar(1.) / m_jac[i].getDiagonal(); | 
|---|
| 81 |  | 
|---|
| 82 |                 btVector3 rel_pos1 = pivotAInW - m_rbA.getCenterOfMassPosition();  | 
|---|
| 83 |                 btVector3 rel_pos2 = pivotBInW - m_rbB.getCenterOfMassPosition(); | 
|---|
| 84 |                 //this jacobian entry could be re-used for all iterations | 
|---|
| 85 |                  | 
|---|
| 86 |                 btVector3 vel1 = m_rbA.getVelocityInLocalPoint(rel_pos1); | 
|---|
| 87 |                 btVector3 vel2 = m_rbB.getVelocityInLocalPoint(rel_pos2); | 
|---|
| 88 |                 btVector3 vel = vel1 - vel2; | 
|---|
| 89 |                  | 
|---|
| 90 |                 btScalar rel_vel; | 
|---|
| 91 |                 rel_vel = normal.dot(vel); | 
|---|
| 92 |  | 
|---|
| 93 |         /* | 
|---|
| 94 |                 //velocity error (first order error) | 
|---|
| 95 |                 btScalar rel_vel = m_jac[i].getRelativeVelocity(m_rbA.getLinearVelocity(),angvelA, | 
|---|
| 96 |                                                                                                                 m_rbB.getLinearVelocity(),angvelB); | 
|---|
| 97 |         */ | 
|---|
| 98 |          | 
|---|
| 99 |                 //positional error (zeroth order error) | 
|---|
| 100 |                 btScalar depth = -(pivotAInW - pivotBInW).dot(normal); //this is the error projected on the normal | 
|---|
| 101 |                  | 
|---|
| 102 |                 btScalar impulse = depth*m_setting.m_tau/timeStep  * jacDiagABInv -  m_setting.m_damping * rel_vel * jacDiagABInv; | 
|---|
| 103 |  | 
|---|
| 104 |                 btScalar impulseClamp = m_setting.m_impulseClamp; | 
|---|
| 105 |                 if (impulseClamp > 0) | 
|---|
| 106 |                 { | 
|---|
| 107 |                         if (impulse < -impulseClamp)  | 
|---|
| 108 |                                 impulse = -impulseClamp; | 
|---|
| 109 |                         if (impulse > impulseClamp)  | 
|---|
| 110 |                                 impulse = impulseClamp; | 
|---|
| 111 |                 } | 
|---|
| 112 |  | 
|---|
| 113 |                 m_appliedImpulse+=impulse; | 
|---|
| 114 |                 btVector3 impulse_vector = normal * impulse; | 
|---|
| 115 |                 m_rbA.applyImpulse(impulse_vector, pivotAInW - m_rbA.getCenterOfMassPosition()); | 
|---|
| 116 |                 m_rbB.applyImpulse(-impulse_vector, pivotBInW - m_rbB.getCenterOfMassPosition()); | 
|---|
| 117 |                  | 
|---|
| 118 |                 normal[i] = 0; | 
|---|
| 119 |         } | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | void    btPoint2PointConstraint::updateRHS(btScalar     timeStep) | 
|---|
| 123 | { | 
|---|
| 124 |         (void)timeStep; | 
|---|
| 125 |  | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|