rigid_body_2d.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************/
  2. /* rigid_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 RIGID_BODY_2D_H
  31. #define RIGID_BODY_2D_H
  32. #include "core/templates/vset.h"
  33. #include "scene/2d/physics/physics_body_2d.h"
  34. class RigidBody2D : public PhysicsBody2D {
  35. GDCLASS(RigidBody2D, PhysicsBody2D);
  36. public:
  37. enum FreezeMode {
  38. FREEZE_MODE_STATIC,
  39. FREEZE_MODE_KINEMATIC,
  40. };
  41. enum CenterOfMassMode {
  42. CENTER_OF_MASS_MODE_AUTO,
  43. CENTER_OF_MASS_MODE_CUSTOM,
  44. };
  45. enum DampMode {
  46. DAMP_MODE_COMBINE,
  47. DAMP_MODE_REPLACE,
  48. };
  49. enum CCDMode {
  50. CCD_MODE_DISABLED,
  51. CCD_MODE_CAST_RAY,
  52. CCD_MODE_CAST_SHAPE,
  53. };
  54. private:
  55. bool can_sleep = true;
  56. bool lock_rotation = false;
  57. bool freeze = false;
  58. FreezeMode freeze_mode = FREEZE_MODE_STATIC;
  59. real_t mass = 1.0;
  60. real_t inertia = 0.0;
  61. CenterOfMassMode center_of_mass_mode = CENTER_OF_MASS_MODE_AUTO;
  62. Vector2 center_of_mass;
  63. Ref<PhysicsMaterial> physics_material_override;
  64. real_t gravity_scale = 1.0;
  65. DampMode linear_damp_mode = DAMP_MODE_COMBINE;
  66. DampMode angular_damp_mode = DAMP_MODE_COMBINE;
  67. real_t linear_damp = 0.0;
  68. real_t angular_damp = 0.0;
  69. Vector2 linear_velocity;
  70. real_t angular_velocity = 0.0;
  71. bool sleeping = false;
  72. int max_contacts_reported = 0;
  73. int contact_count = 0;
  74. bool custom_integrator = false;
  75. CCDMode ccd_mode = CCD_MODE_DISABLED;
  76. struct ShapePair {
  77. int body_shape = 0;
  78. int local_shape = 0;
  79. bool tagged = false;
  80. bool operator<(const ShapePair &p_sp) const {
  81. if (body_shape == p_sp.body_shape) {
  82. return local_shape < p_sp.local_shape;
  83. }
  84. return body_shape < p_sp.body_shape;
  85. }
  86. ShapePair() {}
  87. ShapePair(int p_bs, int p_ls) {
  88. body_shape = p_bs;
  89. local_shape = p_ls;
  90. }
  91. };
  92. struct RigidBody2D_RemoveAction {
  93. RID rid;
  94. ObjectID body_id;
  95. ShapePair pair;
  96. };
  97. struct BodyState {
  98. RID rid;
  99. //int rc;
  100. bool in_scene = false;
  101. VSet<ShapePair> shapes;
  102. };
  103. struct ContactMonitor {
  104. bool locked = false;
  105. HashMap<ObjectID, BodyState> body_map;
  106. };
  107. ContactMonitor *contact_monitor = nullptr;
  108. void _body_enter_tree(ObjectID p_id);
  109. void _body_exit_tree(ObjectID p_id);
  110. void _body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape);
  111. static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState2D *p_state);
  112. void _body_state_changed(PhysicsDirectBodyState2D *p_state);
  113. void _sync_body_state(PhysicsDirectBodyState2D *p_state);
  114. protected:
  115. void _notification(int p_what);
  116. static void _bind_methods();
  117. void _validate_property(PropertyInfo &p_property) const;
  118. GDVIRTUAL1(_integrate_forces, PhysicsDirectBodyState2D *)
  119. void _apply_body_mode();
  120. public:
  121. void set_lock_rotation_enabled(bool p_lock_rotation);
  122. bool is_lock_rotation_enabled() const;
  123. void set_freeze_enabled(bool p_freeze);
  124. bool is_freeze_enabled() const;
  125. void set_freeze_mode(FreezeMode p_freeze_mode);
  126. FreezeMode get_freeze_mode() const;
  127. void set_mass(real_t p_mass);
  128. real_t get_mass() const;
  129. void set_inertia(real_t p_inertia);
  130. real_t get_inertia() const;
  131. void set_center_of_mass_mode(CenterOfMassMode p_mode);
  132. CenterOfMassMode get_center_of_mass_mode() const;
  133. void set_center_of_mass(const Vector2 &p_center_of_mass);
  134. const Vector2 &get_center_of_mass() const;
  135. void set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override);
  136. Ref<PhysicsMaterial> get_physics_material_override() const;
  137. void set_gravity_scale(real_t p_gravity_scale);
  138. real_t get_gravity_scale() const;
  139. void set_linear_damp_mode(DampMode p_mode);
  140. DampMode get_linear_damp_mode() const;
  141. void set_angular_damp_mode(DampMode p_mode);
  142. DampMode get_angular_damp_mode() const;
  143. void set_linear_damp(real_t p_linear_damp);
  144. real_t get_linear_damp() const;
  145. void set_angular_damp(real_t p_angular_damp);
  146. real_t get_angular_damp() const;
  147. void set_linear_velocity(const Vector2 &p_velocity);
  148. Vector2 get_linear_velocity() const;
  149. void set_axis_velocity(const Vector2 &p_axis);
  150. void set_angular_velocity(real_t p_velocity);
  151. real_t get_angular_velocity() const;
  152. void set_use_custom_integrator(bool p_enable);
  153. bool is_using_custom_integrator();
  154. void set_sleeping(bool p_sleeping);
  155. bool is_sleeping() const;
  156. void set_can_sleep(bool p_active);
  157. bool is_able_to_sleep() const;
  158. void set_contact_monitor(bool p_enabled);
  159. bool is_contact_monitor_enabled() const;
  160. void set_max_contacts_reported(int p_amount);
  161. int get_max_contacts_reported() const;
  162. int get_contact_count() const;
  163. void set_continuous_collision_detection_mode(CCDMode p_mode);
  164. CCDMode get_continuous_collision_detection_mode() const;
  165. void apply_central_impulse(const Vector2 &p_impulse);
  166. void apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2());
  167. void apply_torque_impulse(real_t p_torque);
  168. void apply_central_force(const Vector2 &p_force);
  169. void apply_force(const Vector2 &p_force, const Vector2 &p_position = Vector2());
  170. void apply_torque(real_t p_torque);
  171. void add_constant_central_force(const Vector2 &p_force);
  172. void add_constant_force(const Vector2 &p_force, const Vector2 &p_position = Vector2());
  173. void add_constant_torque(real_t p_torque);
  174. void set_constant_force(const Vector2 &p_force);
  175. Vector2 get_constant_force() const;
  176. void set_constant_torque(real_t p_torque);
  177. real_t get_constant_torque() const;
  178. TypedArray<Node2D> get_colliding_bodies() const; //function for script
  179. virtual PackedStringArray get_configuration_warnings() const override;
  180. RigidBody2D();
  181. ~RigidBody2D();
  182. private:
  183. void _reload_physics_characteristics();
  184. };
  185. VARIANT_ENUM_CAST(RigidBody2D::FreezeMode);
  186. VARIANT_ENUM_CAST(RigidBody2D::CenterOfMassMode);
  187. VARIANT_ENUM_CAST(RigidBody2D::DampMode);
  188. VARIANT_ENUM_CAST(RigidBody2D::CCDMode);
  189. #endif // RIGID_BODY_2D_H