physics_body_2d.h 11 KB

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