rigid_body_3d.h 7.8 KB

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