00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00025
00026 #ifndef Dynamic_H
00027 #define Dynamic_H
00028
00029 #include "H3DNetworkingUtils/Config.h"
00030 #include "H3D/DynamicTransform.h"
00031 #include "H3D/MFVec3f.h"
00032 #include "H3D/SFVec3f.h"
00033 #include "H3D/SFMatrix4f.h"
00034 #include "H3D/SFInt32.h"
00035 #include "H3D/MFInt32.h"
00036
00037 namespace H3DNetworkingUtils {
00038
00047
00048 class H3D_NETWORKING_UTILS_DLL_SPEC Dynamic : public H3D::DynamicTransform {
00049
00050 public:
00051
00052 struct SumMFVec3f;
00053 class SumForces;
00054 class SumTorques;
00055 class CalcTorque;
00056
00057 typedef H3D::TypedMFNodeObject<H3D::X3DGeometryNode> MFGeometryNode;
00058
00060 Dynamic( H3D::Inst< AddChildren > _addChildren = 0,
00061 H3D::Inst< RemoveChildren > _removeChildren = 0,
00062 H3D::Inst< MFChild > _children = 0,
00063 H3D::Inst< H3D::SFNode > _metadata = 0,
00064 H3D::Inst< SFBound > _bound = 0,
00065 H3D::Inst< H3D::SFVec3f > _bboxCenter = 0,
00066 H3D::Inst< H3D::SFVec3f > _bboxSize = 0,
00067 H3D::Inst< SFTransformedBound > _transformedBound = 0,
00068 H3D::Inst< SFMatrix4f > _matrix = 0,
00069 H3D::Inst< SFMatrix4f > _accumulatedForward = 0,
00070 H3D::Inst< SFMatrix4f > _accumulatedInverse = 0,
00071 H3D::Inst< H3D::SFVec3f > _position = 0,
00072 H3D::Inst< H3D::SFRotation > _orientation = 0,
00073 H3D::Inst< SFVelocity > _velocity = 0,
00074 H3D::Inst< H3D::SFVec3f > _momentum = 0,
00075
00076 H3D::Inst< H3D::SFVec3f > _force = 0,
00077 H3D::Inst< SFAngularVelocity > _angularVelocity = 0,
00078 H3D::Inst< H3D::SFVec3f > _angularMomentum = 0,
00079 H3D::Inst< SFSpin > _spin = 0,
00080 H3D::Inst< H3D::SFVec3f > _torque = 0,
00081 H3D::Inst< H3D::SFFloat > _mass = 0,
00082 H3D::Inst< H3D::SFMatrix3f > _inertiaTensor = 0,
00083 H3D::Inst< SFMotion > _motion = 0,
00084
00085 H3D::Inst<H3D::MFVec3f > _contactForces = 0,
00086 H3D::Inst<H3D::MFVec3f > _contactTorques = 0,
00087 H3D::Inst<SumMFVec3f > _totalContactForce = 0,
00088 H3D::Inst<CalcTorque > _totalContactTorque = 0,
00089 H3D::Inst<SumForces > _externalForces = 0,
00090 H3D::Inst<SumTorques > _externalTorques = 0,
00091 H3D::Inst<H3D::SFVec3f > _freedom = 0,
00092 H3D::Inst<H3D::SFVec3f > _angularFreedom = 0,
00093 H3D::Inst<H3D::SFFloat > _linearDamping = 0,
00094 H3D::Inst<H3D::SFFloat > _rotationalDamping = 0,
00095 H3D::Inst<H3D::SFFloat > _linearSpringStiffness = 0,
00096 H3D::Inst<H3D::SFFloat > _angularSpringStiffness = 0);
00097
00098
00099 static H3D::H3DNodeDatabase database;
00100
00105 auto_ptr<H3D::MFVec3f> contactForces;
00106
00111 auto_ptr<H3D::MFVec3f> contactTorques;
00112
00124 auto_ptr<H3D::SFVec3f> freedom;
00125
00134 auto_ptr<H3D::SFVec3f> angularFreedom;
00135
00140 auto_ptr<H3D::SFFloat> linearDamping;
00141
00146 auto_ptr<H3D::SFFloat> rotationalDamping;
00147
00152 auto_ptr<SumMFVec3f> totalContactForce;
00153
00158 auto_ptr<CalcTorque> totalContactTorque;
00159
00165 auto_ptr<SumForces> externalForces;
00166
00172 auto_ptr<SumTorques> externalTorques;
00173
00179 auto_ptr<H3D::SFFloat> linearSpringStiffness;
00180
00186 auto_ptr<H3D::SFFloat> angularSpringStiffness;
00187
00189 virtual void addImpulse(const H3D::Vec3f &f) const {
00190 impulse_forces->setValue(impulse_forces->getValue() + f);
00191 }
00192
00194 virtual void addImpulseTorque(const H3D::Vec3f &t) const {
00195 impulse_torques->setValue(impulse_torques->getValue() + t);
00196 }
00197
00200 struct H3D_NETWORKING_UTILS_DLL_SPEC SumMFVec3f : public H3D::TypedField<H3D::SFVec3f, H3D::MFVec3f> {
00201 virtual void update() {
00202 value = H3D::Vec3f(0.0f, 0.0f, 0.0f);
00203 vector<H3D::Vec3f> vec = static_cast<H3D::MFVec3f*>(event.ptr)->getValue();
00204 for (u_int i = 0; i < vec.size(); i++) {
00205 value += vec[i];
00206 }
00207 }
00208 };
00209
00211 class H3D_NETWORKING_UTILS_DLL_SPEC SumForces : public H3D::TypedField< H3D::SFVec3f, H3D::MFVec3f, H3D::AnyNumber<H3D::SFVec3f > > {
00212 public:
00213 SumForces() : dynP (0) {}
00214 virtual void update();
00215 Dynamic * dynP;
00216 };
00217
00219 class H3D_NETWORKING_UTILS_DLL_SPEC SumTorques : public H3D::SFVec3f {
00220 public:
00221 SumTorques() : dynP (0) {}
00222 virtual void update();
00223 Dynamic * dynP;
00224 };
00225
00233 class H3D_NETWORKING_UTILS_DLL_SPEC CalcTorque : public H3D::TypedField< H3D::SFVec3f, H3D::Types<H3D::SFMatrix4f, H3D::SFMatrix4f, H3D::MFVec3f, H3D::MFVec3f, H3D::SFVec3f > > {
00234 public:
00235 virtual void update();
00236 };
00237
00239 class H3D_NETWORKING_UTILS_DLL_SPEC InvertForces : public H3D::TypedField< H3D::MFVec3f, H3D::Types<H3D::MFVec3f, H3D::SFMatrix4f > > {
00240 public:
00241 virtual void update();
00242 };
00243
00244
00251 class H3D_NETWORKING_UTILS_DLL_SPEC LinDamper : public H3D::TypedField<H3D::SFVec3f, H3D::Types<H3D::SFVec3f, H3D::SFFloat, H3D::SFFloat, H3D::SFFloat> > {
00252 virtual void update();
00253 };
00254
00259 class H3D_NETWORKING_UTILS_DLL_SPEC RotDamper : public H3D::TypedField< H3D::SFVec3f, H3D::Types<H3D::SFVec3f, H3D::SFFloat> > {
00260 virtual void update() {
00261 H3D::Vec3f angVel = static_cast<H3D::SFVec3f*>(routes_in[0])->getValue();
00262 H3D::H3DFloat damping = static_cast<H3D::SFFloat*>(routes_in[1])->getValue();
00263 value = -angVel * damping;
00264 }
00265 };
00266
00267 virtual void traverseSG(H3D::TraverseInfo & ti);
00268
00269 virtual void initialize();
00270
00280 class H3D_NETWORKING_UTILS_DLL_SPEC LinSpring : public H3D::TypedField<H3D::SFVec3f,
00281 H3D::Types<H3D::SFBool, H3D::SFVec3f, H3D::SFVec3f, H3D::SFFloat, H3D::SFFloat, H3D::SFFloat> > {
00282 virtual void update();
00283 };
00284
00285 protected:
00286
00287 virtual void searchGroup(X3DGroupingNode* groupP);
00288
00289
00290
00291
00292
00293 class AngSpring : public H3D::TypedField<H3D::SFVec3f, H3D::Types<H3D::SFRotation, H3D::SFFloat> > {
00294 virtual void update() {
00295 H3D::H3DFloat stiffness = static_cast<H3D::SFFloat*>(routes_in[1])->getValue();
00296 if (stiffness > 0.0f) {
00297 H3D::Rotation orn = -static_cast<H3D::SFRotation*>(routes_in[0])->getValue();
00298 H3D::Rotation diff = H3D::Rotation(1,0,0,0) * orn;
00299 value = diff.axis * diff.angle * stiffness;
00300 } else {
00301 value = H3D::Vec3f(0.0f,0.0f,0.0f);
00302 }
00303 }
00304 };
00305 protected:
00306 auto_ptr<H3D::SFVec3f> impulse_forces;
00307 auto_ptr<H3D::SFVec3f> impulse_torques;
00308
00309 auto_ptr<MFGeometryNode> geometries;
00310 auto_ptr<InvertForces> invertForces;
00311 auto_ptr<H3D::MFVec3f> contactPoints;
00312 auto_ptr<LinDamper> lin_damper;
00313 auto_ptr<RotDamper> rot_damper;
00314 auto_ptr<LinSpring> lin_spring;
00315 auto_ptr<AngSpring> ang_spring;
00316 auto_ptr<H3D::SFVec3f> anchor_point;
00317 auto_ptr<H3D::SFFloat> anchor_slack_dist;
00318 auto_ptr<H3D::SFFloat> lin_spring_rest_length;
00319 auto_ptr<H3D::SFBool> lin_spring_enabled;
00320 auto_ptr<H3D::SFFloat> frame_period;
00321 H3D::H3DTime prev_time;
00322
00323 };
00324
00325 }
00326 #endif