physics_body.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*************************************************************************/
  2. /* physics_body.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef PHYSICS_BODY__H
  30. #define PHYSICS_BODY__H
  31. #include "scene/3d/collision_object.h"
  32. #include "servers/physics_server.h"
  33. #include "vset.h"
  34. class PhysicsBody : public CollisionObject {
  35. OBJ_TYPE(PhysicsBody,CollisionObject);
  36. protected:
  37. void _notification(int p_what);
  38. PhysicsBody(PhysicsServer::BodyMode p_mode);
  39. public:
  40. virtual Vector3 get_linear_velocity() const;
  41. virtual Vector3 get_angular_velocity() const;
  42. virtual float get_inverse_mass() const;
  43. PhysicsBody();
  44. };
  45. class StaticBody : public PhysicsBody {
  46. OBJ_TYPE(StaticBody,PhysicsBody);
  47. Transform *pre_xform;
  48. //RID query;
  49. bool setting;
  50. bool pending;
  51. bool simulating_motion;
  52. Vector3 constant_linear_velocity;
  53. Vector3 constant_angular_velocity;
  54. void _update_xform();
  55. void _state_notify(Object *p_object);
  56. protected:
  57. void _notification(int p_what);
  58. static void _bind_methods();
  59. public:
  60. void set_simulate_motion(bool p_enable);
  61. bool is_simulating_motion() const;
  62. void set_constant_linear_velocity(const Vector3& p_vel);
  63. void set_constant_angular_velocity(const Vector3& p_vel);
  64. Vector3 get_constant_linear_velocity() const;
  65. Vector3 get_constant_angular_velocity() const;
  66. StaticBody();
  67. ~StaticBody();
  68. };
  69. class RigidBody : public PhysicsBody {
  70. OBJ_TYPE(RigidBody,PhysicsBody);
  71. public:
  72. enum Mode {
  73. MODE_RIGID,
  74. MODE_STATIC,
  75. MODE_CHARACTER,
  76. MODE_KINEMATIC,
  77. };
  78. enum AxisLock {
  79. AXIS_LOCK_DISABLED,
  80. AXIS_LOCK_X,
  81. AXIS_LOCK_Y,
  82. AXIS_LOCK_Z,
  83. };
  84. private:
  85. bool can_sleep;
  86. PhysicsDirectBodyState *state;
  87. Mode mode;
  88. real_t bounce;
  89. real_t mass;
  90. real_t friction;
  91. Vector3 linear_velocity;
  92. Vector3 angular_velocity;
  93. bool active;
  94. bool ccd;
  95. AxisLock axis_lock;
  96. int max_contacts_reported;
  97. bool custom_integrator;
  98. struct ShapePair {
  99. int body_shape;
  100. int local_shape;
  101. bool tagged;
  102. bool operator<(const ShapePair& p_sp) const {
  103. if (body_shape==p_sp.body_shape)
  104. return local_shape < p_sp.local_shape;
  105. else
  106. return body_shape < p_sp.body_shape;
  107. }
  108. ShapePair() {}
  109. ShapePair(int p_bs, int p_ls) { body_shape=p_bs; local_shape=p_ls; }
  110. };
  111. struct RigidBody_RemoveAction {
  112. ObjectID body_id;
  113. ShapePair pair;
  114. };
  115. struct BodyState {
  116. int rc;
  117. bool in_scene;
  118. VSet<ShapePair> shapes;
  119. };
  120. struct ContactMonitor {
  121. Map<ObjectID,BodyState> body_map;
  122. };
  123. ContactMonitor *contact_monitor;
  124. void _body_enter_scene(ObjectID p_id);
  125. void _body_exit_scene(ObjectID p_id);
  126. void _body_inout(int p_status, ObjectID p_instance, int p_body_shape,int p_local_shape);
  127. void _direct_state_changed(Object *p_state);
  128. protected:
  129. void _notification(int p_what);
  130. static void _bind_methods();
  131. public:
  132. void set_mode(Mode p_mode);
  133. Mode get_mode() const;
  134. void set_mass(real_t p_mass);
  135. real_t get_mass() const;
  136. virtual float get_inverse_mass() const { return 1.0/mass; }
  137. void set_weight(real_t p_weight);
  138. real_t get_weight() const;
  139. void set_friction(real_t p_friction);
  140. real_t get_friction() const;
  141. void set_bounce(real_t p_bounce);
  142. real_t get_bounce() const;
  143. void set_linear_velocity(const Vector3& p_velocity);
  144. Vector3 get_linear_velocity() const;
  145. void set_axis_velocity(const Vector3& p_axis);
  146. void set_angular_velocity(const Vector3&p_velocity);
  147. Vector3 get_angular_velocity() const;
  148. void set_use_custom_integrator(bool p_enable);
  149. bool is_using_custom_integrator();
  150. void set_active(bool p_active);
  151. bool is_active() const;
  152. void set_can_sleep(bool p_active);
  153. bool is_able_to_sleep() const;
  154. void set_contact_monitor(bool p_enabled);
  155. bool is_contact_monitor_enabled() const;
  156. void set_max_contacts_reported(int p_amount);
  157. int get_max_contacts_reported() const;
  158. void set_use_continuous_collision_detection(bool p_enable);
  159. bool is_using_continuous_collision_detection() const;
  160. void set_axis_lock(AxisLock p_lock);
  161. AxisLock get_axis_lock() const;
  162. void apply_impulse(const Vector3& p_pos, const Vector3& p_impulse);
  163. RigidBody();
  164. ~RigidBody();
  165. };
  166. VARIANT_ENUM_CAST(RigidBody::Mode);
  167. VARIANT_ENUM_CAST(RigidBody::AxisLock);
  168. #endif // PHYSICS_BODY__H