[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3OpenCL / RigidBody / b3GpuGenericConstraint.cpp
1 /*
2 Copyright (c) 2012 Advanced Micro Devices, Inc.  
3
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose, 
7 including commercial applications, and to alter it and redistribute it freely, 
8 subject to the following restrictions:
9
10 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
13 */
14 //Originally written by Erwin Coumans
15
16 #include "b3GpuGenericConstraint.h"
17 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
18
19 #include <new>
20 #include "Bullet3Common/b3Transform.h"
21
22 void b3GpuGenericConstraint::getInfo1(unsigned int* info, const b3RigidBodyData* bodies)
23 {
24         switch (m_constraintType)
25         {
26                 case B3_GPU_POINT2POINT_CONSTRAINT_TYPE:
27                 {
28                         *info = 3;
29                         break;
30                 };
31                 default:
32                 {
33                         b3Assert(0);
34                 }
35         };
36 }
37
38 void getInfo2Point2Point(b3GpuGenericConstraint* constraint, b3GpuConstraintInfo2* info, const b3RigidBodyData* bodies)
39 {
40         b3Transform trA;
41         trA.setIdentity();
42         trA.setOrigin(bodies[constraint->m_rbA].m_pos);
43         trA.setRotation(bodies[constraint->m_rbA].m_quat);
44
45         b3Transform trB;
46         trB.setIdentity();
47         trB.setOrigin(bodies[constraint->m_rbB].m_pos);
48         trB.setRotation(bodies[constraint->m_rbB].m_quat);
49
50         // anchor points in global coordinates with respect to body PORs.
51
52         // set jacobian
53         info->m_J1linearAxis[0] = 1;
54         info->m_J1linearAxis[info->rowskip + 1] = 1;
55         info->m_J1linearAxis[2 * info->rowskip + 2] = 1;
56
57         b3Vector3 a1 = trA.getBasis() * constraint->getPivotInA();
58         //b3Vector3 a1a = b3QuatRotate(trA.getRotation(),constraint->getPivotInA());
59
60         {
61                 b3Vector3* angular0 = (b3Vector3*)(info->m_J1angularAxis);
62                 b3Vector3* angular1 = (b3Vector3*)(info->m_J1angularAxis + info->rowskip);
63                 b3Vector3* angular2 = (b3Vector3*)(info->m_J1angularAxis + 2 * info->rowskip);
64                 b3Vector3 a1neg = -a1;
65                 a1neg.getSkewSymmetricMatrix(angular0, angular1, angular2);
66         }
67
68         if (info->m_J2linearAxis)
69         {
70                 info->m_J2linearAxis[0] = -1;
71                 info->m_J2linearAxis[info->rowskip + 1] = -1;
72                 info->m_J2linearAxis[2 * info->rowskip + 2] = -1;
73         }
74
75         b3Vector3 a2 = trB.getBasis() * constraint->getPivotInB();
76
77         {
78                 //      b3Vector3 a2n = -a2;
79                 b3Vector3* angular0 = (b3Vector3*)(info->m_J2angularAxis);
80                 b3Vector3* angular1 = (b3Vector3*)(info->m_J2angularAxis + info->rowskip);
81                 b3Vector3* angular2 = (b3Vector3*)(info->m_J2angularAxis + 2 * info->rowskip);
82                 a2.getSkewSymmetricMatrix(angular0, angular1, angular2);
83         }
84
85         // set right hand side
86         //      b3Scalar currERP = (m_flags & B3_P2P_FLAGS_ERP) ? m_erp : info->erp;
87         b3Scalar currERP = info->erp;
88
89         b3Scalar k = info->fps * currERP;
90         int j;
91         for (j = 0; j < 3; j++)
92         {
93                 info->m_constraintError[j * info->rowskip] = k * (a2[j] + trB.getOrigin()[j] - a1[j] - trA.getOrigin()[j]);
94                 //printf("info->m_constraintError[%d]=%f\n",j,info->m_constraintError[j]);
95         }
96 #if 0
97         if(m_flags & B3_P2P_FLAGS_CFM)
98         {
99                 for (j=0; j<3; j++)
100                 {
101                         info->cfm[j*info->rowskip] = m_cfm;
102                 }
103         }
104 #endif
105
106 #if 0
107         b3Scalar impulseClamp = m_setting.m_impulseClamp;//
108         for (j=0; j<3; j++)
109     {
110                 if (m_setting.m_impulseClamp > 0)
111                 {
112                         info->m_lowerLimit[j*info->rowskip] = -impulseClamp;
113                         info->m_upperLimit[j*info->rowskip] = impulseClamp;
114                 }
115         }
116         info->m_damping = m_setting.m_damping;
117 #endif
118 }
119
120 void b3GpuGenericConstraint::getInfo2(b3GpuConstraintInfo2* info, const b3RigidBodyData* bodies)
121 {
122         switch (m_constraintType)
123         {
124                 case B3_GPU_POINT2POINT_CONSTRAINT_TYPE:
125                 {
126                         getInfo2Point2Point(this, info, bodies);
127                         break;
128                 };
129                 default:
130                 {
131                         b3Assert(0);
132                 }
133         };
134 }