1/**************************************************************************/
2/* godot_jacobian_entry_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
31#ifndef GODOT_JACOBIAN_ENTRY_3D_H
32#define GODOT_JACOBIAN_ENTRY_3D_H
33
34/*
35Adapted to Godot from the Bullet library.
36*/
37
38/*
39Bullet Continuous Collision Detection and Physics Library
40Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
41
42This software is provided 'as-is', without any express or implied warranty.
43In no event will the authors be held liable for any damages arising from the use of this software.
44Permission is granted to anyone to use this software for any purpose,
45including commercial applications, and to alter it and redistribute it freely,
46subject to the following restrictions:
47
481. 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.
492. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
503. This notice may not be removed or altered from any source distribution.
51*/
52
53#include "core/math/transform_3d.h"
54
55class GodotJacobianEntry3D {
56public:
57 GodotJacobianEntry3D() {}
58 //constraint between two different rigidbodies
59 GodotJacobianEntry3D(
60 const Basis &world2A,
61 const Basis &world2B,
62 const Vector3 &rel_pos1, const Vector3 &rel_pos2,
63 const Vector3 &jointAxis,
64 const Vector3 &inertiaInvA,
65 const real_t massInvA,
66 const Vector3 &inertiaInvB,
67 const real_t massInvB) :
68 m_linearJointAxis(jointAxis) {
69 m_aJ = world2A.xform(rel_pos1.cross(m_linearJointAxis));
70 m_bJ = world2B.xform(rel_pos2.cross(-m_linearJointAxis));
71 m_0MinvJt = inertiaInvA * m_aJ;
72 m_1MinvJt = inertiaInvB * m_bJ;
73 m_Adiag = massInvA + m_0MinvJt.dot(m_aJ) + massInvB + m_1MinvJt.dot(m_bJ);
74
75 ERR_FAIL_COND(m_Adiag <= real_t(0.0));
76 }
77
78 //angular constraint between two different rigidbodies
79 GodotJacobianEntry3D(const Vector3 &jointAxis,
80 const Basis &world2A,
81 const Basis &world2B,
82 const Vector3 &inertiaInvA,
83 const Vector3 &inertiaInvB) :
84 m_linearJointAxis(Vector3(real_t(0.), real_t(0.), real_t(0.))) {
85 m_aJ = world2A.xform(jointAxis);
86 m_bJ = world2B.xform(-jointAxis);
87 m_0MinvJt = inertiaInvA * m_aJ;
88 m_1MinvJt = inertiaInvB * m_bJ;
89 m_Adiag = m_0MinvJt.dot(m_aJ) + m_1MinvJt.dot(m_bJ);
90
91 ERR_FAIL_COND(m_Adiag <= real_t(0.0));
92 }
93
94 //angular constraint between two different rigidbodies
95 GodotJacobianEntry3D(const Vector3 &axisInA,
96 const Vector3 &axisInB,
97 const Vector3 &inertiaInvA,
98 const Vector3 &inertiaInvB) :
99 m_linearJointAxis(Vector3(real_t(0.), real_t(0.), real_t(0.))),
100 m_aJ(axisInA),
101 m_bJ(-axisInB) {
102 m_0MinvJt = inertiaInvA * m_aJ;
103 m_1MinvJt = inertiaInvB * m_bJ;
104 m_Adiag = m_0MinvJt.dot(m_aJ) + m_1MinvJt.dot(m_bJ);
105
106 ERR_FAIL_COND(m_Adiag <= real_t(0.0));
107 }
108
109 //constraint on one rigidbody
110 GodotJacobianEntry3D(
111 const Basis &world2A,
112 const Vector3 &rel_pos1, const Vector3 &rel_pos2,
113 const Vector3 &jointAxis,
114 const Vector3 &inertiaInvA,
115 const real_t massInvA) :
116 m_linearJointAxis(jointAxis) {
117 m_aJ = world2A.xform(rel_pos1.cross(jointAxis));
118 m_bJ = world2A.xform(rel_pos2.cross(-jointAxis));
119 m_0MinvJt = inertiaInvA * m_aJ;
120 m_1MinvJt = Vector3(real_t(0.), real_t(0.), real_t(0.));
121 m_Adiag = massInvA + m_0MinvJt.dot(m_aJ);
122
123 ERR_FAIL_COND(m_Adiag <= real_t(0.0));
124 }
125
126 real_t getDiagonal() const { return m_Adiag; }
127
128 // for two constraints on the same rigidbody (for example vehicle friction)
129 real_t getNonDiagonal(const GodotJacobianEntry3D &jacB, const real_t massInvA) const {
130 const GodotJacobianEntry3D &jacA = *this;
131 real_t lin = massInvA * jacA.m_linearJointAxis.dot(jacB.m_linearJointAxis);
132 real_t ang = jacA.m_0MinvJt.dot(jacB.m_aJ);
133 return lin + ang;
134 }
135
136 // for two constraints on sharing two same rigidbodies (for example two contact points between two rigidbodies)
137 real_t getNonDiagonal(const GodotJacobianEntry3D &jacB, const real_t massInvA, const real_t massInvB) const {
138 const GodotJacobianEntry3D &jacA = *this;
139 Vector3 lin = jacA.m_linearJointAxis * jacB.m_linearJointAxis;
140 Vector3 ang0 = jacA.m_0MinvJt * jacB.m_aJ;
141 Vector3 ang1 = jacA.m_1MinvJt * jacB.m_bJ;
142 Vector3 lin0 = massInvA * lin;
143 Vector3 lin1 = massInvB * lin;
144 Vector3 sum = ang0 + ang1 + lin0 + lin1;
145 return sum[0] + sum[1] + sum[2];
146 }
147
148 real_t getRelativeVelocity(const Vector3 &linvelA, const Vector3 &angvelA, const Vector3 &linvelB, const Vector3 &angvelB) {
149 Vector3 linrel = linvelA - linvelB;
150 Vector3 angvela = angvelA * m_aJ;
151 Vector3 angvelb = angvelB * m_bJ;
152 linrel *= m_linearJointAxis;
153 angvela += angvelb;
154 angvela += linrel;
155 real_t rel_vel2 = angvela[0] + angvela[1] + angvela[2];
156 return rel_vel2 + CMP_EPSILON;
157 }
158 //private:
159
160 Vector3 m_linearJointAxis;
161 Vector3 m_aJ;
162 Vector3 m_bJ;
163 Vector3 m_0MinvJt;
164 Vector3 m_1MinvJt;
165 //Optimization: can be stored in the w/last component of one of the vectors
166 real_t m_Adiag = 1.0;
167};
168
169#endif // GODOT_JACOBIAN_ENTRY_3D_H
170