rigid_body_3d.h 7.8 KB

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