Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletDynamics / ConstraintSolver / btGeneric6DofSpringConstraint.h
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 #ifndef BT_GENERIC_6DOF_SPRING_CONSTRAINT_H
17 #define BT_GENERIC_6DOF_SPRING_CONSTRAINT_H
18
19
20 #include "LinearMath/btVector3.h"
21 #include "btTypedConstraint.h"
22 #include "btGeneric6DofConstraint.h"
23
24
25 /// Generic 6 DOF constraint that allows to set spring motors to any translational and rotational DOF
26
27 /// DOF index used in enableSpring() and setStiffness() means:
28 /// 0 : translation X
29 /// 1 : translation Y
30 /// 2 : translation Z
31 /// 3 : rotation X (3rd Euler rotational around new position of X axis, range [-PI+epsilon, PI-epsilon] )
32 /// 4 : rotation Y (2nd Euler rotational around new position of Y axis, range [-PI/2+epsilon, PI/2-epsilon] )
33 /// 5 : rotation Z (1st Euler rotational around Z axis, range [-PI+epsilon, PI-epsilon] )
34
35 ATTRIBUTE_ALIGNED16(class) btGeneric6DofSpringConstraint : public btGeneric6DofConstraint
36 {
37 protected:
38         bool            m_springEnabled[6];
39         btScalar        m_equilibriumPoint[6];
40         btScalar        m_springStiffness[6];
41         btScalar        m_springDamping[6]; // between 0 and 1 (1 == no damping)
42         void init();
43         void internalUpdateSprings(btConstraintInfo2* info);
44 public: 
45         
46         BT_DECLARE_ALIGNED_ALLOCATOR();
47         
48     btGeneric6DofSpringConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB ,bool useLinearReferenceFrameA);
49     btGeneric6DofSpringConstraint(btRigidBody& rbB, const btTransform& frameInB, bool useLinearReferenceFrameB);
50         void enableSpring(int index, bool onOff);
51         void setStiffness(int index, btScalar stiffness);
52         void setDamping(int index, btScalar damping);
53         void setEquilibriumPoint(); // set the current constraint position/orientation as an equilibrium point for all DOF
54         void setEquilibriumPoint(int index);  // set the current constraint position/orientation as an equilibrium point for given DOF
55         void setEquilibriumPoint(int index, btScalar val);
56
57         virtual void setAxis( const btVector3& axis1, const btVector3& axis2);
58
59         virtual void getInfo2 (btConstraintInfo2* info);
60
61         virtual int     calculateSerializeBufferSize() const;
62         ///fills the dataBuffer and returns the struct name (and 0 on failure)
63         virtual const char*     serialize(void* dataBuffer, btSerializer* serializer) const;
64
65 };
66
67
68 ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
69 struct btGeneric6DofSpringConstraintData
70 {
71         btGeneric6DofConstraintData     m_6dofData;
72         
73         int                     m_springEnabled[6];
74         float           m_equilibriumPoint[6];
75         float           m_springStiffness[6];
76         float           m_springDamping[6];
77 };
78
79 SIMD_FORCE_INLINE       int     btGeneric6DofSpringConstraint::calculateSerializeBufferSize() const
80 {
81         return sizeof(btGeneric6DofSpringConstraintData);
82 }
83
84         ///fills the dataBuffer and returns the struct name (and 0 on failure)
85 SIMD_FORCE_INLINE       const char*     btGeneric6DofSpringConstraint::serialize(void* dataBuffer, btSerializer* serializer) const
86 {
87         btGeneric6DofSpringConstraintData* dof = (btGeneric6DofSpringConstraintData*)dataBuffer;
88         btGeneric6DofConstraint::serialize(&dof->m_6dofData,serializer);
89
90         int i;
91         for (i=0;i<6;i++)
92         {
93                 dof->m_equilibriumPoint[i] = (float)m_equilibriumPoint[i];
94                 dof->m_springDamping[i] = (float)m_springDamping[i];
95                 dof->m_springEnabled[i] = m_springEnabled[i]? 1 : 0;
96                 dof->m_springStiffness[i] = (float)m_springStiffness[i];
97         }
98         return "btGeneric6DofSpringConstraintData";
99 }
100
101 #endif // BT_GENERIC_6DOF_SPRING_CONSTRAINT_H
102