jolt_hinge_joint_3d.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**************************************************************************/
  2. /* jolt_hinge_joint_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 JOLT_HINGE_JOINT_3D_H
  31. #define JOLT_HINGE_JOINT_3D_H
  32. #include "../jolt_physics_server_3d.h"
  33. #include "jolt_joint_3d.h"
  34. #include "Jolt/Jolt.h"
  35. #include "Jolt/Physics/Constraints/SliderConstraint.h"
  36. class JoltHingeJoint3D final : public JoltJoint3D {
  37. typedef PhysicsServer3D::HingeJointParam Parameter;
  38. typedef JoltPhysicsServer3D::HingeJointParamJolt JoltParameter;
  39. typedef PhysicsServer3D::HingeJointFlag Flag;
  40. typedef JoltPhysicsServer3D::HingeJointFlagJolt JoltFlag;
  41. double limit_lower = 0.0;
  42. double limit_upper = 0.0;
  43. double limit_spring_frequency = 0.0;
  44. double limit_spring_damping = 0.0;
  45. double motor_target_speed = 0.0f;
  46. double motor_max_torque = FLT_MAX;
  47. bool limits_enabled = false;
  48. bool limit_spring_enabled = false;
  49. bool motor_enabled = false;
  50. JPH::Constraint *_build_hinge(JPH::Body *p_jolt_body_a, JPH::Body *p_jolt_body_b, const Transform3D &p_shifted_ref_a, const Transform3D &p_shifted_ref_b, float p_limit) const;
  51. JPH::Constraint *_build_fixed(JPH::Body *p_jolt_body_a, JPH::Body *p_jolt_body_b, const Transform3D &p_shifted_ref_a, const Transform3D &p_shifted_ref_b) const;
  52. bool _is_sprung() const { return limit_spring_enabled && limit_spring_frequency > 0.0; }
  53. bool _is_fixed() const { return limits_enabled && limit_lower == limit_upper && !_is_sprung(); }
  54. void _update_motor_state();
  55. void _update_motor_velocity();
  56. void _update_motor_limit();
  57. void _limits_changed();
  58. void _limit_spring_changed();
  59. void _motor_state_changed();
  60. void _motor_speed_changed();
  61. void _motor_limit_changed();
  62. public:
  63. JoltHingeJoint3D(const JoltJoint3D &p_old_joint, JoltBody3D *p_body_a, JoltBody3D *p_body_b, const Transform3D &p_local_ref_a, const Transform3D &p_local_ref_b);
  64. virtual PhysicsServer3D::JointType get_type() const override { return PhysicsServer3D::JOINT_TYPE_HINGE; }
  65. double get_param(Parameter p_param) const;
  66. void set_param(Parameter p_param, double p_value);
  67. double get_jolt_param(JoltParameter p_param) const;
  68. void set_jolt_param(JoltParameter p_param, double p_value);
  69. bool get_flag(Flag p_flag) const;
  70. void set_flag(Flag p_flag, bool p_enabled);
  71. bool get_jolt_flag(JoltFlag p_flag) const;
  72. void set_jolt_flag(JoltFlag p_flag, bool p_enabled);
  73. float get_applied_force() const;
  74. float get_applied_torque() const;
  75. virtual void rebuild() override;
  76. };
  77. #endif // JOLT_HINGE_JOINT_3D_H