physics_body.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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-2016 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. uint32_t layer_mask;
  37. protected:
  38. static void _bind_methods();
  39. void _notification(int p_what);
  40. PhysicsBody(PhysicsServer::BodyMode p_mode);
  41. public:
  42. virtual Vector3 get_linear_velocity() const;
  43. virtual Vector3 get_angular_velocity() const;
  44. virtual float get_inverse_mass() const;
  45. void set_layer_mask(uint32_t p_mask);
  46. uint32_t get_layer_mask() const;
  47. void add_collision_exception_with(Node* p_node); //must be physicsbody
  48. void remove_collision_exception_with(Node* p_node);
  49. PhysicsBody();
  50. };
  51. class StaticBody : public PhysicsBody {
  52. OBJ_TYPE(StaticBody,PhysicsBody);
  53. Vector3 constant_linear_velocity;
  54. Vector3 constant_angular_velocity;
  55. real_t bounce;
  56. real_t friction;
  57. protected:
  58. static void _bind_methods();
  59. public:
  60. void set_friction(real_t p_friction);
  61. real_t get_friction() const;
  62. void set_bounce(real_t p_bounce);
  63. real_t get_bounce() const;
  64. void set_constant_linear_velocity(const Vector3& p_vel);
  65. void set_constant_angular_velocity(const Vector3& p_vel);
  66. Vector3 get_constant_linear_velocity() const;
  67. Vector3 get_constant_angular_velocity() const;
  68. StaticBody();
  69. ~StaticBody();
  70. };
  71. class RigidBody : public PhysicsBody {
  72. OBJ_TYPE(RigidBody,PhysicsBody);
  73. public:
  74. enum Mode {
  75. MODE_RIGID,
  76. MODE_STATIC,
  77. MODE_CHARACTER,
  78. MODE_KINEMATIC,
  79. };
  80. enum AxisLock {
  81. AXIS_LOCK_DISABLED,
  82. AXIS_LOCK_X,
  83. AXIS_LOCK_Y,
  84. AXIS_LOCK_Z,
  85. };
  86. private:
  87. bool can_sleep;
  88. PhysicsDirectBodyState *state;
  89. Mode mode;
  90. real_t bounce;
  91. real_t mass;
  92. real_t friction;
  93. Vector3 linear_velocity;
  94. Vector3 angular_velocity;
  95. real_t gravity_scale;
  96. real_t linear_damp;
  97. real_t angular_damp;
  98. bool sleeping;
  99. bool ccd;
  100. AxisLock axis_lock;
  101. int max_contacts_reported;
  102. bool custom_integrator;
  103. struct ShapePair {
  104. int body_shape;
  105. int local_shape;
  106. bool tagged;
  107. bool operator<(const ShapePair& p_sp) const {
  108. if (body_shape==p_sp.body_shape)
  109. return local_shape < p_sp.local_shape;
  110. else
  111. return body_shape < p_sp.body_shape;
  112. }
  113. ShapePair() {}
  114. ShapePair(int p_bs, int p_ls) { body_shape=p_bs; local_shape=p_ls; }
  115. };
  116. struct RigidBody_RemoveAction {
  117. ObjectID body_id;
  118. ShapePair pair;
  119. };
  120. struct BodyState {
  121. //int rc;
  122. bool in_tree;
  123. VSet<ShapePair> shapes;
  124. };
  125. struct ContactMonitor {
  126. bool locked;
  127. Map<ObjectID,BodyState> body_map;
  128. };
  129. ContactMonitor *contact_monitor;
  130. void _body_enter_tree(ObjectID p_id);
  131. void _body_exit_tree(ObjectID p_id);
  132. void _body_inout(int p_status, ObjectID p_instance, int p_body_shape,int p_local_shape);
  133. void _direct_state_changed(Object *p_state);
  134. protected:
  135. void _notification(int p_what);
  136. static void _bind_methods();
  137. public:
  138. void set_mode(Mode p_mode);
  139. Mode get_mode() const;
  140. void set_mass(real_t p_mass);
  141. real_t get_mass() const;
  142. virtual float get_inverse_mass() const { return 1.0/mass; }
  143. void set_weight(real_t p_weight);
  144. real_t get_weight() const;
  145. void set_friction(real_t p_friction);
  146. real_t get_friction() const;
  147. void set_bounce(real_t p_bounce);
  148. real_t get_bounce() const;
  149. void set_linear_velocity(const Vector3& p_velocity);
  150. Vector3 get_linear_velocity() const;
  151. void set_axis_velocity(const Vector3& p_axis);
  152. void set_angular_velocity(const Vector3&p_velocity);
  153. Vector3 get_angular_velocity() const;
  154. void set_gravity_scale(real_t p_gravity_scale);
  155. real_t get_gravity_scale() const;
  156. void set_linear_damp(real_t p_linear_damp);
  157. real_t get_linear_damp() const;
  158. void set_angular_damp(real_t p_angular_damp);
  159. real_t get_angular_damp() const;
  160. void set_use_custom_integrator(bool p_enable);
  161. bool is_using_custom_integrator();
  162. void set_sleeping(bool p_sleeping);
  163. bool is_sleeping() const;
  164. void set_can_sleep(bool p_active);
  165. bool is_able_to_sleep() const;
  166. void set_contact_monitor(bool p_enabled);
  167. bool is_contact_monitor_enabled() const;
  168. void set_max_contacts_reported(int p_amount);
  169. int get_max_contacts_reported() const;
  170. void set_use_continuous_collision_detection(bool p_enable);
  171. bool is_using_continuous_collision_detection() const;
  172. void set_axis_lock(AxisLock p_lock);
  173. AxisLock get_axis_lock() const;
  174. Array get_colliding_bodies() const;
  175. void apply_impulse(const Vector3& p_pos, const Vector3& p_impulse);
  176. RigidBody();
  177. ~RigidBody();
  178. };
  179. VARIANT_ENUM_CAST(RigidBody::Mode);
  180. VARIANT_ENUM_CAST(RigidBody::AxisLock);
  181. class KinematicBody : public PhysicsBody {
  182. OBJ_TYPE(KinematicBody,PhysicsBody);
  183. float margin;
  184. bool collide_static;
  185. bool collide_rigid;
  186. bool collide_kinematic;
  187. bool collide_character;
  188. bool colliding;
  189. Vector3 collision;
  190. Vector3 normal;
  191. Vector3 collider_vel;
  192. ObjectID collider;
  193. int collider_shape;
  194. Variant _get_collider() const;
  195. _FORCE_INLINE_ bool _ignores_mode(PhysicsServer::BodyMode) const;
  196. protected:
  197. static void _bind_methods();
  198. public:
  199. enum {
  200. SLIDE_FLAG_FLOOR,
  201. SLIDE_FLAG_WALL,
  202. SLIDE_FLAG_ROOF
  203. };
  204. Vector3 move(const Vector3& p_motion);
  205. Vector3 move_to(const Vector3& p_position);
  206. bool can_teleport_to(const Vector3& p_position);
  207. bool is_colliding() const;
  208. Vector3 get_collision_pos() const;
  209. Vector3 get_collision_normal() const;
  210. Vector3 get_collider_velocity() const;
  211. ObjectID get_collider() const;
  212. int get_collider_shape() const;
  213. void set_collide_with_static_bodies(bool p_enable);
  214. bool can_collide_with_static_bodies() const;
  215. void set_collide_with_rigid_bodies(bool p_enable);
  216. bool can_collide_with_rigid_bodies() const;
  217. void set_collide_with_kinematic_bodies(bool p_enable);
  218. bool can_collide_with_kinematic_bodies() const;
  219. void set_collide_with_character_bodies(bool p_enable);
  220. bool can_collide_with_character_bodies() const;
  221. void set_collision_margin(float p_margin);
  222. float get_collision_margin() const;
  223. KinematicBody();
  224. ~KinematicBody();
  225. };
  226. #endif // PHYSICS_BODY__H