Debian Codegraph @

modules/godot_physics_3d/joints/godot_generic_6dof_joint_3d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* godot_generic_6dof_joint_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 #include "../godot_joint_3d.h"
38 #include "godot_jacobian_entry_3d.h"
39
40 /*
41 Bullet Continuous Collision Detection and Physics Library
42 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
43
44 This software is provided 'as-is', without any express or implied warranty.
45 In no event will the authors be held liable for any damages arising from the use of this software.
46 Permission is granted to anyone to use this software for any purpose,
47 including commercial applications, and to alter it and redistribute it freely,
48 subject to the following restrictions:
49
50 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.
51 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
52 3. This notice may not be removed or altered from any source distribution.
53 */
54
55 /*
56 2007-09-09
57 GodotGeneric6DOFJoint3D Refactored by Francisco Le?n
58 email: [email protected]
59 http://gimpact.sf.net
60 */
61
62 //! Rotation Limit structure for generic joints
63 class GodotG6DOFRotationalLimitMotor3D {
64 public:
65 //! limit_parameters
66 //!@{
67 real_t m_loLimit = -1e30; //!< joint limit
68 real_t m_hiLimit = 1e30; //!< joint limit
69 real_t m_targetVelocity = 0.0; //!< target motor velocity
70 real_t m_maxMotorForce = 0.1; //!< max force on motor
71 real_t m_maxLimitForce = 300.0; //!< max force on limit
72 real_t m_damping = 1.0; //!< Damping.
73 real_t m_limitSoftness = 0.5; //! Relaxation factor
74 real_t m_ERP = 0.5; //!< Error tolerance factor when joint is at limit
75 real_t m_bounce = 0.0; //!< restitution factor
76 bool m_enableMotor = false;
77 bool m_enableLimit = false;
78
79 //!@}
80
81 //! temp_variables
82 //!@{
83 real_t m_currentLimitError = 0.0; //!< How much is violated this limit
84 int m_currentLimit = 0; //!< 0=free, 1=at lo limit, 2=at hi limit
85 real_t m_accumulatedImpulse = 0.0;
86 //!@}
87
88 GodotG6DOFRotationalLimitMotor3D() {}
89
90 bool isLimited() {
91 return (m_loLimit < m_hiLimit);
92 }
93
94 // Need apply correction.
95 bool needApplyTorques() {
96 return (m_enableMotor || m_currentLimit != 0);
97 }
98
99 // Calculates m_currentLimit and m_currentLimitError.
100 int testLimitValue(real_t test_value);
101
102 // Apply the correction impulses for two bodies.
103 real_t solveAngularLimits(real_t timeStep, Vector3 &axis, real_t jacDiagABInv, GodotBody3D *body0, GodotBody3D *body1, bool p_body0_dynamic, bool p_body1_dynamic);
104 };
105
106 class GodotG6DOFTranslationalLimitMotor3D {
107 public:
108 Vector3 m_lowerLimit = Vector3(0.0, 0.0, 0.0); //!< the constraint lower limits
109 Vector3 m_upperLimit = Vector3(0.0, 0.0, 0.0); //!< the constraint upper limits
110 Vector3 m_accumulatedImpulse = Vector3(0.0, 0.0, 0.0);
111 //! Linear_Limit_parameters
112 //!@{
113 Vector3 m_limitSoftness = Vector3(0.7, 0.7, 0.7); //!< Softness for linear limit
114 Vector3 m_damping = Vector3(1.0, 1.0, 1.0); //!< Damping for linear limit
115 Vector3 m_restitution = Vector3(0.5, 0.5, 0.5); //! Bounce parameter for linear limit
116 //!@}
117 bool enable_limit[3] = { true, true, true };
118
119 //! Test limit
120 /*!
121 * - free means upper < lower,
122 * - locked means upper == lower
123 * - limited means upper > lower
124 * - limitIndex: first 3 are linear, next 3 are angular
125 */
126 inline bool isLimited(int limitIndex) {
127 return (m_upperLimit[limitIndex] >= m_lowerLimit[limitIndex]);
128 }
129
130 real_t solveLinearAxis(
131 real_t timeStep,
132 real_t jacDiagABInv,
133 GodotBody3D *body1, const Vector3 &pointInA,
134 GodotBody3D *body2, const Vector3 &pointInB,
135 bool p_body1_dynamic, bool p_body2_dynamic,
136 int limit_index,
137 const Vector3 &axis_normal_on_a,
138 const Vector3 &anchorPos);
139 };
140
141 class GodotGeneric6DOFJoint3D : public GodotJoint3D {
142 protected:
143 union {
144 struct {
145 GodotBody3D *A;
146 GodotBody3D *B;
147 };
148
149 GodotBody3D *_arr[2] = { nullptr, nullptr };
150 };
151
152 //! relative_frames
153 //!@{
154 Transform3D m_frameInA; //!< the constraint space w.r.t body A
155 Transform3D m_frameInB; //!< the constraint space w.r.t body B
156 //!@}
157
158 //! Jacobians
159 //!@{
160 GodotJacobianEntry3D m_jacLinear[3]; //!< 3 orthogonal linear constraints
161 GodotJacobianEntry3D m_jacAng[3]; //!< 3 orthogonal angular constraints
162 //!@}
163
164 //! Linear_Limit_parameters
165 //!@{
166 GodotG6DOFTranslationalLimitMotor3D m_linearLimits;
167 //!@}
168
169 //! hinge_parameters
170 //!@{
171 GodotG6DOFRotationalLimitMotor3D m_angularLimits[3];
172 //!@}
173
174 protected:
175 //! temporal variables
176 //!@{
177 real_t m_timeStep = 0.0;
178 Transform3D m_calculatedTransformA;
179 Transform3D m_calculatedTransformB;
180 Vector3 m_calculatedAxisAngleDiff;
181 Vector3 m_calculatedAxis[3];
182
183 Vector3 m_AnchorPos; // point between pivots of bodies A and B to solve linear axes
184
185 bool m_useLinearReferenceFrameA = false;
186
187 //!@}
188
189 GodotGeneric6DOFJoint3D(GodotGeneric6DOFJoint3D const &) = delete;
190 void operator=(GodotGeneric6DOFJoint3D const &) = delete;
191
192 void buildLinearJacobian(
193 GodotJacobianEntry3D &jacLinear, const Vector3 &normalWorld,
194 const Vector3 &pivotAInW, const Vector3 &pivotBInW);
195
196 void buildAngularJacobian(GodotJacobianEntry3D &jacAngular, const Vector3 &jointAxisW);
197
198 //! calcs the euler angles between the two bodies.
199 void calculateAngleInfo();
200
201 public:
202 GodotGeneric6DOFJoint3D(GodotBody3D *rbA, GodotBody3D *rbB, const Transform3D &frameInA, const Transform3D &frameInB, bool useLinearReferenceFrameA);
203
204 virtual PhysicsServer3D::JointType get_type() const override { return PhysicsServer3D::JOINT_TYPE_6DOF; }
205
206 virtual bool setup(real_t p_step) override;
207 virtual void solve(real_t p_step) override;
208
209 // Calcs the global transform for the joint offset for body A an B, and also calcs the angle differences between the bodies.
210 void calculateTransforms();
211
212 // Gets the global transform of the offset for body A.
213 const Transform3D &getCalculatedTransformA() const {
214 return m_calculatedTransformA;
215 }
216
217 // Gets the global transform of the offset for body B.
218 const Transform3D &getCalculatedTransformB() const {
219 return m_calculatedTransformB;
220 }
221
222 const Transform3D &getFrameOffsetA() const {
223 return m_frameInA;
224 }
225
226 const Transform3D &getFrameOffsetB() const {
227 return m_frameInB;
228 }
229
230 Transform3D &getFrameOffsetA() {
231 return m_frameInA;
232 }
233
234 Transform3D &getFrameOffsetB() {
235 return m_frameInB;
236 }
237
238 // Performs Jacobian calculation, and also calculates angle differences and axis.
239 void updateRHS(real_t timeStep);
240
241 // Get the rotation axis in global coordinates.
242 Vector3 getAxis(int axis_index) const;
243
244 // Get the relative Euler angle.
245 real_t getAngle(int axis_index) const;
246
247 // Calculates angular correction and returns true if limit needs to be corrected.
248 bool testAngularLimitMotor(int axis_index);
249
250 void setLinearLowerLimit(const Vector3 &linearLower) {
251 m_linearLimits.m_lowerLimit = linearLower;
252 }
253
254 void setLinearUpperLimit(const Vector3 &linearUpper) {
255 m_linearLimits.m_upperLimit = linearUpper;
256 }
257
258 void setAngularLowerLimit(const Vector3 &angularLower) {
259 m_angularLimits[0].m_loLimit = angularLower.x;
260 m_angularLimits[1].m_loLimit = angularLower.y;
261 m_angularLimits[2].m_loLimit = angularLower.z;
262 }
263
264 void setAngularUpperLimit(const Vector3 &angularUpper) {
265 m_angularLimits[0].m_hiLimit = angularUpper.x;
266 m_angularLimits[1].m_hiLimit = angularUpper.y;
267 m_angularLimits[2].m_hiLimit = angularUpper.z;
268 }
269
270 // Retrieves the angular limit information.
271 GodotG6DOFRotationalLimitMotor3D *getRotationalLimitMotor(int index) {
272 return &m_angularLimits[index];
273 }
274
275 // Retrieves the limit information.
276 GodotG6DOFTranslationalLimitMotor3D *getTranslationalLimitMotor() {
277 return &m_linearLimits;
278 }
279
280 // First 3 are linear, next 3 are angular.
281 void setLimit(int axis, real_t lo, real_t hi) {
282 if (axis < 3) {
283 m_linearLimits.m_lowerLimit[axis] = lo;
284 m_linearLimits.m_upperLimit[axis] = hi;
285 } else {
286 m_angularLimits[axis - 3].m_loLimit = lo;
287 m_angularLimits[axis - 3].m_hiLimit = hi;
288 }
289 }
290
291 //! Test limit
292 /*!
293 * - free means upper < lower,
294 * - locked means upper == lower
295 * - limited means upper > lower
296 * - limitIndex: first 3 are linear, next 3 are angular
297 */
298 bool isLimited(int limitIndex) {
299 if (limitIndex < 3) {
300 return m_linearLimits.isLimited(limitIndex);
301 }
302 return m_angularLimits[limitIndex - 3].isLimited();
303 }
304
305 const GodotBody3D *getRigidBodyA() const {
306 return A;
307 }
308 const GodotBody3D *getRigidBodyB() const {
309 return B;
310 }
311
312 virtual void calcAnchorPos(); // overridable
313
314 void set_param(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisParam p_param, real_t p_value);
315 real_t get_param(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisParam p_param) const;
316
317 void set_flag(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisFlag p_flag, bool p_value);
318 bool get_flag(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisFlag p_flag) const;
319 };