physics_body.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*************************************************************************/
  2. /* physics_body.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef PHYSICS_BODY__H
  31. #define PHYSICS_BODY__H
  32. #include "scene/3d/collision_object.h"
  33. #include "servers/physics_server.h"
  34. #include "vset.h"
  35. class PhysicsBody : public CollisionObject {
  36. GDCLASS(PhysicsBody, CollisionObject);
  37. uint32_t collision_layer;
  38. uint32_t collision_mask;
  39. void _set_layers(uint32_t p_mask);
  40. uint32_t _get_layers() const;
  41. protected:
  42. static void _bind_methods();
  43. void _notification(int p_what);
  44. PhysicsBody(PhysicsServer::BodyMode p_mode);
  45. public:
  46. virtual Vector3 get_linear_velocity() const;
  47. virtual Vector3 get_angular_velocity() const;
  48. virtual float get_inverse_mass() const;
  49. void set_collision_layer(uint32_t p_layer);
  50. uint32_t get_collision_layer() const;
  51. void set_collision_mask(uint32_t p_mask);
  52. uint32_t get_collision_mask() const;
  53. void set_collision_layer_bit(int p_bit, bool p_value);
  54. bool get_collision_layer_bit(int p_bit) const;
  55. void set_collision_mask_bit(int p_bit, bool p_value);
  56. bool get_collision_mask_bit(int p_bit) const;
  57. void add_collision_exception_with(Node *p_node); //must be physicsbody
  58. void remove_collision_exception_with(Node *p_node);
  59. PhysicsBody();
  60. };
  61. class StaticBody : public PhysicsBody {
  62. GDCLASS(StaticBody, PhysicsBody);
  63. Vector3 constant_linear_velocity;
  64. Vector3 constant_angular_velocity;
  65. real_t bounce;
  66. real_t friction;
  67. protected:
  68. static void _bind_methods();
  69. public:
  70. void set_friction(real_t p_friction);
  71. real_t get_friction() const;
  72. void set_bounce(real_t p_bounce);
  73. real_t get_bounce() const;
  74. void set_constant_linear_velocity(const Vector3 &p_vel);
  75. void set_constant_angular_velocity(const Vector3 &p_vel);
  76. Vector3 get_constant_linear_velocity() const;
  77. Vector3 get_constant_angular_velocity() const;
  78. StaticBody();
  79. ~StaticBody();
  80. };
  81. class RigidBody : public PhysicsBody {
  82. GDCLASS(RigidBody, PhysicsBody);
  83. public:
  84. enum Mode {
  85. MODE_RIGID,
  86. MODE_STATIC,
  87. MODE_CHARACTER,
  88. MODE_KINEMATIC,
  89. };
  90. enum AxisLock {
  91. AXIS_LOCK_DISABLED,
  92. AXIS_LOCK_X,
  93. AXIS_LOCK_Y,
  94. AXIS_LOCK_Z,
  95. };
  96. private:
  97. bool can_sleep;
  98. PhysicsDirectBodyState *state;
  99. Mode mode;
  100. real_t bounce;
  101. real_t mass;
  102. real_t friction;
  103. Vector3 linear_velocity;
  104. Vector3 angular_velocity;
  105. real_t gravity_scale;
  106. real_t linear_damp;
  107. real_t angular_damp;
  108. bool sleeping;
  109. bool ccd;
  110. AxisLock axis_lock;
  111. int max_contacts_reported;
  112. bool custom_integrator;
  113. struct ShapePair {
  114. int body_shape;
  115. int local_shape;
  116. bool tagged;
  117. bool operator<(const ShapePair &p_sp) const {
  118. if (body_shape == p_sp.body_shape)
  119. return local_shape < p_sp.local_shape;
  120. else
  121. return body_shape < p_sp.body_shape;
  122. }
  123. ShapePair() {}
  124. ShapePair(int p_bs, int p_ls) {
  125. body_shape = p_bs;
  126. local_shape = p_ls;
  127. }
  128. };
  129. struct RigidBody_RemoveAction {
  130. ObjectID body_id;
  131. ShapePair pair;
  132. };
  133. struct BodyState {
  134. //int rc;
  135. bool in_tree;
  136. VSet<ShapePair> shapes;
  137. };
  138. struct ContactMonitor {
  139. bool locked;
  140. Map<ObjectID, BodyState> body_map;
  141. };
  142. ContactMonitor *contact_monitor;
  143. void _body_enter_tree(ObjectID p_id);
  144. void _body_exit_tree(ObjectID p_id);
  145. void _body_inout(int p_status, ObjectID p_instance, int p_body_shape, int p_local_shape);
  146. void _direct_state_changed(Object *p_state);
  147. protected:
  148. void _notification(int p_what);
  149. static void _bind_methods();
  150. public:
  151. void set_mode(Mode p_mode);
  152. Mode get_mode() const;
  153. void set_mass(real_t p_mass);
  154. real_t get_mass() const;
  155. virtual float get_inverse_mass() const { return 1.0 / mass; }
  156. void set_weight(real_t p_weight);
  157. real_t get_weight() const;
  158. void set_friction(real_t p_friction);
  159. real_t get_friction() const;
  160. void set_bounce(real_t p_bounce);
  161. real_t get_bounce() const;
  162. void set_linear_velocity(const Vector3 &p_velocity);
  163. Vector3 get_linear_velocity() const;
  164. void set_axis_velocity(const Vector3 &p_axis);
  165. void set_angular_velocity(const Vector3 &p_velocity);
  166. Vector3 get_angular_velocity() const;
  167. void set_gravity_scale(real_t p_gravity_scale);
  168. real_t get_gravity_scale() const;
  169. void set_linear_damp(real_t p_linear_damp);
  170. real_t get_linear_damp() const;
  171. void set_angular_damp(real_t p_angular_damp);
  172. real_t get_angular_damp() const;
  173. void set_use_custom_integrator(bool p_enable);
  174. bool is_using_custom_integrator();
  175. void set_sleeping(bool p_sleeping);
  176. bool is_sleeping() const;
  177. void set_can_sleep(bool p_active);
  178. bool is_able_to_sleep() const;
  179. void set_contact_monitor(bool p_enabled);
  180. bool is_contact_monitor_enabled() const;
  181. void set_max_contacts_reported(int p_amount);
  182. int get_max_contacts_reported() const;
  183. void set_use_continuous_collision_detection(bool p_enable);
  184. bool is_using_continuous_collision_detection() const;
  185. void set_axis_lock(AxisLock p_lock);
  186. AxisLock get_axis_lock() const;
  187. Array get_colliding_bodies() const;
  188. void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse);
  189. virtual String get_configuration_warning() const;
  190. RigidBody();
  191. ~RigidBody();
  192. };
  193. VARIANT_ENUM_CAST(RigidBody::Mode);
  194. VARIANT_ENUM_CAST(RigidBody::AxisLock);
  195. class KinematicCollision;
  196. class KinematicBody : public PhysicsBody {
  197. GDCLASS(KinematicBody, PhysicsBody);
  198. public:
  199. struct Collision {
  200. Vector3 collision;
  201. Vector3 normal;
  202. Vector3 collider_vel;
  203. ObjectID collider;
  204. int collider_shape;
  205. Variant collider_metadata;
  206. Vector3 remainder;
  207. Vector3 travel;
  208. int local_shape;
  209. };
  210. private:
  211. float margin;
  212. Vector3 floor_velocity;
  213. bool on_floor;
  214. bool on_ceiling;
  215. bool on_wall;
  216. Vector<Collision> colliders;
  217. Vector<Ref<KinematicCollision> > slide_colliders;
  218. Ref<KinematicCollision> motion_cache;
  219. _FORCE_INLINE_ bool _ignores_mode(PhysicsServer::BodyMode) const;
  220. Ref<KinematicCollision> _move(const Vector3 &p_motion);
  221. Ref<KinematicCollision> _get_slide_collision(int p_bounce);
  222. protected:
  223. static void _bind_methods();
  224. public:
  225. bool move_and_collide(const Vector3 &p_motion, Collision &r_collision);
  226. bool test_move(const Transform &p_from, const Vector3 &p_motion);
  227. void set_safe_margin(float p_margin);
  228. float get_safe_margin() const;
  229. Vector3 move_and_slide(const Vector3 &p_linear_velocity, const Vector3 &p_floor_direction = Vector3(0, 0, 0), float p_slope_stop_min_velocity = 0.05, int p_max_slides = 4, float p_floor_max_angle = Math::deg2rad((float)45));
  230. bool is_on_floor() const;
  231. bool is_on_wall() const;
  232. bool is_on_ceiling() const;
  233. Vector3 get_floor_velocity() const;
  234. int get_slide_count() const;
  235. Collision get_slide_collision(int p_bounce) const;
  236. KinematicBody();
  237. ~KinematicBody();
  238. };
  239. class KinematicCollision : public Reference {
  240. GDCLASS(KinematicCollision, Reference);
  241. KinematicBody *owner;
  242. friend class KinematicBody;
  243. KinematicBody::Collision collision;
  244. protected:
  245. static void _bind_methods();
  246. public:
  247. Vector3 get_position() const;
  248. Vector3 get_normal() const;
  249. Vector3 get_travel() const;
  250. Vector3 get_remainder() const;
  251. Object *get_local_shape() const;
  252. Object *get_collider() const;
  253. ObjectID get_collider_id() const;
  254. Object *get_collider_shape() const;
  255. int get_collider_shape_index() const;
  256. Vector3 get_collider_velocity() const;
  257. Variant get_collider_metadata() const;
  258. KinematicCollision();
  259. };
  260. #endif // PHYSICS_BODY__H