[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3Dynamics / ConstraintSolver / b3FixedConstraint.cpp
1
2 #include "b3FixedConstraint.h"
3 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
4 #include "Bullet3Common/b3TransformUtil.h"
5 #include <new>
6
7 b3FixedConstraint::b3FixedConstraint(int rbA, int rbB, const b3Transform& frameInA, const b3Transform& frameInB)
8         : b3TypedConstraint(B3_FIXED_CONSTRAINT_TYPE, rbA, rbB)
9 {
10         m_pivotInA = frameInA.getOrigin();
11         m_pivotInB = frameInB.getOrigin();
12         m_relTargetAB = frameInA.getRotation() * frameInB.getRotation().inverse();
13 }
14
15 b3FixedConstraint::~b3FixedConstraint()
16 {
17 }
18
19 void b3FixedConstraint::getInfo1(b3ConstraintInfo1* info, const b3RigidBodyData* bodies)
20 {
21         info->m_numConstraintRows = 6;
22         info->nub = 6;
23 }
24
25 void b3FixedConstraint::getInfo2(b3ConstraintInfo2* info, const b3RigidBodyData* bodies)
26 {
27         //fix the 3 linear degrees of freedom
28
29         const b3Vector3& worldPosA = bodies[m_rbA].m_pos;
30         const b3Quaternion& worldOrnA = bodies[m_rbA].m_quat;
31         const b3Vector3& worldPosB = bodies[m_rbB].m_pos;
32         const b3Quaternion& worldOrnB = bodies[m_rbB].m_quat;
33
34         info->m_J1linearAxis[0] = 1;
35         info->m_J1linearAxis[info->rowskip + 1] = 1;
36         info->m_J1linearAxis[2 * info->rowskip + 2] = 1;
37
38         b3Vector3 a1 = b3QuatRotate(worldOrnA, m_pivotInA);
39         {
40                 b3Vector3* angular0 = (b3Vector3*)(info->m_J1angularAxis);
41                 b3Vector3* angular1 = (b3Vector3*)(info->m_J1angularAxis + info->rowskip);
42                 b3Vector3* angular2 = (b3Vector3*)(info->m_J1angularAxis + 2 * info->rowskip);
43                 b3Vector3 a1neg = -a1;
44                 a1neg.getSkewSymmetricMatrix(angular0, angular1, angular2);
45         }
46
47         if (info->m_J2linearAxis)
48         {
49                 info->m_J2linearAxis[0] = -1;
50                 info->m_J2linearAxis[info->rowskip + 1] = -1;
51                 info->m_J2linearAxis[2 * info->rowskip + 2] = -1;
52         }
53
54         b3Vector3 a2 = b3QuatRotate(worldOrnB, m_pivotInB);
55
56         {
57                 //      b3Vector3 a2n = -a2;
58                 b3Vector3* angular0 = (b3Vector3*)(info->m_J2angularAxis);
59                 b3Vector3* angular1 = (b3Vector3*)(info->m_J2angularAxis + info->rowskip);
60                 b3Vector3* angular2 = (b3Vector3*)(info->m_J2angularAxis + 2 * info->rowskip);
61                 a2.getSkewSymmetricMatrix(angular0, angular1, angular2);
62         }
63
64         // set right hand side for the linear dofs
65         b3Scalar k = info->fps * info->erp;
66         b3Vector3 linearError = k * (a2 + worldPosB - a1 - worldPosA);
67         int j;
68         for (j = 0; j < 3; j++)
69         {
70                 info->m_constraintError[j * info->rowskip] = linearError[j];
71                 //printf("info->m_constraintError[%d]=%f\n",j,info->m_constraintError[j]);
72         }
73
74         //fix the 3 angular degrees of freedom
75
76         int start_row = 3;
77         int s = info->rowskip;
78         int start_index = start_row * s;
79
80         // 3 rows to make body rotations equal
81         info->m_J1angularAxis[start_index] = 1;
82         info->m_J1angularAxis[start_index + s + 1] = 1;
83         info->m_J1angularAxis[start_index + s * 2 + 2] = 1;
84         if (info->m_J2angularAxis)
85         {
86                 info->m_J2angularAxis[start_index] = -1;
87                 info->m_J2angularAxis[start_index + s + 1] = -1;
88                 info->m_J2angularAxis[start_index + s * 2 + 2] = -1;
89         }
90
91         // set right hand side for the angular dofs
92
93         b3Vector3 diff;
94         b3Scalar angle;
95         b3Quaternion qrelCur = worldOrnA * worldOrnB.inverse();
96
97         b3TransformUtil::calculateDiffAxisAngleQuaternion(m_relTargetAB, qrelCur, diff, angle);
98         diff *= -angle;
99         for (j = 0; j < 3; j++)
100         {
101                 info->m_constraintError[(3 + j) * info->rowskip] = k * diff[j];
102         }
103 }