Simple game engine with complete export to scripting language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RigidBodySimulation.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #ifndef BLUECORE_RIGID_BODY_SIMULATION_H
  2. #define BLUECORE_RIGID_BODY_SIMULATION_H
  3. // system includes
  4. #include <list>
  5. #include <map>
  6. #include <vector>
  7. #include "ode/ode.h"
  8. #include "squirrel.h"
  9. #include "trimeshloader.h"
  10. // project includes
  11. #include "Utilities/Referenced.h"
  12. #include "Utilities/Buffer.h"
  13. #include "Utilities/sigslot.h"
  14. #include "Math/Vector.h"
  15. #include "Math/Matrix.h"
  16. #include "Math/Quaternion.h"
  17. #include "ScriptSystem.h"
  18. namespace BlueCore
  19. {
  20. class RigidBodySimulation;
  21. class RigidBody : public Referenced
  22. {
  23. Vector3 _Position;
  24. Quaternion _Orientation;
  25. Vector3 _LinearVelocity;
  26. Vector3 _AngularVelocity;
  27. dBodyID _Body;
  28. dSpaceID _Space;
  29. struct CollisionMesh
  30. {
  31. dTriMeshDataID trimeshdata;
  32. dGeomID geom;
  33. };
  34. std::vector<CollisionMesh> _CollisionMeshes;
  35. HSQOBJECT _CollisionHandler;
  36. weak_ptr<RigidBodySimulation> _Simulation;
  37. public:
  38. RigidBody(RigidBodySimulation* simulation, dWorldID world, dSpaceID space);
  39. ~RigidBody();
  40. const Vector3 &getPosition() const;
  41. void setPosition(const Vector3 &v);
  42. const Vector3 &getLinearVelocity() const;
  43. void setLinearVelocity(const Vector3 &v);
  44. const Quaternion &getOrientation() const;
  45. void setOrientation(const Quaternion &q);
  46. const Vector3 &getAngularVelocity() const;
  47. void applyGlobalForce(const Vector3 &force, const Vector3 &point);
  48. void applyGlobalForce(const Vector3 &force);
  49. void applyLocalForce(const Vector3 &force, const Vector3 &point);
  50. void applyLocalForce(const Vector3 &force);
  51. void applyGlobalMomentum(const Vector3 &momentum, const Vector3 &point);
  52. void applyLocalMomentum(const Vector3 &momentum, const Vector3 &point);
  53. Vector3 getLocalLinearMomentum() const;
  54. Vector3 getGlobalLinearMomentum() const;
  55. Vector3 getLocalAngularMomentum() const;
  56. Vector3 getGlobalAngularMomentum() const;
  57. Vector3 getLocalVelocity() const;
  58. Vector3 getGlobalVelocity() const;
  59. Scalar getMass() const;
  60. sigslot::signal1<RigidBody *> CollisionSignal;
  61. void saveState();
  62. unsigned int addCollisionMesh(const std::string &meshname, Scalar density);
  63. void setCollisionMeshPosition(unsigned int geom, const Vector3 &position);
  64. void
  65. setCollisionMeshRotation(unsigned int geom,
  66. const Quaternion &rotation);
  67. void getCollisionMeshPosition(unsigned int geom, Vector3 &position);
  68. void getCollisionMeshRotation(unsigned int geom, Quaternion &rotation);
  69. void enableCollisionMesh(unsigned int geom);
  70. void disableCollisionMesh(unsigned int geom);
  71. unsigned int getCollisionMeshId(dGeomID geom);
  72. HSQOBJECT getCollisionHandler();
  73. void setCollisionHandler(HSQOBJECT handler);
  74. };
  75. class RigidBodySimulation : public Referenced
  76. {
  77. public:
  78. typedef struct
  79. {
  80. Buffer<float> vertices;
  81. Buffer<int> indices;
  82. } Trimesh;
  83. private:
  84. static void nearCallback(void *data, dGeomID o1, dGeomID o2);
  85. dWorldID _world;
  86. dSpaceID _space;
  87. dJointGroupID _contactgroup;
  88. int _maxContacts;
  89. Scalar _delta;
  90. Scalar _stepSize;
  91. int _steps;
  92. std::list<RigidBody *> _bodies;
  93. std::map<std::string, Trimesh *> _Trimeshes;
  94. ref_ptr<ScriptSystem> _ScriptSystem;
  95. public:
  96. RigidBodySimulation(ScriptSystem *scriptsystem);
  97. ~RigidBodySimulation();
  98. void initializeSingleton();
  99. void shutdownSingleton();
  100. RigidBody *createRigidBody();
  101. void deleteRigidBody(RigidBody *rigid_body);
  102. void removeAll();
  103. int getSteps();
  104. Scalar getStepSize();
  105. int getMaxContacts();
  106. void saveStates();
  107. void updateSteps(Scalar time);
  108. bool step();
  109. sigslot::signal1<Scalar> StepSignal;
  110. const Trimesh *getTrimesh(const std::string &name);
  111. };
  112. }
  113. #endif