physics_body_2d.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**************************************************************************/
  2. /* physics_body_2d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. void _set_layers(uint32_t p_mask);
  40. uint32_t _get_layers() const;
  41. protected:
  42. void _notification(int p_what);
  43. PhysicsBody2D(Physics2DServer::BodyMode p_mode);
  44. static void _bind_methods();
  45. public:
  46. Array get_collision_exceptions();
  47. void add_collision_exception_with(Node *p_node); //must be physicsbody
  48. void remove_collision_exception_with(Node *p_node);
  49. };
  50. class StaticBody2D : public PhysicsBody2D {
  51. GDCLASS(StaticBody2D, PhysicsBody2D);
  52. Vector2 constant_linear_velocity;
  53. real_t constant_angular_velocity;
  54. Ref<PhysicsMaterial> physics_material_override;
  55. protected:
  56. static void _bind_methods();
  57. public:
  58. #ifndef DISABLE_DEPRECATED
  59. void set_friction(real_t p_friction);
  60. real_t get_friction() const;
  61. void set_bounce(real_t p_bounce);
  62. real_t get_bounce() const;
  63. #endif
  64. void set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override);
  65. Ref<PhysicsMaterial> get_physics_material_override() const;
  66. void set_constant_linear_velocity(const Vector2 &p_vel);
  67. void set_constant_angular_velocity(real_t p_vel);
  68. Vector2 get_constant_linear_velocity() const;
  69. real_t get_constant_angular_velocity() const;
  70. StaticBody2D();
  71. ~StaticBody2D();
  72. private:
  73. void _reload_physics_characteristics();
  74. };
  75. class RigidBody2D : public PhysicsBody2D {
  76. GDCLASS(RigidBody2D, PhysicsBody2D);
  77. public:
  78. enum Mode {
  79. MODE_RIGID,
  80. MODE_STATIC,
  81. MODE_CHARACTER,
  82. MODE_KINEMATIC,
  83. };
  84. enum CCDMode {
  85. CCD_MODE_DISABLED,
  86. CCD_MODE_CAST_RAY,
  87. CCD_MODE_CAST_SHAPE,
  88. };
  89. private:
  90. bool can_sleep;
  91. Physics2DDirectBodyState *state;
  92. Mode mode;
  93. real_t mass;
  94. Ref<PhysicsMaterial> physics_material_override;
  95. real_t gravity_scale;
  96. real_t linear_damp;
  97. real_t angular_damp;
  98. Vector2 linear_velocity;
  99. real_t angular_velocity;
  100. bool sleeping;
  101. int max_contacts_reported;
  102. bool custom_integrator;
  103. CCDMode ccd_mode;
  104. struct ShapePair {
  105. int body_shape;
  106. int local_shape;
  107. bool tagged;
  108. bool operator<(const ShapePair &p_sp) const {
  109. if (body_shape == p_sp.body_shape) {
  110. return local_shape < p_sp.local_shape;
  111. }
  112. return body_shape < p_sp.body_shape;
  113. }
  114. ShapePair() {}
  115. ShapePair(int p_bs, int p_ls) {
  116. body_shape = p_bs;
  117. local_shape = p_ls;
  118. }
  119. };
  120. struct RigidBody2D_RemoveAction {
  121. RID rid;
  122. ObjectID body_id;
  123. ShapePair pair;
  124. };
  125. struct BodyState {
  126. RID rid;
  127. bool in_scene;
  128. VSet<ShapePair> shapes;
  129. };
  130. struct ContactMonitor {
  131. bool locked;
  132. Map<ObjectID, BodyState> body_map;
  133. };
  134. ContactMonitor *contact_monitor;
  135. void _body_enter_tree(ObjectID p_id);
  136. void _body_exit_tree(ObjectID p_id);
  137. void _body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape);
  138. void _direct_state_changed(Object *p_state);
  139. bool _test_motion(const Vector2 &p_motion, bool p_infinite_inertia = true, float p_margin = 0.08, const Ref<Physics2DTestMotionResult> &p_result = Ref<Physics2DTestMotionResult>());
  140. protected:
  141. void _notification(int p_what);
  142. static void _bind_methods();
  143. public:
  144. void set_mode(Mode p_mode);
  145. Mode get_mode() const;
  146. void set_mass(real_t p_mass);
  147. real_t get_mass() const;
  148. void set_inertia(real_t p_inertia);
  149. real_t get_inertia() const;
  150. void set_weight(real_t p_weight);
  151. real_t get_weight() const;
  152. #ifndef DISABLE_DEPRECATED
  153. void set_friction(real_t p_friction);
  154. real_t get_friction() const;
  155. void set_bounce(real_t p_bounce);
  156. real_t get_bounce() const;
  157. #endif
  158. void set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override);
  159. Ref<PhysicsMaterial> get_physics_material_override() 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_central_impulse(const Vector2 &p_impulse);
  184. void apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse);
  185. void apply_torque_impulse(float p_torque);
  186. void set_applied_force(const Vector2 &p_force);
  187. Vector2 get_applied_force() const;
  188. void set_applied_torque(const float p_torque);
  189. float get_applied_torque() const;
  190. void add_central_force(const Vector2 &p_force);
  191. void add_force(const Vector2 &p_offset, const Vector2 &p_force);
  192. void add_torque(float p_torque);
  193. Array get_colliding_bodies() const; //function for script
  194. virtual String get_configuration_warning() const;
  195. RigidBody2D();
  196. ~RigidBody2D();
  197. private:
  198. void _reload_physics_characteristics();
  199. };
  200. VARIANT_ENUM_CAST(RigidBody2D::Mode);
  201. VARIANT_ENUM_CAST(RigidBody2D::CCDMode);
  202. class KinematicBody2D : public PhysicsBody2D {
  203. GDCLASS(KinematicBody2D, PhysicsBody2D);
  204. public:
  205. enum MovingPlatformApplyVelocityOnLeave {
  206. PLATFORM_VEL_ON_LEAVE_ALWAYS,
  207. PLATFORM_VEL_ON_LEAVE_UPWARD_ONLY,
  208. PLATFORM_VEL_ON_LEAVE_NEVER,
  209. };
  210. struct Collision {
  211. Vector2 collision;
  212. Vector2 normal;
  213. Vector2 collider_vel;
  214. ObjectID collider;
  215. RID collider_rid;
  216. int collider_shape;
  217. Variant collider_metadata;
  218. Vector2 remainder;
  219. Vector2 travel;
  220. int local_shape;
  221. real_t collision_safe_fraction;
  222. real_t get_angle(const Vector2 &p_up_direction) const {
  223. return Math::acos(normal.dot(p_up_direction));
  224. }
  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. MovingPlatformApplyVelocityOnLeave moving_platform_apply_velocity_on_leave = PLATFORM_VEL_ON_LEAVE_ALWAYS;
  236. Vector<Collision> colliders;
  237. Vector<Ref<KinematicCollision2D>> slide_colliders;
  238. Ref<KinematicCollision2D> motion_cache;
  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. Ref<KinematicCollision2D> _get_last_slide_collision();
  242. Transform2D last_valid_transform;
  243. void _direct_state_changed(Object *p_state);
  244. Vector2 _move_and_slide_internal(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);
  245. void _set_collision_direction(const Collision &p_collision, const Vector2 &p_up_direction, float p_floor_max_angle);
  246. void set_moving_platform_apply_velocity_on_leave(MovingPlatformApplyVelocityOnLeave p_on_leave_velocity);
  247. MovingPlatformApplyVelocityOnLeave get_moving_platform_apply_velocity_on_leave() const;
  248. protected:
  249. void _notification(int p_what);
  250. static void _bind_methods();
  251. public:
  252. 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, bool p_cancel_sliding = true, const Set<RID> &p_exclude = Set<RID>());
  253. bool test_move(const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia = true);
  254. bool separate_raycast_shapes(bool p_infinite_inertia, Collision &r_collision);
  255. void set_safe_margin(float p_margin);
  256. float get_safe_margin() const;
  257. 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);
  258. 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);
  259. bool is_on_floor() const;
  260. bool is_on_wall() const;
  261. bool is_on_ceiling() const;
  262. Vector2 get_floor_normal() const;
  263. real_t get_floor_angle(const Vector2 &p_up_direction = Vector2(0.0, -1.0)) const;
  264. Vector2 get_floor_velocity() const;
  265. int get_slide_count() const;
  266. Collision get_slide_collision(int p_bounce) const;
  267. void set_sync_to_physics(bool p_enable);
  268. bool is_sync_to_physics_enabled() const;
  269. KinematicBody2D();
  270. ~KinematicBody2D();
  271. };
  272. VARIANT_ENUM_CAST(KinematicBody2D::MovingPlatformApplyVelocityOnLeave);
  273. class KinematicCollision2D : public Reference {
  274. GDCLASS(KinematicCollision2D, Reference);
  275. KinematicBody2D *owner;
  276. friend class KinematicBody2D;
  277. KinematicBody2D::Collision collision;
  278. protected:
  279. static void _bind_methods();
  280. public:
  281. Vector2 get_position() const;
  282. Vector2 get_normal() const;
  283. Vector2 get_travel() const;
  284. Vector2 get_remainder() const;
  285. real_t get_angle(const Vector2 &p_up_direction = Vector2(0.0, -1.0)) const;
  286. Object *get_local_shape() const;
  287. Object *get_collider() const;
  288. ObjectID get_collider_id() const;
  289. RID get_collider_rid() const;
  290. Object *get_collider_shape() const;
  291. int get_collider_shape_index() const;
  292. Vector2 get_collider_velocity() const;
  293. Variant get_collider_metadata() const;
  294. KinematicCollision2D();
  295. };
  296. #endif // PHYSICS_BODY_2D_H