Skip to content
0.2.0-dev Download ZIP
139 lines ·5.3 KB Raw
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#pragma once
32
33/*
34Adapted to Godot from the Bullet library.
35*/
36
37/*
38Bullet Continuous Collision Detection and Physics Library
39GodotConeTwistJoint3D is Copyright (c) 2007 Starbreeze Studios
40
41This software is provided 'as-is', without any express or implied warranty.
42In no event will the authors be held liable for any damages arising from the use of this software.
43Permission is granted to anyone to use this software for any purpose,
44including commercial applications, and to alter it and redistribute it freely,
45subject to the following restrictions:
46
471. 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.
482. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
493. This notice may not be removed or altered from any source distribution.
50
51Written by: Marcus Hennix
52*/
53
54#include "../godot_joint_3d.h"
55#include "godot_jacobian_entry_3d.h"
56
57// GodotConeTwistJoint3D can be used to simulate ragdoll joints (upper arm, leg etc).
58class GodotConeTwistJoint3D : public GodotJoint3D {
59#ifdef IN_PARALLELL_SOLVER
60public:
61#endif
62
63 union {
64 struct {
65 GodotBody3D *A;
66 GodotBody3D *B;
67 };
68
69 GodotBody3D *_arr[2] = { nullptr, nullptr };
70 };
71
72 GodotJacobianEntry3D m_jac[3] = {}; //3 orthogonal linear constraints
73
74 real_t m_appliedImpulse = 0.0;
75 Transform3D m_rbAFrame;
76 Transform3D m_rbBFrame;
77
78 real_t m_limitSoftness = 0.0;
79 real_t m_biasFactor = 0.3;
80 real_t m_relaxationFactor = 1.0;
81
82 real_t m_swingSpan1 = Math::TAU / 8.0;
83 real_t m_swingSpan2 = 0.0;
84 real_t m_twistSpan = 0.0;
85
86 Vector3 m_swingAxis;
87 Vector3 m_twistAxis;
88
89 real_t m_kSwing = 0.0;
90 real_t m_kTwist = 0.0;
91
92 real_t m_twistLimitSign = 0.0;
93 real_t m_swingCorrection = 0.0;
94 real_t m_twistCorrection = 0.0;
95
96 real_t m_accSwingLimitImpulse = 0.0;
97 real_t m_accTwistLimitImpulse = 0.0;
98
99 bool m_angularOnly = false;
100 bool m_solveTwistLimit = false;
101 bool m_solveSwingLimit = false;
102
103public:
104 virtual PS3DE::JointType get_type() const override { return PS3DE::JOINT_TYPE_CONE_TWIST; }
105
106 virtual bool setup(real_t p_step) override;
107 virtual void solve(real_t p_step) override;
108
109 GodotConeTwistJoint3D(GodotBody3D *rbA, GodotBody3D *rbB, const Transform3D &rbAFrame, const Transform3D &rbBFrame);
110
111 void setAngularOnly(bool angularOnly) {
112 m_angularOnly = angularOnly;
113 }
114
115 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) {
116 m_swingSpan1 = _swingSpan1;
117 m_swingSpan2 = _swingSpan2;
118 m_twistSpan = _twistSpan;
119
120 m_limitSoftness = _softness;
121 m_biasFactor = _biasFactor;
122 m_relaxationFactor = _relaxationFactor;
123 }
124
125 inline int getSolveTwistLimit() {
126 return m_solveTwistLimit;
127 }
128
129 inline int getSolveSwingLimit() {
130 return m_solveTwistLimit;
131 }
132
133 inline real_t getTwistLimitSign() {
134 return m_twistLimitSign;
135 }
136
137 void set_param(PS3DE::ConeTwistJointParam p_param, real_t p_value);
138 real_t get_param(PS3DE::ConeTwistJointParam p_param) const;
139};