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