Home | History | Annotate | Download | only in ConstraintSolver
      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 #ifdef BT_USE_DOUBLE_PRECISION
     25 #define btGeneric6DofSpringConstraintData2		btGeneric6DofSpringConstraintDoubleData2
     26 #define btGeneric6DofSpringConstraintDataName	"btGeneric6DofSpringConstraintDoubleData2"
     27 #else
     28 #define btGeneric6DofSpringConstraintData2		btGeneric6DofSpringConstraintData
     29 #define btGeneric6DofSpringConstraintDataName	"btGeneric6DofSpringConstraintData"
     30 #endif //BT_USE_DOUBLE_PRECISION
     31 
     32 
     33 
     34 /// Generic 6 DOF constraint that allows to set spring motors to any translational and rotational DOF
     35 
     36 /// DOF index used in enableSpring() and setStiffness() means:
     37 /// 0 : translation X
     38 /// 1 : translation Y
     39 /// 2 : translation Z
     40 /// 3 : rotation X (3rd Euler rotational around new position of X axis, range [-PI+epsilon, PI-epsilon] )
     41 /// 4 : rotation Y (2nd Euler rotational around new position of Y axis, range [-PI/2+epsilon, PI/2-epsilon] )
     42 /// 5 : rotation Z (1st Euler rotational around Z axis, range [-PI+epsilon, PI-epsilon] )
     43 
     44 ATTRIBUTE_ALIGNED16(class) btGeneric6DofSpringConstraint : public btGeneric6DofConstraint
     45 {
     46 protected:
     47 	bool		m_springEnabled[6];
     48 	btScalar	m_equilibriumPoint[6];
     49 	btScalar	m_springStiffness[6];
     50 	btScalar	m_springDamping[6]; // between 0 and 1 (1 == no damping)
     51 	void init();
     52 	void internalUpdateSprings(btConstraintInfo2* info);
     53 public:
     54 
     55 	BT_DECLARE_ALIGNED_ALLOCATOR();
     56 
     57     btGeneric6DofSpringConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB ,bool useLinearReferenceFrameA);
     58     btGeneric6DofSpringConstraint(btRigidBody& rbB, const btTransform& frameInB, bool useLinearReferenceFrameB);
     59 	void enableSpring(int index, bool onOff);
     60 	void setStiffness(int index, btScalar stiffness);
     61 	void setDamping(int index, btScalar damping);
     62 	void setEquilibriumPoint(); // set the current constraint position/orientation as an equilibrium point for all DOF
     63 	void setEquilibriumPoint(int index);  // set the current constraint position/orientation as an equilibrium point for given DOF
     64 	void setEquilibriumPoint(int index, btScalar val);
     65 
     66 	virtual void setAxis( const btVector3& axis1, const btVector3& axis2);
     67 
     68 	virtual void getInfo2 (btConstraintInfo2* info);
     69 
     70 	virtual	int	calculateSerializeBufferSize() const;
     71 	///fills the dataBuffer and returns the struct name (and 0 on failure)
     72 	virtual	const char*	serialize(void* dataBuffer, btSerializer* serializer) const;
     73 
     74 };
     75 
     76 
     77 struct btGeneric6DofSpringConstraintData
     78 {
     79 	btGeneric6DofConstraintData	m_6dofData;
     80 
     81 	int			m_springEnabled[6];
     82 	float		m_equilibriumPoint[6];
     83 	float		m_springStiffness[6];
     84 	float		m_springDamping[6];
     85 };
     86 
     87 struct btGeneric6DofSpringConstraintDoubleData2
     88 {
     89 	btGeneric6DofConstraintDoubleData2	m_6dofData;
     90 
     91 	int			m_springEnabled[6];
     92 	double		m_equilibriumPoint[6];
     93 	double		m_springStiffness[6];
     94 	double		m_springDamping[6];
     95 };
     96 
     97 
     98 SIMD_FORCE_INLINE	int	btGeneric6DofSpringConstraint::calculateSerializeBufferSize() const
     99 {
    100 	return sizeof(btGeneric6DofSpringConstraintData2);
    101 }
    102 
    103 	///fills the dataBuffer and returns the struct name (and 0 on failure)
    104 SIMD_FORCE_INLINE	const char*	btGeneric6DofSpringConstraint::serialize(void* dataBuffer, btSerializer* serializer) const
    105 {
    106 	btGeneric6DofSpringConstraintData2* dof = (btGeneric6DofSpringConstraintData2*)dataBuffer;
    107 	btGeneric6DofConstraint::serialize(&dof->m_6dofData,serializer);
    108 
    109 	int i;
    110 	for (i=0;i<6;i++)
    111 	{
    112 		dof->m_equilibriumPoint[i] = m_equilibriumPoint[i];
    113 		dof->m_springDamping[i] = m_springDamping[i];
    114 		dof->m_springEnabled[i] = m_springEnabled[i]? 1 : 0;
    115 		dof->m_springStiffness[i] = m_springStiffness[i];
    116 	}
    117 	return btGeneric6DofSpringConstraintDataName;
    118 }
    119 
    120 #endif // BT_GENERIC_6DOF_SPRING_CONSTRAINT_H
    121 
    122