Path: blob/master/modules/godot_physics_3d/joints/godot_jacobian_entry_3d.h
10278 views
/**************************************************************************/1/* godot_jacobian_entry_3d.h */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#pragma once3132/*33Adapted to Godot from the Bullet library.34*/3536/*37Bullet Continuous Collision Detection and Physics Library38Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/3940This software is provided 'as-is', without any express or implied warranty.41In no event will the authors be held liable for any damages arising from the use of this software.42Permission is granted to anyone to use this software for any purpose,43including commercial applications, and to alter it and redistribute it freely,44subject to the following restrictions:45461. 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.472. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.483. This notice may not be removed or altered from any source distribution.49*/5051#include "core/math/transform_3d.h"5253class GodotJacobianEntry3D {54public:55GodotJacobianEntry3D() {}56//constraint between two different rigidbodies57GodotJacobianEntry3D(58const Basis &world2A,59const Basis &world2B,60const Vector3 &rel_pos1, const Vector3 &rel_pos2,61const Vector3 &jointAxis,62const Vector3 &inertiaInvA,63const real_t massInvA,64const Vector3 &inertiaInvB,65const real_t massInvB) :66m_linearJointAxis(jointAxis) {67m_aJ = world2A.xform(rel_pos1.cross(m_linearJointAxis));68m_bJ = world2B.xform(rel_pos2.cross(-m_linearJointAxis));69m_0MinvJt = inertiaInvA * m_aJ;70m_1MinvJt = inertiaInvB * m_bJ;71m_Adiag = massInvA + m_0MinvJt.dot(m_aJ) + massInvB + m_1MinvJt.dot(m_bJ);7273ERR_FAIL_COND(m_Adiag <= real_t(0.0));74}7576//angular constraint between two different rigidbodies77GodotJacobianEntry3D(const Vector3 &jointAxis,78const Basis &world2A,79const Basis &world2B,80const Vector3 &inertiaInvA,81const Vector3 &inertiaInvB) :82m_linearJointAxis(Vector3(real_t(0.), real_t(0.), real_t(0.))) {83m_aJ = world2A.xform(jointAxis);84m_bJ = world2B.xform(-jointAxis);85m_0MinvJt = inertiaInvA * m_aJ;86m_1MinvJt = inertiaInvB * m_bJ;87m_Adiag = m_0MinvJt.dot(m_aJ) + m_1MinvJt.dot(m_bJ);8889ERR_FAIL_COND(m_Adiag <= real_t(0.0));90}9192//angular constraint between two different rigidbodies93GodotJacobianEntry3D(const Vector3 &axisInA,94const Vector3 &axisInB,95const Vector3 &inertiaInvA,96const Vector3 &inertiaInvB) :97m_linearJointAxis(Vector3(real_t(0.), real_t(0.), real_t(0.))),98m_aJ(axisInA),99m_bJ(-axisInB) {100m_0MinvJt = inertiaInvA * m_aJ;101m_1MinvJt = inertiaInvB * m_bJ;102m_Adiag = m_0MinvJt.dot(m_aJ) + m_1MinvJt.dot(m_bJ);103104ERR_FAIL_COND(m_Adiag <= real_t(0.0));105}106107//constraint on one rigidbody108GodotJacobianEntry3D(109const Basis &world2A,110const Vector3 &rel_pos1, const Vector3 &rel_pos2,111const Vector3 &jointAxis,112const Vector3 &inertiaInvA,113const real_t massInvA) :114m_linearJointAxis(jointAxis) {115m_aJ = world2A.xform(rel_pos1.cross(jointAxis));116m_bJ = world2A.xform(rel_pos2.cross(-jointAxis));117m_0MinvJt = inertiaInvA * m_aJ;118m_1MinvJt = Vector3(real_t(0.), real_t(0.), real_t(0.));119m_Adiag = massInvA + m_0MinvJt.dot(m_aJ);120121ERR_FAIL_COND(m_Adiag <= real_t(0.0));122}123124real_t getDiagonal() const { return m_Adiag; }125126// for two constraints on the same rigidbody (for example vehicle friction)127real_t getNonDiagonal(const GodotJacobianEntry3D &jacB, const real_t massInvA) const {128const GodotJacobianEntry3D &jacA = *this;129real_t lin = massInvA * jacA.m_linearJointAxis.dot(jacB.m_linearJointAxis);130real_t ang = jacA.m_0MinvJt.dot(jacB.m_aJ);131return lin + ang;132}133134// for two constraints on sharing two same rigidbodies (for example two contact points between two rigidbodies)135real_t getNonDiagonal(const GodotJacobianEntry3D &jacB, const real_t massInvA, const real_t massInvB) const {136const GodotJacobianEntry3D &jacA = *this;137Vector3 lin = jacA.m_linearJointAxis * jacB.m_linearJointAxis;138Vector3 ang0 = jacA.m_0MinvJt * jacB.m_aJ;139Vector3 ang1 = jacA.m_1MinvJt * jacB.m_bJ;140Vector3 lin0 = massInvA * lin;141Vector3 lin1 = massInvB * lin;142Vector3 sum = ang0 + ang1 + lin0 + lin1;143return sum[0] + sum[1] + sum[2];144}145146real_t getRelativeVelocity(const Vector3 &linvelA, const Vector3 &angvelA, const Vector3 &linvelB, const Vector3 &angvelB) {147Vector3 linrel = linvelA - linvelB;148Vector3 angvela = angvelA * m_aJ;149Vector3 angvelb = angvelB * m_bJ;150linrel *= m_linearJointAxis;151angvela += angvelb;152angvela += linrel;153real_t rel_vel2 = angvela[0] + angvela[1] + angvela[2];154return rel_vel2 + CMP_EPSILON;155}156//private:157158Vector3 m_linearJointAxis;159Vector3 m_aJ;160Vector3 m_bJ;161Vector3 m_0MinvJt;162Vector3 m_1MinvJt;163//Optimization: can be stored in the w/last component of one of the vectors164real_t m_Adiag = 1.0;165};166167168