Path: blob/master/modules/godot_physics_3d/joints/godot_pin_joint_3d.cpp
10278 views
/**************************************************************************/1/* godot_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/*31Adapted to Godot from the Bullet library.32*/3334/*35Bullet Continuous Collision Detection and Physics Library36Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/3738This software is provided 'as-is', without any express or implied warranty.39In no event will the authors be held liable for any damages arising from the use of this software.40Permission is granted to anyone to use this software for any purpose,41including commercial applications, and to alter it and redistribute it freely,42subject to the following restrictions:43441. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.452. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.463. This notice may not be removed or altered from any source distribution.47*/4849#include "godot_pin_joint_3d.h"5051bool GodotPinJoint3D::setup(real_t p_step) {52dynamic_A = (A->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC);53dynamic_B = (B->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC);5455if (!dynamic_A && !dynamic_B) {56return false;57}5859m_appliedImpulse = real_t(0.);6061Vector3 normal(0, 0, 0);6263for (int i = 0; i < 3; i++) {64normal[i] = 1;65memnew_placement(66&m_jac[i],67GodotJacobianEntry3D(68A->get_principal_inertia_axes().transposed(),69B->get_principal_inertia_axes().transposed(),70A->get_transform().xform(m_pivotInA) - A->get_transform().origin - A->get_center_of_mass(),71B->get_transform().xform(m_pivotInB) - B->get_transform().origin - B->get_center_of_mass(),72normal,73A->get_inv_inertia(),74A->get_inv_mass(),75B->get_inv_inertia(),76B->get_inv_mass()));77normal[i] = 0;78}7980return true;81}8283void GodotPinJoint3D::solve(real_t p_step) {84Vector3 pivotAInW = A->get_transform().xform(m_pivotInA);85Vector3 pivotBInW = B->get_transform().xform(m_pivotInB);8687Vector3 normal(0, 0, 0);8889//Vector3 angvelA = A->get_transform().origin.getBasis().transpose() * A->getAngularVelocity();90//Vector3 angvelB = B->get_transform().origin.getBasis().transpose() * B->getAngularVelocity();9192for (int i = 0; i < 3; i++) {93normal[i] = 1;94real_t jacDiagABInv = real_t(1.) / m_jac[i].getDiagonal();9596Vector3 rel_pos1 = pivotAInW - A->get_transform().origin;97Vector3 rel_pos2 = pivotBInW - B->get_transform().origin;98//this jacobian entry could be reused for all iterations99100Vector3 vel1 = A->get_velocity_in_local_point(rel_pos1);101Vector3 vel2 = B->get_velocity_in_local_point(rel_pos2);102Vector3 vel = vel1 - vel2;103104real_t rel_vel;105rel_vel = normal.dot(vel);106107/*108//velocity error (first order error)109real_t rel_vel = m_jac[i].getRelativeVelocity(A->getLinearVelocity(),angvelA,110B->getLinearVelocity(),angvelB);111*/112113//positional error (zeroth order error)114real_t depth = -(pivotAInW - pivotBInW).dot(normal); //this is the error projected on the normal115116real_t impulse = depth * m_tau / p_step * jacDiagABInv - m_damping * rel_vel * jacDiagABInv;117118real_t impulseClamp = m_impulseClamp;119if (impulseClamp > 0) {120if (impulse < -impulseClamp) {121impulse = -impulseClamp;122}123if (impulse > impulseClamp) {124impulse = impulseClamp;125}126}127128m_appliedImpulse += impulse;129Vector3 impulse_vector = normal * impulse;130if (dynamic_A) {131A->apply_impulse(impulse_vector, pivotAInW - A->get_transform().origin);132}133if (dynamic_B) {134B->apply_impulse(-impulse_vector, pivotBInW - B->get_transform().origin);135}136137normal[i] = 0;138}139}140141void GodotPinJoint3D::set_param(PhysicsServer3D::PinJointParam p_param, real_t p_value) {142switch (p_param) {143case PhysicsServer3D::PIN_JOINT_BIAS:144m_tau = p_value;145break;146case PhysicsServer3D::PIN_JOINT_DAMPING:147m_damping = p_value;148break;149case PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP:150m_impulseClamp = p_value;151break;152}153}154155real_t GodotPinJoint3D::get_param(PhysicsServer3D::PinJointParam p_param) const {156switch (p_param) {157case PhysicsServer3D::PIN_JOINT_BIAS:158return m_tau;159case PhysicsServer3D::PIN_JOINT_DAMPING:160return m_damping;161case PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP:162return m_impulseClamp;163}164165return 0;166}167168GodotPinJoint3D::GodotPinJoint3D(GodotBody3D *p_body_a, const Vector3 &p_pos_a, GodotBody3D *p_body_b, const Vector3 &p_pos_b) :169GodotJoint3D(_arr, 2) {170A = p_body_a;171B = p_body_b;172m_pivotInA = p_pos_a;173m_pivotInB = p_pos_b;174175A->add_constraint(this, 0);176B->add_constraint(this, 1);177}178179GodotPinJoint3D::~GodotPinJoint3D() {180}181182183