00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef CGELSkeletonNodeH
00015 #define CGELSkeletonNodeH
00016
00017 #include "chai3d.h"
00018
00019 using namespace std;
00020
00021
00022
00030
00031
00032
00040
00041 class cGELSkeletonNode
00042 {
00043 public:
00044
00045
00046
00047
00048
00050 cGELSkeletonNode();
00051
00053 ~cGELSkeletonNode();
00054
00055
00056
00057
00058
00059
00061 void setMass(double a_mass);
00062
00064 inline void addForce(cVector3d &a_force)
00065 {
00066 m_force.add(a_force);
00067 }
00068
00070 inline void addTorque(cVector3d &a_torque)
00071 {
00072 m_torque.add(a_torque);
00073 }
00074
00076 inline void setExternalForce(cVector3d &a_force)
00077 {
00078 m_externalForce = a_force;
00079 }
00080
00082 inline void setExternalTorque(cVector3d &a_torque)
00083 {
00084 m_externalTorque = a_torque;
00085 }
00086
00088 inline void computeNextPose(double a_timeInterval)
00089 {
00090 if (!m_fixed)
00091 {
00092
00093 cVector3d damping;
00094 m_vel.mulr(-m_kDampingPos * m_mass, damping);
00095 m_force.add(damping);
00096 m_acc = cDiv(m_mass, cAdd(m_force, m_externalForce));
00097 m_vel = cAdd(m_vel, cMul(a_timeInterval, m_acc));
00098 m_nextPos = cAdd(m_pos, cMul(a_timeInterval, m_vel));
00099 }
00100 else
00101 {
00102 m_nextPos = m_pos;
00103 }
00104
00105
00106 cVector3d dampingAng;
00107 m_angVel.mulr(-m_kDampingRot * m_mass, dampingAng);
00108 m_torque.add(dampingAng);
00109 m_angAcc = cMul((1/m_inertia), m_torque);
00110 m_angVel = cAdd(m_angVel, cMul(a_timeInterval, m_angAcc));
00111
00112 double normAngVel = m_angVel.length();
00113 if (normAngVel < 0.000001)
00114 {
00115 m_nextRot = m_rot;
00116 }
00117 else
00118 {
00119 m_nextRot = cRotate(m_rot, m_angVel, a_timeInterval * normAngVel);
00120 }
00121 }
00122
00124 inline void applyNextPose()
00125 {
00126 m_pos = m_nextPos;
00127 m_rot = m_nextRot;
00128 }
00129
00131 inline void clearForces()
00132 {
00133 if (m_useGravity)
00134 {
00135 m_force = m_gravity;
00136 m_force.mul(m_mass);
00137 }
00138 else
00139 {
00140 m_force.zero();
00141 }
00142 m_torque.zero();
00143 }
00144
00146 inline void clearExternalForces()
00147 {
00148 m_externalForce.zero();
00149 m_externalTorque.zero();
00150 }
00151
00153 inline void render()
00154 {
00155
00156 cMatrixGL mat;
00157 mat.set(m_pos, m_rot);
00158 mat.glMatrixPushMultiply();
00159
00160
00161 m_color.render();
00162 cDrawSphere(m_radius, 12, 12);
00163
00164
00165 if (default_showFrame == true)
00166 {
00167 double frameScale = 3.0 * m_radius;
00168 cDrawFrame(frameScale);
00169 }
00170
00171
00172 mat.glMatrixPop();
00173
00174
00175 glColor4fv( (const float *)&m_color);
00176 cVector3d v = cAdd(m_pos, cMul(1.0/50.0, m_externalForce));
00177 glBegin(GL_LINES);
00178 glVertex3dv( (const double *)&m_pos);
00179 glVertex3dv( (const double *)&v);
00180 glEnd();
00181 }
00182
00183
00184
00185
00186
00187
00189 cColorf m_color;
00190
00191
00192
00193
00194
00195
00197 double m_radius;
00198
00200 double m_mass;
00201
00203 cVector3d m_force;
00204
00206 cVector3d m_torque;
00207
00209 cVector3d m_acc;
00210
00212 cVector3d m_angAcc;
00213
00215 cVector3d m_vel;
00216
00218 cVector3d m_angVel;
00219
00221 double m_kDampingPos;
00222
00224 double m_kDampingRot;
00225
00227 double m_inertia;
00228
00230 bool m_fixed;
00231
00233 bool m_useGravity;
00234
00236 cVector3d m_gravity;
00237
00238
00239
00240
00241
00242
00244 cVector3d m_pos;
00245
00247 cMatrix3d m_rot;
00248
00250 cVector3d m_nextPos;
00251
00253 cMatrix3d m_nextRot;
00254
00255
00256 private:
00257
00258
00259
00260
00261
00263 cVector3d m_externalForce;
00264
00266 cVector3d m_externalTorque;
00267
00268
00269 public:
00270
00271
00272
00273
00274
00276 static double default_radius;
00277
00279 static double default_kDampingPos;
00280
00282 static double default_kDampingRot;
00283
00285 static double default_mass;
00286
00288 static bool default_useGravity;
00289
00291 static cVector3d default_gravity;
00292
00294 static bool default_showFrame;
00295
00297 static cColorf default_color;
00298 };
00299
00300
00301 #endif
00302
00303
00304