Path: blob/master/modules/jolt_physics/joints/jolt_slider_joint_3d.cpp
10278 views
/**************************************************************************/1/* jolt_slider_joint_3d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "jolt_slider_joint_3d.h"3132#include "../misc/jolt_type_conversions.h"33#include "../objects/jolt_body_3d.h"34#include "../spaces/jolt_space_3d.h"3536#include "Jolt/Physics/Constraints/FixedConstraint.h"37#include "Jolt/Physics/Constraints/SliderConstraint.h"3839namespace {4041constexpr double SLIDER_DEFAULT_LINEAR_LIMIT_SOFTNESS = 1.0;42constexpr double SLIDER_DEFAULT_LINEAR_LIMIT_RESTITUTION = 0.7;43constexpr double SLIDER_DEFAULT_LINEAR_LIMIT_DAMPING = 1.0;4445constexpr double SLIDER_DEFAULT_LINEAR_MOTION_SOFTNESS = 1.0;46constexpr double SLIDER_DEFAULT_LINEAR_MOTION_RESTITUTION = 0.7;47constexpr double SLIDER_DEFAULT_LINEAR_MOTION_DAMPING = 0.0;4849constexpr double SLIDER_DEFAULT_LINEAR_ORTHO_SOFTNESS = 1.0;50constexpr double SLIDER_DEFAULT_LINEAR_ORTHO_RESTITUTION = 0.7;51constexpr double SLIDER_DEFAULT_LINEAR_ORTHO_DAMPING = 1.0;5253constexpr double SLIDER_DEFAULT_ANGULAR_LIMIT_UPPER = 0.0;54constexpr double SLIDER_DEFAULT_ANGULAR_LIMIT_LOWER = 0.0;55constexpr double SLIDER_DEFAULT_ANGULAR_LIMIT_SOFTNESS = 1.0;56constexpr double SLIDER_DEFAULT_ANGULAR_LIMIT_RESTITUTION = 0.7;57constexpr double SLIDER_DEFAULT_ANGULAR_LIMIT_DAMPING = 0.0;5859constexpr double SLIDER_DEFAULT_ANGULAR_MOTION_SOFTNESS = 1.0;60constexpr double SLIDER_DEFAULT_ANGULAR_MOTION_RESTITUTION = 0.7;61constexpr double SLIDER_DEFAULT_ANGULAR_MOTION_DAMPING = 1.0;6263constexpr double SLIDER_DEFAULT_ANGULAR_ORTHO_SOFTNESS = 1.0;64constexpr double SLIDER_DEFAULT_ANGULAR_ORTHO_RESTITUTION = 0.7;65constexpr double SLIDER_DEFAULT_ANGULAR_ORTHO_DAMPING = 1.0;6667} // namespace6869JPH::Constraint *JoltSliderJoint3D::_build_slider(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 {70JPH::SliderConstraintSettings constraint_settings;7172constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;73constraint_settings.mAutoDetectPoint = false;74constraint_settings.mPoint1 = to_jolt_r(p_shifted_ref_a.origin);75constraint_settings.mSliderAxis1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_X));76constraint_settings.mNormalAxis1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_Z));77constraint_settings.mPoint2 = to_jolt_r(p_shifted_ref_b.origin);78constraint_settings.mSliderAxis2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_X));79constraint_settings.mNormalAxis2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_Z));80constraint_settings.mLimitsMin = -p_limit;81constraint_settings.mLimitsMax = p_limit;8283if (limit_spring_enabled) {84constraint_settings.mLimitsSpringSettings.mFrequency = (float)limit_spring_frequency;85constraint_settings.mLimitsSpringSettings.mDamping = (float)limit_spring_damping;86}8788if (p_jolt_body_a == nullptr) {89return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b);90} else if (p_jolt_body_b == nullptr) {91return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld);92} else {93return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b);94}95}9697JPH::Constraint *JoltSliderJoint3D::_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 {98JPH::FixedConstraintSettings constraint_settings;99100constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;101constraint_settings.mAutoDetectPoint = false;102constraint_settings.mPoint1 = to_jolt_r(p_shifted_ref_a.origin);103constraint_settings.mAxisX1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_X));104constraint_settings.mAxisY1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_Y));105constraint_settings.mPoint2 = to_jolt_r(p_shifted_ref_b.origin);106constraint_settings.mAxisX2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_X));107constraint_settings.mAxisY2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_Y));108109if (p_jolt_body_a == nullptr) {110return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b);111} else if (p_jolt_body_b == nullptr) {112return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld);113} else {114return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b);115}116}117118void JoltSliderJoint3D::_update_motor_state() {119if (unlikely(_is_fixed())) {120return;121}122123if (JPH::SliderConstraint *constraint = static_cast<JPH::SliderConstraint *>(jolt_ref.GetPtr())) {124constraint->SetMotorState(motor_enabled ? JPH::EMotorState::Velocity : JPH::EMotorState::Off);125}126}127128void JoltSliderJoint3D::_update_motor_velocity() {129if (unlikely(_is_fixed())) {130return;131}132133if (JPH::SliderConstraint *constraint = static_cast<JPH::SliderConstraint *>(jolt_ref.GetPtr())) {134constraint->SetTargetVelocity((float)motor_target_speed);135}136}137138void JoltSliderJoint3D::_update_motor_limit() {139if (unlikely(_is_fixed())) {140return;141}142143if (JPH::SliderConstraint *constraint = static_cast<JPH::SliderConstraint *>(jolt_ref.GetPtr())) {144JPH::MotorSettings &motor_settings = constraint->GetMotorSettings();145motor_settings.mMinForceLimit = (float)-motor_max_force;146motor_settings.mMaxForceLimit = (float)motor_max_force;147}148}149150void JoltSliderJoint3D::_limits_changed() {151rebuild();152_wake_up_bodies();153}154155void JoltSliderJoint3D::_limit_spring_changed() {156rebuild();157_wake_up_bodies();158}159160void JoltSliderJoint3D::_motor_state_changed() {161_update_motor_state();162_wake_up_bodies();163}164165void JoltSliderJoint3D::_motor_speed_changed() {166_update_motor_velocity();167_wake_up_bodies();168}169170void JoltSliderJoint3D::_motor_limit_changed() {171_update_motor_limit();172_wake_up_bodies();173}174175JoltSliderJoint3D::JoltSliderJoint3D(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) :176JoltJoint3D(p_old_joint, p_body_a, p_body_b, p_local_ref_a, p_local_ref_b) {177rebuild();178}179180double JoltSliderJoint3D::get_param(PhysicsServer3D::SliderJointParam p_param) const {181switch (p_param) {182case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER: {183return limit_upper;184}185case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER: {186return limit_lower;187}188case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS: {189return SLIDER_DEFAULT_LINEAR_LIMIT_SOFTNESS;190}191case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION: {192return SLIDER_DEFAULT_LINEAR_LIMIT_RESTITUTION;193}194case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING: {195return SLIDER_DEFAULT_LINEAR_LIMIT_DAMPING;196}197case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_SOFTNESS: {198return SLIDER_DEFAULT_LINEAR_MOTION_SOFTNESS;199}200case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_RESTITUTION: {201return SLIDER_DEFAULT_LINEAR_MOTION_RESTITUTION;202}203case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_DAMPING: {204return SLIDER_DEFAULT_LINEAR_MOTION_DAMPING;205}206case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS: {207return SLIDER_DEFAULT_LINEAR_ORTHO_SOFTNESS;208}209case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION: {210return SLIDER_DEFAULT_LINEAR_ORTHO_RESTITUTION;211}212case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING: {213return SLIDER_DEFAULT_LINEAR_ORTHO_DAMPING;214}215case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER: {216return SLIDER_DEFAULT_ANGULAR_LIMIT_UPPER;217}218case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER: {219return SLIDER_DEFAULT_ANGULAR_LIMIT_LOWER;220}221case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS: {222return SLIDER_DEFAULT_ANGULAR_LIMIT_SOFTNESS;223}224case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION: {225return SLIDER_DEFAULT_ANGULAR_LIMIT_RESTITUTION;226}227case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING: {228return SLIDER_DEFAULT_ANGULAR_LIMIT_DAMPING;229}230case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS: {231return SLIDER_DEFAULT_ANGULAR_MOTION_SOFTNESS;232}233case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION: {234return SLIDER_DEFAULT_ANGULAR_MOTION_RESTITUTION;235}236case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_DAMPING: {237return SLIDER_DEFAULT_ANGULAR_MOTION_DAMPING;238}239case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS: {240return SLIDER_DEFAULT_ANGULAR_ORTHO_SOFTNESS;241}242case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION: {243return SLIDER_DEFAULT_ANGULAR_ORTHO_RESTITUTION;244}245case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING: {246return SLIDER_DEFAULT_ANGULAR_ORTHO_DAMPING;247}248default: {249ERR_FAIL_V_MSG(0.0, vformat("Unhandled slider joint parameter: '%d'. This should not happen. Please report this.", p_param));250}251}252}253254void JoltSliderJoint3D::set_param(PhysicsServer3D::SliderJointParam p_param, double p_value) {255switch (p_param) {256case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER: {257limit_upper = p_value;258_limits_changed();259} break;260case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER: {261limit_lower = p_value;262_limits_changed();263} break;264case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS: {265if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_LIMIT_SOFTNESS)) {266WARN_PRINT(vformat("Slider joint linear limit softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));267}268} break;269case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION: {270if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_LIMIT_RESTITUTION)) {271WARN_PRINT(vformat("Slider joint linear limit restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));272}273} break;274case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING: {275if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_LIMIT_DAMPING)) {276WARN_PRINT(vformat("Slider joint linear limit damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));277}278} break;279case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_SOFTNESS: {280if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_MOTION_SOFTNESS)) {281WARN_PRINT(vformat("Slider joint linear motion softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));282}283} break;284case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_RESTITUTION: {285if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_MOTION_RESTITUTION)) {286WARN_PRINT(vformat("Slider joint linear motion restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));287}288} break;289case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_DAMPING: {290if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_MOTION_DAMPING)) {291WARN_PRINT(vformat("Slider joint linear motion damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));292}293} break;294case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS: {295if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_ORTHO_SOFTNESS)) {296WARN_PRINT(vformat("Slider joint linear ortho softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));297}298} break;299case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION: {300if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_ORTHO_RESTITUTION)) {301WARN_PRINT(vformat("Slider joint linear ortho restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));302}303} break;304case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING: {305if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_ORTHO_DAMPING)) {306WARN_PRINT(vformat("Slider joint linear ortho damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));307}308} break;309case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER: {310if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_LIMIT_UPPER)) {311WARN_PRINT(vformat("Slider joint angular limits are not supported when using Jolt Physics. Any such value will be ignored. Try using Generic6DOFJoint3D instead. This joint connects %s.", _bodies_to_string()));312}313} break;314case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER: {315if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_LIMIT_LOWER)) {316WARN_PRINT(vformat("Slider joint angular limits are not supported when using Jolt Physics. Any such value will be ignored. Try using Generic6DOFJoint3D instead. This joint connects %s.", _bodies_to_string()));317}318} break;319case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS: {320if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_LIMIT_SOFTNESS)) {321WARN_PRINT(vformat("Slider joint angular limit softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));322}323} break;324case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION: {325if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_LIMIT_RESTITUTION)) {326WARN_PRINT(vformat("Slider joint angular limit restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));327}328} break;329case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING: {330if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_LIMIT_DAMPING)) {331WARN_PRINT(vformat("Slider joint angular limit damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));332}333} break;334case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS: {335if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_MOTION_SOFTNESS)) {336WARN_PRINT(vformat("Slider joint angular motion softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));337}338} break;339case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION: {340if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_MOTION_RESTITUTION)) {341WARN_PRINT(vformat("Slider joint angular motion restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));342}343} break;344case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_DAMPING: {345if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_MOTION_DAMPING)) {346WARN_PRINT(vformat("Slider joint angular motion damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));347}348} break;349case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS: {350if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_ORTHO_SOFTNESS)) {351WARN_PRINT(vformat("Slider joint angular ortho softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));352}353} break;354case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION: {355if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_ORTHO_RESTITUTION)) {356WARN_PRINT(vformat("Slider joint angular ortho restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));357}358} break;359case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING: {360if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_ORTHO_DAMPING)) {361WARN_PRINT(vformat("Slider joint angular ortho damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));362}363} break;364default: {365ERR_FAIL_MSG(vformat("Unhandled slider joint parameter: '%d'. This should not happen. Please report this.", p_param));366} break;367}368}369370double JoltSliderJoint3D::get_jolt_param(JoltParameter p_param) const {371switch (p_param) {372case JoltPhysicsServer3D::SLIDER_JOINT_LIMIT_SPRING_FREQUENCY: {373return limit_spring_frequency;374}375case JoltPhysicsServer3D::SLIDER_JOINT_LIMIT_SPRING_DAMPING: {376return limit_spring_damping;377}378case JoltPhysicsServer3D::SLIDER_JOINT_MOTOR_TARGET_VELOCITY: {379return motor_target_speed;380}381case JoltPhysicsServer3D::SLIDER_JOINT_MOTOR_MAX_FORCE: {382return motor_max_force;383}384default: {385ERR_FAIL_V_MSG(0.0, vformat("Unhandled parameter: '%d'. This should not happen. Please report this.", p_param));386}387}388}389390void JoltSliderJoint3D::set_jolt_param(JoltParameter p_param, double p_value) {391switch (p_param) {392case JoltPhysicsServer3D::SLIDER_JOINT_LIMIT_SPRING_FREQUENCY: {393limit_spring_frequency = p_value;394_limit_spring_changed();395} break;396case JoltPhysicsServer3D::SLIDER_JOINT_LIMIT_SPRING_DAMPING: {397limit_spring_damping = p_value;398_limit_spring_changed();399} break;400case JoltPhysicsServer3D::SLIDER_JOINT_MOTOR_TARGET_VELOCITY: {401motor_target_speed = p_value;402_motor_speed_changed();403} break;404case JoltPhysicsServer3D::SLIDER_JOINT_MOTOR_MAX_FORCE: {405motor_max_force = p_value;406_motor_limit_changed();407} break;408default: {409ERR_FAIL_MSG(vformat("Unhandled parameter: '%d'. This should not happen. Please report this.", p_param));410} break;411}412}413414bool JoltSliderJoint3D::get_jolt_flag(JoltFlag p_flag) const {415switch (p_flag) {416case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_USE_LIMIT: {417return limits_enabled;418}419case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_USE_LIMIT_SPRING: {420return limit_spring_enabled;421}422case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_ENABLE_MOTOR: {423return motor_enabled;424}425default: {426ERR_FAIL_V_MSG(false, vformat("Unhandled flag: '%d'. This should not happen. Please report this.", p_flag));427}428}429}430431void JoltSliderJoint3D::set_jolt_flag(JoltFlag p_flag, bool p_enabled) {432switch (p_flag) {433case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_USE_LIMIT: {434limits_enabled = p_enabled;435_limits_changed();436} break;437case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_USE_LIMIT_SPRING: {438limit_spring_enabled = p_enabled;439_limit_spring_changed();440} break;441case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_ENABLE_MOTOR: {442motor_enabled = p_enabled;443_motor_state_changed();444} break;445default: {446ERR_FAIL_MSG(vformat("Unhandled flag: '%d'. This should not happen. Please report this.", p_flag));447} break;448}449}450451float JoltSliderJoint3D::get_applied_force() const {452ERR_FAIL_NULL_V(jolt_ref, 0.0f);453454JoltSpace3D *space = get_space();455ERR_FAIL_NULL_V(space, 0.0f);456457const float last_step = space->get_last_step();458if (unlikely(last_step == 0.0f)) {459return 0.0f;460}461462if (_is_fixed()) {463JPH::FixedConstraint *constraint = static_cast<JPH::FixedConstraint *>(jolt_ref.GetPtr());464return constraint->GetTotalLambdaPosition().Length() / last_step;465} else {466JPH::SliderConstraint *constraint = static_cast<JPH::SliderConstraint *>(jolt_ref.GetPtr());467const JPH::Vec3 total_lambda = JPH::Vec3(constraint->GetTotalLambdaPosition()[0], constraint->GetTotalLambdaPosition()[1], constraint->GetTotalLambdaPositionLimits() + constraint->GetTotalLambdaMotor());468return total_lambda.Length() / last_step;469}470}471472float JoltSliderJoint3D::get_applied_torque() const {473ERR_FAIL_NULL_V(jolt_ref, 0.0f);474475JoltSpace3D *space = get_space();476ERR_FAIL_NULL_V(space, 0.0f);477478const float last_step = space->get_last_step();479if (unlikely(last_step == 0.0f)) {480return 0.0f;481}482483if (_is_fixed()) {484JPH::FixedConstraint *constraint = static_cast<JPH::FixedConstraint *>(jolt_ref.GetPtr());485return constraint->GetTotalLambdaRotation().Length() / last_step;486} else {487JPH::SliderConstraint *constraint = static_cast<JPH::SliderConstraint *>(jolt_ref.GetPtr());488return constraint->GetTotalLambdaRotation().Length() / last_step;489}490}491492void JoltSliderJoint3D::rebuild() {493destroy();494495JoltSpace3D *space = get_space();496if (space == nullptr) {497return;498}499500JPH::Body *jolt_body_a = body_a != nullptr ? body_a->get_jolt_body() : nullptr;501JPH::Body *jolt_body_b = body_b != nullptr ? body_b->get_jolt_body() : nullptr;502ERR_FAIL_COND(jolt_body_a == nullptr && jolt_body_b == nullptr);503504float ref_shift = 0.0f;505float limit = FLT_MAX;506507if (limits_enabled && limit_lower <= limit_upper) {508const double limit_midpoint = (limit_lower + limit_upper) / 2.0f;509510ref_shift = float(-limit_midpoint);511limit = float(limit_upper - limit_midpoint);512}513514Transform3D shifted_ref_a;515Transform3D shifted_ref_b;516517_shift_reference_frames(Vector3(ref_shift, 0.0f, 0.0f), Vector3(), shifted_ref_a, shifted_ref_b);518519if (_is_fixed()) {520jolt_ref = _build_fixed(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b);521} else {522jolt_ref = _build_slider(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b, limit);523}524525space->add_joint(this);526527_update_enabled();528_update_iterations();529_update_motor_state();530_update_motor_velocity();531_update_motor_limit();532}533534535