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