| 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 "btContactConstraint.h" | 
|---|
| 18 | #include "BulletDynamics/Dynamics/btRigidBody.h" | 
|---|
| 19 | #include "LinearMath/btVector3.h" | 
|---|
| 20 | #include "btJacobianEntry.h" | 
|---|
| 21 | #include "btContactSolverInfo.h" | 
|---|
| 22 | #include "LinearMath/btMinMax.h" | 
|---|
| 23 | #include "BulletCollision/NarrowPhaseCollision/btManifoldPoint.h" | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | btContactConstraint::btContactConstraint(btPersistentManifold* contactManifold,btRigidBody& rbA,btRigidBody& rbB) | 
|---|
| 28 | :btTypedConstraint(CONTACT_CONSTRAINT_TYPE,rbA,rbB), | 
|---|
| 29 |         m_contactManifold(*contactManifold) | 
|---|
| 30 | { | 
|---|
| 31 |  | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | btContactConstraint::~btContactConstraint() | 
|---|
| 35 | { | 
|---|
| 36 |  | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | void    btContactConstraint::setContactManifold(btPersistentManifold* contactManifold) | 
|---|
| 40 | { | 
|---|
| 41 |         m_contactManifold = *contactManifold; | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | void btContactConstraint::getInfo1 (btConstraintInfo1* info) | 
|---|
| 45 | { | 
|---|
| 46 |  | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | void btContactConstraint::getInfo2 (btConstraintInfo2* info) | 
|---|
| 50 | { | 
|---|
| 51 |  | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | void    btContactConstraint::buildJacobian() | 
|---|
| 55 | { | 
|---|
| 56 |  | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | #include "btContactConstraint.h" | 
|---|
| 64 | #include "BulletDynamics/Dynamics/btRigidBody.h" | 
|---|
| 65 | #include "LinearMath/btVector3.h" | 
|---|
| 66 | #include "btJacobianEntry.h" | 
|---|
| 67 | #include "btContactSolverInfo.h" | 
|---|
| 68 | #include "LinearMath/btMinMax.h" | 
|---|
| 69 | #include "BulletCollision/NarrowPhaseCollision/btManifoldPoint.h" | 
|---|
| 70 |  | 
|---|
| 71 | #define ASSERT2 btAssert | 
|---|
| 72 |  | 
|---|
| 73 | #define USE_INTERNAL_APPLY_IMPULSE 1 | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | //bilateral constraint between two dynamic objects | 
|---|
| 77 | void resolveSingleBilateral(btRigidBody& body1, const btVector3& pos1, | 
|---|
| 78 |                       btRigidBody& body2, const btVector3& pos2, | 
|---|
| 79 |                       btScalar distance, const btVector3& normal,btScalar& impulse ,btScalar timeStep) | 
|---|
| 80 | { | 
|---|
| 81 |         (void)timeStep; | 
|---|
| 82 |         (void)distance; | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 |         btScalar normalLenSqr = normal.length2(); | 
|---|
| 86 |         ASSERT2(btFabs(normalLenSqr) < btScalar(1.1)); | 
|---|
| 87 |         if (normalLenSqr > btScalar(1.1)) | 
|---|
| 88 |         { | 
|---|
| 89 |                 impulse = btScalar(0.); | 
|---|
| 90 |                 return; | 
|---|
| 91 |         } | 
|---|
| 92 |         btVector3 rel_pos1 = pos1 - body1.getCenterOfMassPosition();  | 
|---|
| 93 |         btVector3 rel_pos2 = pos2 - body2.getCenterOfMassPosition(); | 
|---|
| 94 |         //this jacobian entry could be re-used for all iterations | 
|---|
| 95 |          | 
|---|
| 96 |         btVector3 vel1 = body1.getVelocityInLocalPoint(rel_pos1); | 
|---|
| 97 |         btVector3 vel2 = body2.getVelocityInLocalPoint(rel_pos2); | 
|---|
| 98 |         btVector3 vel = vel1 - vel2; | 
|---|
| 99 |          | 
|---|
| 100 |  | 
|---|
| 101 |            btJacobianEntry jac(body1.getCenterOfMassTransform().getBasis().transpose(), | 
|---|
| 102 |                 body2.getCenterOfMassTransform().getBasis().transpose(), | 
|---|
| 103 |                 rel_pos1,rel_pos2,normal,body1.getInvInertiaDiagLocal(),body1.getInvMass(), | 
|---|
| 104 |                 body2.getInvInertiaDiagLocal(),body2.getInvMass()); | 
|---|
| 105 |  | 
|---|
| 106 |         btScalar jacDiagAB = jac.getDiagonal(); | 
|---|
| 107 |         btScalar jacDiagABInv = btScalar(1.) / jacDiagAB; | 
|---|
| 108 |          | 
|---|
| 109 |           btScalar rel_vel = jac.getRelativeVelocity( | 
|---|
| 110 |                 body1.getLinearVelocity(), | 
|---|
| 111 |                 body1.getCenterOfMassTransform().getBasis().transpose() * body1.getAngularVelocity(), | 
|---|
| 112 |                 body2.getLinearVelocity(), | 
|---|
| 113 |                 body2.getCenterOfMassTransform().getBasis().transpose() * body2.getAngularVelocity());  | 
|---|
| 114 |         btScalar a; | 
|---|
| 115 |         a=jacDiagABInv; | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 |         rel_vel = normal.dot(vel); | 
|---|
| 119 |          | 
|---|
| 120 |         //todo: move this into proper structure | 
|---|
| 121 |         btScalar contactDamping = btScalar(0.2); | 
|---|
| 122 |  | 
|---|
| 123 | #ifdef ONLY_USE_LINEAR_MASS | 
|---|
| 124 |         btScalar massTerm = btScalar(1.) / (body1.getInvMass() + body2.getInvMass()); | 
|---|
| 125 |         impulse = - contactDamping * rel_vel * massTerm; | 
|---|
| 126 | #else    | 
|---|
| 127 |         btScalar velocityImpulse = -contactDamping * rel_vel * jacDiagABInv; | 
|---|
| 128 |         impulse = velocityImpulse; | 
|---|
| 129 | #endif | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 |  | 
|---|