physics_body_2d.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. return body_shape < p_sp.body_shape;
  123. }
  124. ShapePair() {}
  125. ShapePair(int p_bs, int p_ls) {
  126. body_shape = p_bs;
  127. local_shape = p_ls;
  128. }
  129. };
  130. struct RigidBody2D_RemoveAction {
  131. ObjectID body_id;
  132. ShapePair pair;
  133. };
  134. struct BodyState {
  135. //int rc;
  136. bool in_scene;
  137. VSet<ShapePair> shapes;
  138. };
  139. struct ContactMonitor {
  140. bool locked;
  141. Map<ObjectID, BodyState> body_map;
  142. };
  143. ContactMonitor *contact_monitor;
  144. void _body_enter_tree(ObjectID p_id);
  145. void _body_exit_tree(ObjectID p_id);
  146. void _body_inout(int p_status, ObjectID p_instance, int p_body_shape, int p_local_shape);
  147. void _direct_state_changed(Object *p_state);
  148. bool _test_motion(const Vector2 &p_motion, bool p_infinite_inertia = true, float p_margin = 0.08, const Ref<Physics2DTestMotionResult> &p_result = Ref<Physics2DTestMotionResult>());
  149. protected:
  150. void _notification(int p_what);
  151. static void _bind_methods();
  152. public:
  153. void set_mode(Mode p_mode);
  154. Mode get_mode() const;
  155. void set_mass(real_t p_mass);
  156. real_t get_mass() const;
  157. void set_inertia(real_t p_inertia);
  158. real_t get_inertia() const;
  159. void set_weight(real_t p_weight);
  160. real_t get_weight() const;
  161. #ifndef DISABLE_DEPRECATED
  162. void set_friction(real_t p_friction);
  163. real_t get_friction() const;
  164. void set_bounce(real_t p_bounce);
  165. real_t get_bounce() const;
  166. #endif
  167. void set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override);
  168. Ref<PhysicsMaterial> get_physics_material_override() const;
  169. void set_gravity_scale(real_t p_gravity_scale);
  170. real_t get_gravity_scale() const;
  171. void set_linear_damp(real_t p_linear_damp);
  172. real_t get_linear_damp() const;
  173. void set_angular_damp(real_t p_angular_damp);
  174. real_t get_angular_damp() const;
  175. void set_linear_velocity(const Vector2 &p_velocity);
  176. Vector2 get_linear_velocity() const;
  177. void set_axis_velocity(const Vector2 &p_axis);
  178. void set_angular_velocity(real_t p_velocity);
  179. real_t get_angular_velocity() const;
  180. void set_use_custom_integrator(bool p_enable);
  181. bool is_using_custom_integrator();
  182. void set_sleeping(bool p_sleeping);
  183. bool is_sleeping() const;
  184. void set_can_sleep(bool p_active);
  185. bool is_able_to_sleep() const;
  186. void set_contact_monitor(bool p_enabled);
  187. bool is_contact_monitor_enabled() const;
  188. void set_max_contacts_reported(int p_amount);
  189. int get_max_contacts_reported() const;
  190. void set_continuous_collision_detection_mode(CCDMode p_mode);
  191. CCDMode get_continuous_collision_detection_mode() const;
  192. void apply_central_impulse(const Vector2 &p_impulse);
  193. void apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse);
  194. void apply_torque_impulse(float p_torque);
  195. void set_applied_force(const Vector2 &p_force);
  196. Vector2 get_applied_force() const;
  197. void set_applied_torque(const float p_torque);
  198. float get_applied_torque() const;
  199. void add_central_force(const Vector2 &p_force);
  200. void add_force(const Vector2 &p_offset, const Vector2 &p_force);
  201. void add_torque(float p_torque);
  202. Array get_colliding_bodies() const; //function for script
  203. virtual String get_configuration_warning() const;
  204. RigidBody2D();
  205. ~RigidBody2D();
  206. private:
  207. void _reload_physics_characteristics();
  208. };
  209. VARIANT_ENUM_CAST(RigidBody2D::Mode);
  210. VARIANT_ENUM_CAST(RigidBody2D::CCDMode);
  211. class KinematicBody2D : public PhysicsBody2D {
  212. GDCLASS(KinematicBody2D, PhysicsBody2D);
  213. public:
  214. struct Collision {
  215. Vector2 collision;
  216. Vector2 normal;
  217. Vector2 collider_vel;
  218. ObjectID collider;
  219. RID collider_rid;
  220. int collider_shape;
  221. Variant collider_metadata;
  222. Vector2 remainder;
  223. Vector2 travel;
  224. int local_shape;
  225. };
  226. private:
  227. float margin;
  228. Vector2 floor_normal;
  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_up_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_up_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_normal() const;
  258. Vector2 get_floor_velocity() const;
  259. int get_slide_count() const;
  260. Collision get_slide_collision(int p_bounce) const;
  261. void set_sync_to_physics(bool p_enable);
  262. bool is_sync_to_physics_enabled() const;
  263. KinematicBody2D();
  264. ~KinematicBody2D();
  265. };
  266. class KinematicCollision2D : public Reference {
  267. GDCLASS(KinematicCollision2D, Reference);
  268. KinematicBody2D *owner;
  269. friend class KinematicBody2D;
  270. KinematicBody2D::Collision collision;
  271. protected:
  272. static void _bind_methods();
  273. public:
  274. Vector2 get_position() const;
  275. Vector2 get_normal() const;
  276. Vector2 get_travel() const;
  277. Vector2 get_remainder() const;
  278. Object *get_local_shape() const;
  279. Object *get_collider() const;
  280. ObjectID get_collider_id() const;
  281. RID get_collider_rid() const;
  282. Object *get_collider_shape() const;
  283. int get_collider_shape_index() const;
  284. Vector2 get_collider_velocity() const;
  285. Variant get_collider_metadata() const;
  286. KinematicCollision2D();
  287. };
  288. #endif // PHYSICS_BODY_2D_H