physics_body_2d.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*************************************************************************/
  2. /* physics_body_2d.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_2D_H
  31. #define PHYSICS_BODY_2D_H
  32. #include "scene/2d/collision_object_2d.h"
  33. #include "servers/physics_2d_server.h"
  34. #include "vset.h"
  35. class KinematicCollision2D;
  36. class PhysicsBody2D : public CollisionObject2D {
  37. GDCLASS(PhysicsBody2D, CollisionObject2D);
  38. uint32_t collision_layer;
  39. uint32_t collision_mask;
  40. void _set_layers(uint32_t p_mask);
  41. uint32_t _get_layers() const;
  42. protected:
  43. void _notification(int p_what);
  44. PhysicsBody2D(Physics2DServer::BodyMode p_mode);
  45. static void _bind_methods();
  46. public:
  47. void set_collision_layer(uint32_t p_layer);
  48. uint32_t get_collision_layer() const;
  49. void set_collision_mask(uint32_t p_mask);
  50. uint32_t get_collision_mask() const;
  51. void set_collision_mask_bit(int p_bit, bool p_value);
  52. bool get_collision_mask_bit(int p_bit) 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 add_collision_exception_with(Node *p_node); //must be physicsbody
  56. void remove_collision_exception_with(Node *p_node);
  57. PhysicsBody2D();
  58. };
  59. class StaticBody2D : public PhysicsBody2D {
  60. GDCLASS(StaticBody2D, PhysicsBody2D);
  61. Vector2 constant_linear_velocity;
  62. real_t constant_angular_velocity;
  63. real_t bounce;
  64. real_t friction;
  65. protected:
  66. static void _bind_methods();
  67. public:
  68. void set_friction(real_t p_friction);
  69. real_t get_friction() const;
  70. void set_bounce(real_t p_bounce);
  71. real_t get_bounce() const;
  72. void set_constant_linear_velocity(const Vector2 &p_vel);
  73. void set_constant_angular_velocity(real_t p_vel);
  74. Vector2 get_constant_linear_velocity() const;
  75. real_t get_constant_angular_velocity() const;
  76. StaticBody2D();
  77. ~StaticBody2D();
  78. };
  79. class RigidBody2D : public PhysicsBody2D {
  80. GDCLASS(RigidBody2D, PhysicsBody2D);
  81. public:
  82. enum Mode {
  83. MODE_RIGID,
  84. MODE_STATIC,
  85. MODE_CHARACTER,
  86. MODE_KINEMATIC,
  87. };
  88. enum CCDMode {
  89. CCD_MODE_DISABLED,
  90. CCD_MODE_CAST_RAY,
  91. CCD_MODE_CAST_SHAPE,
  92. };
  93. private:
  94. bool can_sleep;
  95. Physics2DDirectBodyState *state;
  96. Mode mode;
  97. real_t bounce;
  98. real_t mass;
  99. real_t friction;
  100. real_t gravity_scale;
  101. real_t linear_damp;
  102. real_t angular_damp;
  103. Vector2 linear_velocity;
  104. real_t angular_velocity;
  105. bool sleeping;
  106. int max_contacts_reported;
  107. bool custom_integrator;
  108. CCDMode ccd_mode;
  109. struct ShapePair {
  110. int body_shape;
  111. int local_shape;
  112. bool tagged;
  113. bool operator<(const ShapePair &p_sp) const {
  114. if (body_shape == p_sp.body_shape)
  115. return local_shape < p_sp.local_shape;
  116. else
  117. return body_shape < p_sp.body_shape;
  118. }
  119. ShapePair() {}
  120. ShapePair(int p_bs, int p_ls) {
  121. body_shape = p_bs;
  122. local_shape = p_ls;
  123. }
  124. };
  125. struct RigidBody2D_RemoveAction {
  126. ObjectID body_id;
  127. ShapePair pair;
  128. };
  129. struct BodyState {
  130. //int rc;
  131. bool in_scene;
  132. VSet<ShapePair> shapes;
  133. };
  134. struct ContactMonitor {
  135. bool locked;
  136. Map<ObjectID, BodyState> body_map;
  137. };
  138. ContactMonitor *contact_monitor;
  139. void _body_enter_tree(ObjectID p_id);
  140. void _body_exit_tree(ObjectID p_id);
  141. void _body_inout(int p_status, ObjectID p_instance, int p_body_shape, int p_local_shape);
  142. void _direct_state_changed(Object *p_state);
  143. bool _test_motion(const Vector2 &p_motion, float p_margin = 0.08, const Ref<Physics2DTestMotionResult> &p_result = Ref<Physics2DTestMotionResult>());
  144. protected:
  145. void _notification(int p_what);
  146. static void _bind_methods();
  147. public:
  148. void set_mode(Mode p_mode);
  149. Mode get_mode() const;
  150. void set_mass(real_t p_mass);
  151. real_t get_mass() const;
  152. void set_inertia(real_t p_inertia);
  153. real_t get_inertia() const;
  154. void set_weight(real_t p_weight);
  155. real_t get_weight() const;
  156. void set_friction(real_t p_friction);
  157. real_t get_friction() const;
  158. void set_bounce(real_t p_bounce);
  159. real_t get_bounce() const;
  160. void set_gravity_scale(real_t p_gravity_scale);
  161. real_t get_gravity_scale() const;
  162. void set_linear_damp(real_t p_linear_damp);
  163. real_t get_linear_damp() const;
  164. void set_angular_damp(real_t p_angular_damp);
  165. real_t get_angular_damp() const;
  166. void set_linear_velocity(const Vector2 &p_velocity);
  167. Vector2 get_linear_velocity() const;
  168. void set_axis_velocity(const Vector2 &p_axis);
  169. void set_angular_velocity(real_t p_velocity);
  170. real_t get_angular_velocity() const;
  171. void set_use_custom_integrator(bool p_enable);
  172. bool is_using_custom_integrator();
  173. void set_sleeping(bool p_sleeping);
  174. bool is_sleeping() const;
  175. void set_can_sleep(bool p_active);
  176. bool is_able_to_sleep() const;
  177. void set_contact_monitor(bool p_enabled);
  178. bool is_contact_monitor_enabled() const;
  179. void set_max_contacts_reported(int p_amount);
  180. int get_max_contacts_reported() const;
  181. void set_continuous_collision_detection_mode(CCDMode p_mode);
  182. CCDMode get_continuous_collision_detection_mode() const;
  183. void apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse);
  184. void set_applied_force(const Vector2 &p_force);
  185. Vector2 get_applied_force() const;
  186. void set_applied_torque(const float p_torque);
  187. float get_applied_torque() const;
  188. void add_force(const Vector2 &p_offset, const Vector2 &p_force);
  189. Array get_colliding_bodies() const; //function for script
  190. virtual String get_configuration_warning() const;
  191. RigidBody2D();
  192. ~RigidBody2D();
  193. };
  194. VARIANT_ENUM_CAST(RigidBody2D::Mode);
  195. VARIANT_ENUM_CAST(RigidBody2D::CCDMode);
  196. class KinematicBody2D : public PhysicsBody2D {
  197. GDCLASS(KinematicBody2D, PhysicsBody2D);
  198. public:
  199. struct Collision {
  200. Vector2 collision;
  201. Vector2 normal;
  202. Vector2 collider_vel;
  203. ObjectID collider;
  204. int collider_shape;
  205. Variant collider_metadata;
  206. Vector2 remainder;
  207. Vector2 travel;
  208. int local_shape;
  209. };
  210. private:
  211. float margin;
  212. Vector2 floor_velocity;
  213. bool on_floor;
  214. bool on_ceiling;
  215. bool on_wall;
  216. Vector<Collision> colliders;
  217. Vector<Ref<KinematicCollision2D> > slide_colliders;
  218. Ref<KinematicCollision2D> motion_cache;
  219. _FORCE_INLINE_ bool _ignores_mode(Physics2DServer::BodyMode) const;
  220. Ref<KinematicCollision2D> _move(const Vector2 &p_motion);
  221. Ref<KinematicCollision2D> _get_slide_collision(int p_bounce);
  222. protected:
  223. static void _bind_methods();
  224. public:
  225. bool move_and_collide(const Vector2 &p_motion, Collision &r_collision);
  226. bool test_move(const Transform2D &p_from, const Vector2 &p_motion);
  227. void set_safe_margin(float p_margin);
  228. float get_safe_margin() const;
  229. Vector2 move_and_slide(const Vector2 &p_linear_velocity, const Vector2 &p_floor_direction = Vector2(0, 0), float p_slope_stop_min_velocity = 5, 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. Vector2 get_floor_velocity() const;
  234. int get_slide_count() const;
  235. Collision get_slide_collision(int p_bounce) const;
  236. KinematicBody2D();
  237. ~KinematicBody2D();
  238. };
  239. class KinematicCollision2D : public Reference {
  240. GDCLASS(KinematicCollision2D, Reference);
  241. KinematicBody2D *owner;
  242. friend class KinematicBody2D;
  243. KinematicBody2D::Collision collision;
  244. protected:
  245. static void _bind_methods();
  246. public:
  247. Vector2 get_position() const;
  248. Vector2 get_normal() const;
  249. Vector2 get_travel() const;
  250. Vector2 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. Vector2 get_collider_velocity() const;
  257. Variant get_collider_metadata() const;
  258. KinematicCollision2D();
  259. };
  260. #endif // PHYSICS_BODY_2D_H