jolt_generic_6dof_joint_3d.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**************************************************************************/
  2. /* jolt_generic_6dof_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_GENERIC_6DOF_JOINT_3D_H
  31. #define JOLT_GENERIC_6DOF_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/SixDOFConstraint.h"
  36. class JoltGeneric6DOFJoint3D final : public JoltJoint3D {
  37. typedef Vector3::Axis Axis;
  38. typedef JPH::SixDOFConstraintSettings::EAxis JoltAxis;
  39. typedef PhysicsServer3D::G6DOFJointAxisParam Param;
  40. typedef JoltPhysicsServer3D::G6DOFJointAxisParamJolt JoltParam;
  41. typedef PhysicsServer3D::G6DOFJointAxisFlag Flag;
  42. typedef JoltPhysicsServer3D::G6DOFJointAxisFlagJolt JoltFlag;
  43. enum {
  44. AXIS_LINEAR_X = JoltAxis::TranslationX,
  45. AXIS_LINEAR_Y = JoltAxis::TranslationY,
  46. AXIS_LINEAR_Z = JoltAxis::TranslationZ,
  47. AXIS_ANGULAR_X = JoltAxis::RotationX,
  48. AXIS_ANGULAR_Y = JoltAxis::RotationY,
  49. AXIS_ANGULAR_Z = JoltAxis::RotationZ,
  50. AXIS_COUNT = JoltAxis::Num,
  51. AXES_LINEAR = AXIS_LINEAR_X,
  52. AXES_ANGULAR = AXIS_ANGULAR_X,
  53. };
  54. double limit_lower[AXIS_COUNT] = {};
  55. double limit_upper[AXIS_COUNT] = {};
  56. double limit_spring_frequency[AXIS_COUNT] = {};
  57. double limit_spring_damping[AXIS_COUNT] = {};
  58. double motor_speed[AXIS_COUNT] = {};
  59. double motor_limit[AXIS_COUNT] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
  60. double spring_stiffness[AXIS_COUNT] = {};
  61. double spring_frequency[AXIS_COUNT] = {};
  62. double spring_damping[AXIS_COUNT] = {};
  63. double spring_equilibrium[AXIS_COUNT] = {};
  64. double spring_limit[AXIS_COUNT] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
  65. bool limit_enabled[AXIS_COUNT] = {};
  66. bool limit_spring_enabled[AXIS_COUNT] = {};
  67. bool motor_enabled[AXIS_COUNT] = {};
  68. bool spring_enabled[AXIS_COUNT] = {};
  69. bool spring_use_frequency[AXIS_COUNT] = {};
  70. JPH::Constraint *_build_6dof(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;
  71. void _update_limit_spring_parameters(int p_axis);
  72. void _update_motor_state(int p_axis);
  73. void _update_motor_velocity(int p_axis);
  74. void _update_motor_limit(int p_axis);
  75. void _update_spring_parameters(int p_axis);
  76. void _update_spring_equilibrium(int p_axis);
  77. void _limits_changed();
  78. void _limit_spring_parameters_changed(int p_axis);
  79. void _motor_state_changed(int p_axis);
  80. void _motor_speed_changed(int p_axis);
  81. void _motor_limit_changed(int p_axis);
  82. void _spring_state_changed(int p_axis);
  83. void _spring_parameters_changed(int p_axis);
  84. void _spring_equilibrium_changed(int p_axis);
  85. void _spring_limit_changed(int p_axis);
  86. public:
  87. JoltGeneric6DOFJoint3D(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);
  88. virtual PhysicsServer3D::JointType get_type() const override { return PhysicsServer3D::JOINT_TYPE_6DOF; }
  89. double get_param(Axis p_axis, Param p_param) const;
  90. void set_param(Axis p_axis, Param p_param, double p_value);
  91. bool get_flag(Axis p_axis, Flag p_flag) const;
  92. void set_flag(Axis p_axis, Flag p_flag, bool p_enabled);
  93. double get_jolt_param(Axis p_axis, JoltParam p_param) const;
  94. void set_jolt_param(Axis p_axis, JoltParam p_param, double p_value);
  95. bool get_jolt_flag(Axis p_axis, JoltFlag p_flag) const;
  96. void set_jolt_flag(Axis p_axis, JoltFlag p_flag, bool p_enabled);
  97. float get_applied_force() const;
  98. float get_applied_torque() const;
  99. virtual void rebuild() override;
  100. };
  101. #endif // JOLT_GENERIC_6DOF_JOINT_3D_H