Path: blob/master/modules/jolt_physics/joints/jolt_pin_joint_3d.cpp
10278 views
/**************************************************************************/1/* jolt_pin_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_pin_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/PointConstraint.h"3738namespace {3940constexpr double PIN_DEFAULT_BIAS = 0.3;41constexpr double PIN_DEFAULT_DAMPING = 1.0;42constexpr double PIN_DEFAULT_IMPULSE_CLAMP = 0.0;4344} // namespace4546JPH::Constraint *JoltPinJoint3D::_build_pin(JPH::Body *p_jolt_body_a, JPH::Body *p_jolt_body_b, const Transform3D &p_shifted_ref_a, const Transform3D &p_shifted_ref_b) {47JPH::PointConstraintSettings constraint_settings;48constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;49constraint_settings.mPoint1 = to_jolt_r(p_shifted_ref_a.origin);50constraint_settings.mPoint2 = to_jolt_r(p_shifted_ref_b.origin);5152if (p_jolt_body_a == nullptr) {53return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b);54} else if (p_jolt_body_b == nullptr) {55return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld);56} else {57return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b);58}59}6061void JoltPinJoint3D::_points_changed() {62rebuild();63_wake_up_bodies();64}6566JoltPinJoint3D::JoltPinJoint3D(const JoltJoint3D &p_old_joint, JoltBody3D *p_body_a, JoltBody3D *p_body_b, const Vector3 &p_local_a, const Vector3 &p_local_b) :67JoltJoint3D(p_old_joint, p_body_a, p_body_b, Transform3D({}, p_local_a), Transform3D({}, p_local_b)) {68rebuild();69}7071void JoltPinJoint3D::set_local_a(const Vector3 &p_local_a) {72local_ref_a = Transform3D({}, p_local_a);73_points_changed();74}7576void JoltPinJoint3D::set_local_b(const Vector3 &p_local_b) {77local_ref_b = Transform3D({}, p_local_b);78_points_changed();79}8081double JoltPinJoint3D::get_param(PhysicsServer3D::PinJointParam p_param) const {82switch (p_param) {83case PhysicsServer3D::PIN_JOINT_BIAS: {84return PIN_DEFAULT_BIAS;85}86case PhysicsServer3D::PIN_JOINT_DAMPING: {87return PIN_DEFAULT_DAMPING;88}89case PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP: {90return PIN_DEFAULT_IMPULSE_CLAMP;91}92default: {93ERR_FAIL_V_MSG(0.0, vformat("Unhandled pin joint parameter: '%d'. This should not happen. Please report this.", p_param));94}95}96}9798void JoltPinJoint3D::set_param(PhysicsServer3D::PinJointParam p_param, double p_value) {99switch (p_param) {100case PhysicsServer3D::PIN_JOINT_BIAS: {101if (!Math::is_equal_approx(p_value, PIN_DEFAULT_BIAS)) {102WARN_PRINT(vformat("Pin joint bias is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));103}104} break;105case PhysicsServer3D::PIN_JOINT_DAMPING: {106if (!Math::is_equal_approx(p_value, PIN_DEFAULT_DAMPING)) {107WARN_PRINT(vformat("Pin joint damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));108}109} break;110case PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP: {111if (!Math::is_equal_approx(p_value, PIN_DEFAULT_IMPULSE_CLAMP)) {112WARN_PRINT(vformat("Pin joint impulse clamp is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));113}114} break;115default: {116ERR_FAIL_MSG(vformat("Unhandled pin joint parameter: '%d'. This should not happen. Please report this.", p_param));117} break;118}119}120121float JoltPinJoint3D::get_applied_force() const {122JPH::PointConstraint *constraint = static_cast<JPH::PointConstraint *>(jolt_ref.GetPtr());123ERR_FAIL_NULL_V(constraint, 0.0f);124125JoltSpace3D *space = get_space();126ERR_FAIL_NULL_V(space, 0.0f);127128const float last_step = space->get_last_step();129if (unlikely(last_step == 0.0f)) {130return 0.0f;131}132133return constraint->GetTotalLambdaPosition().Length() / last_step;134}135136void JoltPinJoint3D::rebuild() {137destroy();138139JoltSpace3D *space = get_space();140if (space == nullptr) {141return;142}143144JPH::Body *jolt_body_a = body_a != nullptr ? body_a->get_jolt_body() : nullptr;145JPH::Body *jolt_body_b = body_b != nullptr ? body_b->get_jolt_body() : nullptr;146ERR_FAIL_COND(jolt_body_a == nullptr && jolt_body_b == nullptr);147148Transform3D shifted_ref_a;149Transform3D shifted_ref_b;150151_shift_reference_frames(Vector3(), Vector3(), shifted_ref_a, shifted_ref_b);152153jolt_ref = _build_pin(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b);154155space->add_joint(this);156157_update_enabled();158_update_iterations();159}160161162