physics_body_3d.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /**************************************************************************/
  2. /* physics_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 PHYSICS_BODY_3D_H
  31. #define PHYSICS_BODY_3D_H
  32. #include "core/templates/vset.h"
  33. #include "scene/3d/collision_object_3d.h"
  34. #include "scene/resources/physics_material.h"
  35. #include "servers/physics_server_3d.h"
  36. #include "skeleton_3d.h"
  37. class KinematicCollision3D;
  38. class PhysicsBody3D : public CollisionObject3D {
  39. GDCLASS(PhysicsBody3D, CollisionObject3D);
  40. protected:
  41. static void _bind_methods();
  42. PhysicsBody3D(PhysicsServer3D::BodyMode p_mode);
  43. Ref<KinematicCollision3D> motion_cache;
  44. uint16_t locked_axis = 0;
  45. Ref<KinematicCollision3D> _move(const Vector3 &p_motion, bool p_test_only = false, real_t p_margin = 0.001, bool p_recovery_as_collision = false, int p_max_collisions = 1);
  46. public:
  47. bool move_and_collide(const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult &r_result, bool p_test_only = false, bool p_cancel_sliding = true);
  48. bool test_move(const Transform3D &p_from, const Vector3 &p_motion, const Ref<KinematicCollision3D> &r_collision = Ref<KinematicCollision3D>(), real_t p_margin = 0.001, bool p_recovery_as_collision = false, int p_max_collisions = 1);
  49. void set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool p_lock);
  50. bool get_axis_lock(PhysicsServer3D::BodyAxis p_axis) const;
  51. virtual Vector3 get_linear_velocity() const;
  52. virtual Vector3 get_angular_velocity() const;
  53. virtual real_t get_inverse_mass() const;
  54. TypedArray<PhysicsBody3D> get_collision_exceptions();
  55. void add_collision_exception_with(Node *p_node); //must be physicsbody
  56. void remove_collision_exception_with(Node *p_node);
  57. virtual ~PhysicsBody3D();
  58. };
  59. class StaticBody3D : public PhysicsBody3D {
  60. GDCLASS(StaticBody3D, PhysicsBody3D);
  61. private:
  62. Vector3 constant_linear_velocity;
  63. Vector3 constant_angular_velocity;
  64. Ref<PhysicsMaterial> physics_material_override;
  65. protected:
  66. static void _bind_methods();
  67. public:
  68. void set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override);
  69. Ref<PhysicsMaterial> get_physics_material_override() const;
  70. void set_constant_linear_velocity(const Vector3 &p_vel);
  71. void set_constant_angular_velocity(const Vector3 &p_vel);
  72. Vector3 get_constant_linear_velocity() const;
  73. Vector3 get_constant_angular_velocity() const;
  74. StaticBody3D(PhysicsServer3D::BodyMode p_mode = PhysicsServer3D::BODY_MODE_STATIC);
  75. private:
  76. void _reload_physics_characteristics();
  77. };
  78. class AnimatableBody3D : public StaticBody3D {
  79. GDCLASS(AnimatableBody3D, StaticBody3D);
  80. private:
  81. Vector3 linear_velocity;
  82. Vector3 angular_velocity;
  83. bool sync_to_physics = true;
  84. Transform3D last_valid_transform;
  85. static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state);
  86. void _body_state_changed(PhysicsDirectBodyState3D *p_state);
  87. protected:
  88. void _notification(int p_what);
  89. static void _bind_methods();
  90. public:
  91. virtual Vector3 get_linear_velocity() const override;
  92. virtual Vector3 get_angular_velocity() const override;
  93. AnimatableBody3D();
  94. private:
  95. void _update_kinematic_motion();
  96. void set_sync_to_physics(bool p_enable);
  97. bool is_sync_to_physics_enabled() const;
  98. };
  99. class RigidBody3D : public PhysicsBody3D {
  100. GDCLASS(RigidBody3D, PhysicsBody3D);
  101. public:
  102. enum FreezeMode {
  103. FREEZE_MODE_STATIC,
  104. FREEZE_MODE_KINEMATIC,
  105. };
  106. enum CenterOfMassMode {
  107. CENTER_OF_MASS_MODE_AUTO,
  108. CENTER_OF_MASS_MODE_CUSTOM,
  109. };
  110. enum DampMode {
  111. DAMP_MODE_COMBINE,
  112. DAMP_MODE_REPLACE,
  113. };
  114. private:
  115. bool can_sleep = true;
  116. bool lock_rotation = false;
  117. bool freeze = false;
  118. FreezeMode freeze_mode = FREEZE_MODE_STATIC;
  119. real_t mass = 1.0;
  120. Vector3 inertia;
  121. CenterOfMassMode center_of_mass_mode = CENTER_OF_MASS_MODE_AUTO;
  122. Vector3 center_of_mass;
  123. Ref<PhysicsMaterial> physics_material_override;
  124. Vector3 linear_velocity;
  125. Vector3 angular_velocity;
  126. Basis inverse_inertia_tensor;
  127. real_t gravity_scale = 1.0;
  128. DampMode linear_damp_mode = DAMP_MODE_COMBINE;
  129. DampMode angular_damp_mode = DAMP_MODE_COMBINE;
  130. real_t linear_damp = 0.0;
  131. real_t angular_damp = 0.0;
  132. bool sleeping = false;
  133. bool ccd = false;
  134. int max_contacts_reported = 0;
  135. bool custom_integrator = false;
  136. struct ShapePair {
  137. int body_shape = 0;
  138. int local_shape = 0;
  139. bool tagged = false;
  140. bool operator<(const ShapePair &p_sp) const {
  141. if (body_shape == p_sp.body_shape) {
  142. return local_shape < p_sp.local_shape;
  143. } else {
  144. return body_shape < p_sp.body_shape;
  145. }
  146. }
  147. ShapePair() {}
  148. ShapePair(int p_bs, int p_ls) {
  149. body_shape = p_bs;
  150. local_shape = p_ls;
  151. tagged = false;
  152. }
  153. };
  154. struct RigidBody3D_RemoveAction {
  155. RID rid;
  156. ObjectID body_id;
  157. ShapePair pair;
  158. };
  159. struct BodyState {
  160. RID rid;
  161. //int rc;
  162. bool in_tree = false;
  163. VSet<ShapePair> shapes;
  164. };
  165. struct ContactMonitor {
  166. bool locked = false;
  167. HashMap<ObjectID, BodyState> body_map;
  168. };
  169. ContactMonitor *contact_monitor = nullptr;
  170. void _body_enter_tree(ObjectID p_id);
  171. void _body_exit_tree(ObjectID p_id);
  172. void _body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape);
  173. static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state);
  174. void _sync_body_state(PhysicsDirectBodyState3D *p_state);
  175. protected:
  176. void _notification(int p_what);
  177. static void _bind_methods();
  178. void _validate_property(PropertyInfo &p_property) const;
  179. GDVIRTUAL1(_integrate_forces, PhysicsDirectBodyState3D *)
  180. virtual void _body_state_changed(PhysicsDirectBodyState3D *p_state);
  181. void _apply_body_mode();
  182. public:
  183. void set_lock_rotation_enabled(bool p_lock_rotation);
  184. bool is_lock_rotation_enabled() const;
  185. void set_freeze_enabled(bool p_freeze);
  186. bool is_freeze_enabled() const;
  187. void set_freeze_mode(FreezeMode p_freeze_mode);
  188. FreezeMode get_freeze_mode() const;
  189. void set_mass(real_t p_mass);
  190. real_t get_mass() const;
  191. virtual real_t get_inverse_mass() const override { return 1.0 / mass; }
  192. void set_inertia(const Vector3 &p_inertia);
  193. const Vector3 &get_inertia() const;
  194. void set_center_of_mass_mode(CenterOfMassMode p_mode);
  195. CenterOfMassMode get_center_of_mass_mode() const;
  196. void set_center_of_mass(const Vector3 &p_center_of_mass);
  197. const Vector3 &get_center_of_mass() const;
  198. void set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override);
  199. Ref<PhysicsMaterial> get_physics_material_override() const;
  200. void set_linear_velocity(const Vector3 &p_velocity);
  201. Vector3 get_linear_velocity() const override;
  202. void set_axis_velocity(const Vector3 &p_axis);
  203. void set_angular_velocity(const Vector3 &p_velocity);
  204. Vector3 get_angular_velocity() const override;
  205. Basis get_inverse_inertia_tensor() const;
  206. void set_gravity_scale(real_t p_gravity_scale);
  207. real_t get_gravity_scale() const;
  208. void set_linear_damp_mode(DampMode p_mode);
  209. DampMode get_linear_damp_mode() const;
  210. void set_angular_damp_mode(DampMode p_mode);
  211. DampMode get_angular_damp_mode() const;
  212. void set_linear_damp(real_t p_linear_damp);
  213. real_t get_linear_damp() const;
  214. void set_angular_damp(real_t p_angular_damp);
  215. real_t get_angular_damp() const;
  216. void set_use_custom_integrator(bool p_enable);
  217. bool is_using_custom_integrator();
  218. void set_sleeping(bool p_sleeping);
  219. bool is_sleeping() const;
  220. void set_can_sleep(bool p_active);
  221. bool is_able_to_sleep() const;
  222. void set_contact_monitor(bool p_enabled);
  223. bool is_contact_monitor_enabled() const;
  224. void set_max_contacts_reported(int p_amount);
  225. int get_max_contacts_reported() const;
  226. int get_contact_count() const;
  227. void set_use_continuous_collision_detection(bool p_enable);
  228. bool is_using_continuous_collision_detection() const;
  229. TypedArray<Node3D> get_colliding_bodies() const;
  230. void apply_central_impulse(const Vector3 &p_impulse);
  231. void apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position = Vector3());
  232. void apply_torque_impulse(const Vector3 &p_impulse);
  233. void apply_central_force(const Vector3 &p_force);
  234. void apply_force(const Vector3 &p_force, const Vector3 &p_position = Vector3());
  235. void apply_torque(const Vector3 &p_torque);
  236. void add_constant_central_force(const Vector3 &p_force);
  237. void add_constant_force(const Vector3 &p_force, const Vector3 &p_position = Vector3());
  238. void add_constant_torque(const Vector3 &p_torque);
  239. void set_constant_force(const Vector3 &p_force);
  240. Vector3 get_constant_force() const;
  241. void set_constant_torque(const Vector3 &p_torque);
  242. Vector3 get_constant_torque() const;
  243. virtual PackedStringArray get_configuration_warnings() const override;
  244. RigidBody3D();
  245. ~RigidBody3D();
  246. private:
  247. void _reload_physics_characteristics();
  248. };
  249. VARIANT_ENUM_CAST(RigidBody3D::FreezeMode);
  250. VARIANT_ENUM_CAST(RigidBody3D::CenterOfMassMode);
  251. VARIANT_ENUM_CAST(RigidBody3D::DampMode);
  252. class KinematicCollision3D;
  253. class CharacterBody3D : public PhysicsBody3D {
  254. GDCLASS(CharacterBody3D, PhysicsBody3D);
  255. public:
  256. enum MotionMode {
  257. MOTION_MODE_GROUNDED,
  258. MOTION_MODE_FLOATING,
  259. };
  260. enum PlatformOnLeave {
  261. PLATFORM_ON_LEAVE_ADD_VELOCITY,
  262. PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY,
  263. PLATFORM_ON_LEAVE_DO_NOTHING,
  264. };
  265. bool move_and_slide();
  266. void apply_floor_snap();
  267. const Vector3 &get_velocity() const;
  268. void set_velocity(const Vector3 &p_velocity);
  269. bool is_on_floor() const;
  270. bool is_on_floor_only() const;
  271. bool is_on_wall() const;
  272. bool is_on_wall_only() const;
  273. bool is_on_ceiling() const;
  274. bool is_on_ceiling_only() const;
  275. const Vector3 &get_last_motion() const;
  276. Vector3 get_position_delta() const;
  277. const Vector3 &get_floor_normal() const;
  278. const Vector3 &get_wall_normal() const;
  279. const Vector3 &get_real_velocity() const;
  280. real_t get_floor_angle(const Vector3 &p_up_direction = Vector3(0.0, 1.0, 0.0)) const;
  281. const Vector3 &get_platform_velocity() const;
  282. const Vector3 &get_platform_angular_velocity() const;
  283. virtual Vector3 get_linear_velocity() const override;
  284. int get_slide_collision_count() const;
  285. PhysicsServer3D::MotionResult get_slide_collision(int p_bounce) const;
  286. CharacterBody3D();
  287. ~CharacterBody3D();
  288. private:
  289. real_t margin = 0.001;
  290. MotionMode motion_mode = MOTION_MODE_GROUNDED;
  291. PlatformOnLeave platform_on_leave = PLATFORM_ON_LEAVE_ADD_VELOCITY;
  292. union CollisionState {
  293. uint32_t state = 0;
  294. struct {
  295. bool floor;
  296. bool wall;
  297. bool ceiling;
  298. };
  299. CollisionState() {
  300. }
  301. CollisionState(bool p_floor, bool p_wall, bool p_ceiling) {
  302. floor = p_floor;
  303. wall = p_wall;
  304. ceiling = p_ceiling;
  305. }
  306. };
  307. CollisionState collision_state;
  308. bool floor_constant_speed = false;
  309. bool floor_stop_on_slope = true;
  310. bool floor_block_on_wall = true;
  311. bool slide_on_ceiling = true;
  312. int max_slides = 6;
  313. int platform_layer = 0;
  314. RID platform_rid;
  315. ObjectID platform_object_id;
  316. uint32_t platform_floor_layers = UINT32_MAX;
  317. uint32_t platform_wall_layers = 0;
  318. real_t floor_snap_length = 0.1;
  319. real_t floor_max_angle = Math::deg_to_rad((real_t)45.0);
  320. real_t wall_min_slide_angle = Math::deg_to_rad((real_t)15.0);
  321. Vector3 up_direction = Vector3(0.0, 1.0, 0.0);
  322. Vector3 velocity;
  323. Vector3 floor_normal;
  324. Vector3 wall_normal;
  325. Vector3 ceiling_normal;
  326. Vector3 last_motion;
  327. Vector3 platform_velocity;
  328. Vector3 platform_angular_velocity;
  329. Vector3 platform_ceiling_velocity;
  330. Vector3 previous_position;
  331. Vector3 real_velocity;
  332. Vector<PhysicsServer3D::MotionResult> motion_results;
  333. Vector<Ref<KinematicCollision3D>> slide_colliders;
  334. void set_safe_margin(real_t p_margin);
  335. real_t get_safe_margin() const;
  336. bool is_floor_stop_on_slope_enabled() const;
  337. void set_floor_stop_on_slope_enabled(bool p_enabled);
  338. bool is_floor_constant_speed_enabled() const;
  339. void set_floor_constant_speed_enabled(bool p_enabled);
  340. bool is_floor_block_on_wall_enabled() const;
  341. void set_floor_block_on_wall_enabled(bool p_enabled);
  342. bool is_slide_on_ceiling_enabled() const;
  343. void set_slide_on_ceiling_enabled(bool p_enabled);
  344. int get_max_slides() const;
  345. void set_max_slides(int p_max_slides);
  346. real_t get_floor_max_angle() const;
  347. void set_floor_max_angle(real_t p_radians);
  348. real_t get_floor_snap_length();
  349. void set_floor_snap_length(real_t p_floor_snap_length);
  350. real_t get_wall_min_slide_angle() const;
  351. void set_wall_min_slide_angle(real_t p_radians);
  352. uint32_t get_platform_floor_layers() const;
  353. void set_platform_floor_layers(const uint32_t p_exclude_layer);
  354. uint32_t get_platform_wall_layers() const;
  355. void set_platform_wall_layers(const uint32_t p_exclude_layer);
  356. void set_motion_mode(MotionMode p_mode);
  357. MotionMode get_motion_mode() const;
  358. void set_platform_on_leave(PlatformOnLeave p_on_leave_velocity);
  359. PlatformOnLeave get_platform_on_leave() const;
  360. void _move_and_slide_floating(double p_delta);
  361. void _move_and_slide_grounded(double p_delta, bool p_was_on_floor);
  362. Ref<KinematicCollision3D> _get_slide_collision(int p_bounce);
  363. Ref<KinematicCollision3D> _get_last_slide_collision();
  364. const Vector3 &get_up_direction() const;
  365. bool _on_floor_if_snapped(bool p_was_on_floor, bool p_vel_dir_facing_up);
  366. void set_up_direction(const Vector3 &p_up_direction);
  367. void _set_collision_direction(const PhysicsServer3D::MotionResult &p_result, CollisionState &r_state, CollisionState p_apply_state = CollisionState(true, true, true));
  368. void _set_platform_data(const PhysicsServer3D::MotionCollision &p_collision);
  369. void _snap_on_floor(bool p_was_on_floor, bool p_vel_dir_facing_up);
  370. protected:
  371. void _notification(int p_what);
  372. static void _bind_methods();
  373. void _validate_property(PropertyInfo &p_property) const;
  374. };
  375. VARIANT_ENUM_CAST(CharacterBody3D::MotionMode);
  376. VARIANT_ENUM_CAST(CharacterBody3D::PlatformOnLeave);
  377. class KinematicCollision3D : public RefCounted {
  378. GDCLASS(KinematicCollision3D, RefCounted);
  379. PhysicsBody3D *owner = nullptr;
  380. friend class PhysicsBody3D;
  381. friend class CharacterBody3D;
  382. PhysicsServer3D::MotionResult result;
  383. protected:
  384. static void _bind_methods();
  385. public:
  386. Vector3 get_travel() const;
  387. Vector3 get_remainder() const;
  388. int get_collision_count() const;
  389. real_t get_depth() const;
  390. Vector3 get_position(int p_collision_index = 0) const;
  391. Vector3 get_normal(int p_collision_index = 0) const;
  392. real_t get_angle(int p_collision_index = 0, const Vector3 &p_up_direction = Vector3(0.0, 1.0, 0.0)) const;
  393. Object *get_local_shape(int p_collision_index = 0) const;
  394. Object *get_collider(int p_collision_index = 0) const;
  395. ObjectID get_collider_id(int p_collision_index = 0) const;
  396. RID get_collider_rid(int p_collision_index = 0) const;
  397. Object *get_collider_shape(int p_collision_index = 0) const;
  398. int get_collider_shape_index(int p_collision_index = 0) const;
  399. Vector3 get_collider_velocity(int p_collision_index = 0) const;
  400. };
  401. class PhysicalBone3D : public PhysicsBody3D {
  402. GDCLASS(PhysicalBone3D, PhysicsBody3D);
  403. public:
  404. enum DampMode {
  405. DAMP_MODE_COMBINE,
  406. DAMP_MODE_REPLACE,
  407. };
  408. enum JointType {
  409. JOINT_TYPE_NONE,
  410. JOINT_TYPE_PIN,
  411. JOINT_TYPE_CONE,
  412. JOINT_TYPE_HINGE,
  413. JOINT_TYPE_SLIDER,
  414. JOINT_TYPE_6DOF
  415. };
  416. struct JointData {
  417. virtual JointType get_joint_type() { return JOINT_TYPE_NONE; }
  418. /// "j" is used to set the parameter inside the PhysicsServer3D
  419. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j);
  420. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  421. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  422. virtual ~JointData() {}
  423. };
  424. struct PinJointData : public JointData {
  425. virtual JointType get_joint_type() { return JOINT_TYPE_PIN; }
  426. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j);
  427. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  428. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  429. real_t bias = 0.3;
  430. real_t damping = 1.0;
  431. real_t impulse_clamp = 0.0;
  432. };
  433. struct ConeJointData : public JointData {
  434. virtual JointType get_joint_type() { return JOINT_TYPE_CONE; }
  435. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j);
  436. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  437. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  438. real_t swing_span = Math_PI * 0.25;
  439. real_t twist_span = Math_PI;
  440. real_t bias = 0.3;
  441. real_t softness = 0.8;
  442. real_t relaxation = 1.;
  443. };
  444. struct HingeJointData : public JointData {
  445. virtual JointType get_joint_type() { return JOINT_TYPE_HINGE; }
  446. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j);
  447. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  448. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  449. bool angular_limit_enabled = false;
  450. real_t angular_limit_upper = Math_PI * 0.5;
  451. real_t angular_limit_lower = -Math_PI * 0.5;
  452. real_t angular_limit_bias = 0.3;
  453. real_t angular_limit_softness = 0.9;
  454. real_t angular_limit_relaxation = 1.;
  455. };
  456. struct SliderJointData : public JointData {
  457. virtual JointType get_joint_type() { return JOINT_TYPE_SLIDER; }
  458. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j);
  459. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  460. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  461. real_t linear_limit_upper = 1.0;
  462. real_t linear_limit_lower = -1.0;
  463. real_t linear_limit_softness = 1.0;
  464. real_t linear_limit_restitution = 0.7;
  465. real_t linear_limit_damping = 1.0;
  466. real_t angular_limit_upper = 0.0;
  467. real_t angular_limit_lower = 0.0;
  468. real_t angular_limit_softness = 1.0;
  469. real_t angular_limit_restitution = 0.7;
  470. real_t angular_limit_damping = 1.0;
  471. };
  472. struct SixDOFJointData : public JointData {
  473. struct SixDOFAxisData {
  474. bool linear_limit_enabled = true;
  475. real_t linear_limit_upper = 0.0;
  476. real_t linear_limit_lower = 0.0;
  477. real_t linear_limit_softness = 0.7;
  478. real_t linear_restitution = 0.5;
  479. real_t linear_damping = 1.0;
  480. bool linear_spring_enabled = false;
  481. real_t linear_spring_stiffness = 0.0;
  482. real_t linear_spring_damping = 0.0;
  483. real_t linear_equilibrium_point = 0.0;
  484. bool angular_limit_enabled = true;
  485. real_t angular_limit_upper = 0.0;
  486. real_t angular_limit_lower = 0.0;
  487. real_t angular_limit_softness = 0.5;
  488. real_t angular_restitution = 0.0;
  489. real_t angular_damping = 1.0;
  490. real_t erp = 0.5;
  491. bool angular_spring_enabled = false;
  492. real_t angular_spring_stiffness = 0.0;
  493. real_t angular_spring_damping = 0.0;
  494. real_t angular_equilibrium_point = 0.0;
  495. };
  496. virtual JointType get_joint_type() { return JOINT_TYPE_6DOF; }
  497. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j);
  498. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  499. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  500. SixDOFAxisData axis_data[3];
  501. SixDOFJointData() {}
  502. };
  503. private:
  504. #ifdef TOOLS_ENABLED
  505. // if false gizmo move body
  506. bool gizmo_move_joint = false;
  507. #endif
  508. JointData *joint_data = nullptr;
  509. Transform3D joint_offset;
  510. RID joint;
  511. Skeleton3D *parent_skeleton = nullptr;
  512. Transform3D body_offset;
  513. Transform3D body_offset_inverse;
  514. bool simulate_physics = false;
  515. bool _internal_simulate_physics = false;
  516. int bone_id = -1;
  517. String bone_name;
  518. real_t bounce = 0.0;
  519. real_t mass = 1.0;
  520. real_t friction = 1.0;
  521. Vector3 linear_velocity;
  522. Vector3 angular_velocity;
  523. real_t gravity_scale = 1.0;
  524. bool can_sleep = true;
  525. bool custom_integrator = false;
  526. DampMode linear_damp_mode = DAMP_MODE_COMBINE;
  527. DampMode angular_damp_mode = DAMP_MODE_COMBINE;
  528. real_t linear_damp = 0.0;
  529. real_t angular_damp = 0.0;
  530. protected:
  531. bool _set(const StringName &p_name, const Variant &p_value);
  532. bool _get(const StringName &p_name, Variant &r_ret) const;
  533. void _get_property_list(List<PropertyInfo> *p_list) const;
  534. void _notification(int p_what);
  535. GDVIRTUAL1(_integrate_forces, PhysicsDirectBodyState3D *)
  536. static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state);
  537. void _body_state_changed(PhysicsDirectBodyState3D *p_state);
  538. static void _bind_methods();
  539. private:
  540. void _sync_body_state(PhysicsDirectBodyState3D *p_state);
  541. static Skeleton3D *find_skeleton_parent(Node *p_parent);
  542. void _update_joint_offset();
  543. void _fix_joint_offset();
  544. void _reload_joint();
  545. public:
  546. void _on_bone_parent_changed();
  547. void set_linear_velocity(const Vector3 &p_velocity);
  548. Vector3 get_linear_velocity() const override;
  549. void set_angular_velocity(const Vector3 &p_velocity);
  550. Vector3 get_angular_velocity() const override;
  551. void set_use_custom_integrator(bool p_enable);
  552. bool is_using_custom_integrator();
  553. #ifdef TOOLS_ENABLED
  554. void _set_gizmo_move_joint(bool p_move_joint);
  555. virtual Transform3D get_global_gizmo_transform() const override;
  556. virtual Transform3D get_local_gizmo_transform() const override;
  557. #endif
  558. const JointData *get_joint_data() const;
  559. Skeleton3D *find_skeleton_parent();
  560. int get_bone_id() const {
  561. return bone_id;
  562. }
  563. void set_joint_type(JointType p_joint_type);
  564. JointType get_joint_type() const;
  565. void set_joint_offset(const Transform3D &p_offset);
  566. const Transform3D &get_joint_offset() const;
  567. void set_joint_rotation(const Vector3 &p_euler_rad);
  568. Vector3 get_joint_rotation() const;
  569. void set_body_offset(const Transform3D &p_offset);
  570. const Transform3D &get_body_offset() const;
  571. void set_simulate_physics(bool p_simulate);
  572. bool get_simulate_physics();
  573. bool is_simulating_physics();
  574. void set_bone_name(const String &p_name);
  575. const String &get_bone_name() const;
  576. void set_mass(real_t p_mass);
  577. real_t get_mass() const;
  578. void set_friction(real_t p_friction);
  579. real_t get_friction() const;
  580. void set_bounce(real_t p_bounce);
  581. real_t get_bounce() const;
  582. void set_gravity_scale(real_t p_gravity_scale);
  583. real_t get_gravity_scale() const;
  584. void set_linear_damp_mode(DampMode p_mode);
  585. DampMode get_linear_damp_mode() const;
  586. void set_angular_damp_mode(DampMode p_mode);
  587. DampMode get_angular_damp_mode() const;
  588. void set_linear_damp(real_t p_linear_damp);
  589. real_t get_linear_damp() const;
  590. void set_angular_damp(real_t p_angular_damp);
  591. real_t get_angular_damp() const;
  592. void set_can_sleep(bool p_active);
  593. bool is_able_to_sleep() const;
  594. void apply_central_impulse(const Vector3 &p_impulse);
  595. void apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position = Vector3());
  596. void reset_physics_simulation_state();
  597. void reset_to_rest_position();
  598. PhysicalBone3D();
  599. ~PhysicalBone3D();
  600. private:
  601. void update_bone_id();
  602. void update_offset();
  603. void _start_physics_simulation();
  604. void _stop_physics_simulation();
  605. };
  606. VARIANT_ENUM_CAST(PhysicalBone3D::JointType);
  607. VARIANT_ENUM_CAST(PhysicalBone3D::DampMode);
  608. #endif // PHYSICS_BODY_3D_H