[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletDynamics / ConstraintSolver / btHinge2Constraint.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3 Copyright (C) 2006, 2007 Sony Computer Entertainment Inc. 
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 #include "btHinge2Constraint.h"
17 #include "BulletDynamics/Dynamics/btRigidBody.h"
18 #include "LinearMath/btTransformUtil.h"
19
20 // constructor
21 // anchor, axis1 and axis2 are in world coordinate system
22 // axis1 must be orthogonal to axis2
23 btHinge2Constraint::btHinge2Constraint(btRigidBody& rbA, btRigidBody& rbB, btVector3& anchor, btVector3& axis1, btVector3& axis2)
24         : btGeneric6DofSpring2Constraint(rbA, rbB, btTransform::getIdentity(), btTransform::getIdentity(), RO_XYZ),
25           m_anchor(anchor),
26           m_axis1(axis1),
27           m_axis2(axis2)
28 {
29         // build frame basis
30         // 6DOF constraint uses Euler angles and to define limits
31         // it is assumed that rotational order is :
32         // Z - first, allowed limits are (-PI,PI);
33         // new position of Y - second (allowed limits are (-PI/2 + epsilon, PI/2 - epsilon), where epsilon is a small positive number
34         // used to prevent constraint from instability on poles;
35         // new position of X, allowed limits are (-PI,PI);
36         // So to simulate ODE Universal joint we should use parent axis as Z, child axis as Y and limit all other DOFs
37         // Build the frame in world coordinate system first
38         btVector3 zAxis = axis1.normalize();
39         btVector3 xAxis = axis2.normalize();
40         btVector3 yAxis = zAxis.cross(xAxis);  // we want right coordinate system
41         btTransform frameInW;
42         frameInW.setIdentity();
43         frameInW.getBasis().setValue(xAxis[0], yAxis[0], zAxis[0],
44                                                                  xAxis[1], yAxis[1], zAxis[1],
45                                                                  xAxis[2], yAxis[2], zAxis[2]);
46         frameInW.setOrigin(anchor);
47         // now get constraint frame in local coordinate systems
48         m_frameInA = rbA.getCenterOfMassTransform().inverse() * frameInW;
49         m_frameInB = rbB.getCenterOfMassTransform().inverse() * frameInW;
50         // sei limits
51         setLinearLowerLimit(btVector3(0.f, 0.f, -1.f));
52         setLinearUpperLimit(btVector3(0.f, 0.f, 1.f));
53         // like front wheels of a car
54         setAngularLowerLimit(btVector3(1.f, 0.f, -SIMD_HALF_PI * 0.5f));
55         setAngularUpperLimit(btVector3(-1.f, 0.f, SIMD_HALF_PI * 0.5f));
56         // enable suspension
57         enableSpring(2, true);
58         setStiffness(2, SIMD_PI * SIMD_PI * 4.f);
59         setDamping(2, 0.01f);
60         setEquilibriumPoint();
61 }