1/**************************************************************************/
2/* godot_cone_twist_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#ifndef GODOT_CONE_TWIST_JOINT_3D_H
32#define GODOT_CONE_TWIST_JOINT_3D_H
33
34/*
35Adapted to Godot from the Bullet library.
36*/
37
38/*
39Bullet Continuous Collision Detection and Physics Library
40GodotConeTwistJoint3D is Copyright (c) 2007 Starbreeze Studios
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
52Written by: Marcus Hennix
53*/
54
55#include "servers/physics_3d/godot_joint_3d.h"
56#include "servers/physics_3d/joints/godot_jacobian_entry_3d.h"
57
58// GodotConeTwistJoint3D can be used to simulate ragdoll joints (upper arm, leg etc).
59class GodotConeTwistJoint3D : public GodotJoint3D {
60#ifdef IN_PARALLELL_SOLVER
61public:
62#endif
63
64 union {
65 struct {
66 GodotBody3D *A;
67 GodotBody3D *B;
68 };
69
70 GodotBody3D *_arr[2] = { nullptr, nullptr };
71 };
72
73 GodotJacobianEntry3D m_jac[3] = {}; //3 orthogonal linear constraints
74
75 real_t m_appliedImpulse = 0.0;
76 Transform3D m_rbAFrame;
77 Transform3D m_rbBFrame;
78
79 real_t m_limitSoftness = 0.0;
80 real_t m_biasFactor = 0.3;
81 real_t m_relaxationFactor = 1.0;
82
83 real_t m_swingSpan1 = Math_TAU / 8.0;
84 real_t m_swingSpan2 = 0.0;
85 real_t m_twistSpan = 0.0;
86
87 Vector3 m_swingAxis;
88 Vector3 m_twistAxis;
89
90 real_t m_kSwing = 0.0;
91 real_t m_kTwist = 0.0;
92
93 real_t m_twistLimitSign = 0.0;
94 real_t m_swingCorrection = 0.0;
95 real_t m_twistCorrection = 0.0;
96
97 real_t m_accSwingLimitImpulse = 0.0;
98 real_t m_accTwistLimitImpulse = 0.0;
99
100 bool m_angularOnly = false;
101 bool m_solveTwistLimit = false;
102 bool m_solveSwingLimit = false;
103
104public:
105 virtual PhysicsServer3D::JointType get_type() const override { return PhysicsServer3D::JOINT_TYPE_CONE_TWIST; }
106
107 virtual bool setup(real_t p_step) override;
108 virtual void solve(real_t p_step) override;
109
110 GodotConeTwistJoint3D(GodotBody3D *rbA, GodotBody3D *rbB, const Transform3D &rbAFrame, const Transform3D &rbBFrame);
111
112 void setAngularOnly(bool angularOnly) {
113 m_angularOnly = angularOnly;
114 }
115
116 void setLimit(real_t _swingSpan1, real_t _swingSpan2, real_t _twistSpan, real_t _softness = 0.8f, real_t _biasFactor = 0.3f, real_t _relaxationFactor = 1.0f) {
117 m_swingSpan1 = _swingSpan1;
118 m_swingSpan2 = _swingSpan2;
119 m_twistSpan = _twistSpan;
120
121 m_limitSoftness = _softness;
122 m_biasFactor = _biasFactor;
123 m_relaxationFactor = _relaxationFactor;
124 }
125
126 inline int getSolveTwistLimit() {
127 return m_solveTwistLimit;
128 }
129
130 inline int getSolveSwingLimit() {
131 return m_solveTwistLimit;
132 }
133
134 inline real_t getTwistLimitSign() {
135 return m_twistLimitSign;
136 }
137
138 void set_param(PhysicsServer3D::ConeTwistJointParam p_param, real_t p_value);
139 real_t get_param(PhysicsServer3D::ConeTwistJointParam p_param) const;
140};
141
142#endif // GODOT_CONE_TWIST_JOINT_3D_H
143