00001 #ifndef PH_JOINTMULTI_H
00002 #define PH_JOINTMULTI_H
00003
00004 #include <Physics/PHJoint.h>
00005
00006 namespace Spr{;
00007
00008
00009 template <int NDOF>
00010 class PHJointMulti:public PHJointBase{
00011 public:
00012 typedef PTM::TVector<NDOF, double> AVec;
00013 AVec torque;
00014 AVec accel;
00015 AVec delta_position;
00016 AVec velocity;
00017 void AddTorque(AVec t){ torque+=t; }
00018 void SetTorque(AVec t){ torque=t; }
00019 AVec GetTorque(){ return torque; }
00020 AVec GetVelocity(){ return velocity; }
00021
00022 virtual int GetJointDof(){ return NDOF; }
00023
00024 virtual double GetJointVelocity(int i){ return velocity[i]; }
00025
00026 virtual double GetJointAccel(int i){ return accel[i]; }
00027
00028 virtual double GetJointTorque(int i){ return torque[i]; }
00029
00030 virtual void SetJointTorque(double v, int i){ torque[i] = v; }
00031
00032 virtual void AddJointTorque(double v, int i){ torque[i] += v; }
00033
00034 void CompArticulatedInertia(double dt){
00035
00036 PHJointBase::CompArticulatedInertia(dt);
00037 Iss = (S_tr * Ia * S).inv();
00038
00039
00040 OfParent(&PHJointMulti::Ia) +=
00041 pXc_Mat_cXp( Ia - (Ia * S*Iss*S_tr * Ia) );
00042
00043 OfParent(&PHJointMulti::Za) +=
00044 pXc_Vec( Z_plus_Ia_c + Ia*S*Iss*(torque - S_tr*Z_plus_Ia_c) );
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 void PreIntegrate(SGScene* scene){
00056 double dt = scene->GetTimeStep();
00057 a_p = cXp_Vec(OfParent(&PHJointMulti::a));
00058
00059 if (intType == SYMPLETIC){
00060
00061
00062
00063 accel = Iss*(torque - S_tr*(Z_plus_Ia_c + Ia*a_p));
00064
00065 velocity += accel * scene->GetTimeStep();
00066
00067 delta_position = velocity * scene->GetTimeStep();
00068 }else{
00069
00070 accel = Iss*(torque - S_tr*(Z_plus_Ia_c + Ia*a_p));
00071
00072 delta_position = (velocity + 0.5 * accel * dt) * dt;
00073
00074 velocity += accel * dt;
00075 }
00076 velocity *= scene->GetVelocityLossPerStep();
00077
00078 a = a_p + c + S*accel;
00079 if (velocity.norm() > PHJOINT_MAX_VELOCITY){
00080 DSTR << "The velocity of " << GetName() << ":" << velocity << "was limited" << std::endl;
00081 if (_finite(velocity[0]) ){
00082 velocity = PHJOINT_MAX_VELOCITY * velocity.unit();
00083 }else{
00084 velocity.clear();
00085 }
00086 }
00087 }
00088 void CalcAccel(double dt){
00089 GetParent()->CalcAccel(dt);
00090 a_p = cXp_Vec(OfParent(&PHJointMulti::a));
00091
00092 accel = Iss*(torque - S_tr*(Z_plus_Ia_c + Ia*a_p));
00093
00094 a = a_p + c + S*accel;
00095 }
00096 protected:
00097 typedef PTM::TMatrixCol<6, NDOF, double> SMat;
00098 typedef PTM::TMatrixCol<NDOF, 6, double> SMatTr;
00099 SMat S;
00100 SMatTr S_tr;
00101 PTM::TMatrixCol<NDOF, NDOF, double> Iss;
00102 virtual void Reset(){
00103 accel.clear();
00104 torque.clear();
00105 PHJointBase::Reset();
00106 }
00107
00108
00109 virtual void LoadState(const SGBehaviorStates& states);
00110
00111 virtual void SaveState(SGBehaviorStates& states) const;
00112 */
00113 virtual void ClearTorqueRecursive(){
00114 PHJointBase::ClearTorqueRecursive();
00115 torque.clear();
00116 }
00117 };
00118
00119
00120
00121 class PHJointBall:public PHJointMulti<3>{
00122 double massFactor;
00123 public:
00124 SGOBJECTDEF(PHJointBall);
00125 Quaterniond position;
00126 Vec3d center;
00127 double minDot;
00128 double minTwist;
00129 double maxTwist;
00130 double conelimitK;
00131 double conelimitB;
00132
00133 PHJointBall();
00134
00135 virtual double GetJointPosition(int i){ return position.rotation()[i]; }
00136
00137 protected:
00138 virtual void Integrate(SGScene* scene);
00139 virtual void CompJointAxis();
00140 virtual void CompRelativePosition();
00141 virtual void CompRelativeVelocity();
00142 virtual void CompCoriolisAccel();
00143 virtual void Loaded(SGScene* s);
00144 virtual double MassFactor();
00145
00146 virtual void LoadState(const SGBehaviorStates& states);
00147
00148 virtual void SaveState(SGBehaviorStates& states) const;
00149 };
00150
00151
00152 class PHJointUniversal:public PHJointMulti<2>{
00153 public:
00154 SGOBJECTDEF(PHJointUniversal);
00155 Vec2d position;
00156
00157 virtual double GetJointPosition(int i){ return position[i]; }
00158
00159 virtual void Integrate(SGScene* scene);
00160 virtual void CompJointAxis();
00161 virtual void CompRelativePosition();
00162 virtual void CompRelativeVelocity();
00163 virtual void CompCoriolisAccel();
00164
00165 virtual void LoadState(const SGBehaviorStates& states);
00166
00167 virtual void SaveState(SGBehaviorStates& states) const;
00168 };
00169
00170 }
00171 #endif