1 | /**************************************************************************/ |
2 | /* godot_joints_2d.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_JOINTS_2D_H |
32 | #define GODOT_JOINTS_2D_H |
33 | |
34 | #include "godot_body_2d.h" |
35 | #include "godot_constraint_2d.h" |
36 | |
37 | class GodotJoint2D : public GodotConstraint2D { |
38 | real_t bias = 0; |
39 | real_t max_bias = 3.40282e+38; |
40 | real_t max_force = 3.40282e+38; |
41 | |
42 | protected: |
43 | bool dynamic_A = false; |
44 | bool dynamic_B = false; |
45 | |
46 | public: |
47 | _FORCE_INLINE_ void set_max_force(real_t p_force) { max_force = p_force; } |
48 | _FORCE_INLINE_ real_t get_max_force() const { return max_force; } |
49 | |
50 | _FORCE_INLINE_ void set_bias(real_t p_bias) { bias = p_bias; } |
51 | _FORCE_INLINE_ real_t get_bias() const { return bias; } |
52 | |
53 | _FORCE_INLINE_ void set_max_bias(real_t p_bias) { max_bias = p_bias; } |
54 | _FORCE_INLINE_ real_t get_max_bias() const { return max_bias; } |
55 | |
56 | virtual bool setup(real_t p_step) override { return false; } |
57 | virtual bool pre_solve(real_t p_step) override { return false; } |
58 | virtual void solve(real_t p_step) override {} |
59 | |
60 | void copy_settings_from(GodotJoint2D *p_joint); |
61 | |
62 | virtual PhysicsServer2D::JointType get_type() const { return PhysicsServer2D::JOINT_TYPE_MAX; } |
63 | GodotJoint2D(GodotBody2D **p_body_ptr = nullptr, int p_body_count = 0) : |
64 | GodotConstraint2D(p_body_ptr, p_body_count) {} |
65 | |
66 | virtual ~GodotJoint2D() { |
67 | for (int i = 0; i < get_body_count(); i++) { |
68 | GodotBody2D *body = get_body_ptr()[i]; |
69 | if (body) { |
70 | body->remove_constraint(this, i); |
71 | } |
72 | } |
73 | }; |
74 | }; |
75 | |
76 | class GodotPinJoint2D : public GodotJoint2D { |
77 | union { |
78 | struct { |
79 | GodotBody2D *A; |
80 | GodotBody2D *B; |
81 | }; |
82 | |
83 | GodotBody2D *_arr[2] = { nullptr, nullptr }; |
84 | }; |
85 | |
86 | Transform2D M; |
87 | Vector2 rA, rB; |
88 | Vector2 anchor_A; |
89 | Vector2 anchor_B; |
90 | Vector2 bias; |
91 | Vector2 P; |
92 | real_t softness = 0.0; |
93 | |
94 | public: |
95 | virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_PIN; } |
96 | |
97 | virtual bool setup(real_t p_step) override; |
98 | virtual bool pre_solve(real_t p_step) override; |
99 | virtual void solve(real_t p_step) override; |
100 | |
101 | void set_param(PhysicsServer2D::PinJointParam p_param, real_t p_value); |
102 | real_t get_param(PhysicsServer2D::PinJointParam p_param) const; |
103 | |
104 | GodotPinJoint2D(const Vector2 &p_pos, GodotBody2D *p_body_a, GodotBody2D *p_body_b = nullptr); |
105 | }; |
106 | |
107 | class GodotGrooveJoint2D : public GodotJoint2D { |
108 | union { |
109 | struct { |
110 | GodotBody2D *A; |
111 | GodotBody2D *B; |
112 | }; |
113 | |
114 | GodotBody2D *_arr[2] = { nullptr, nullptr }; |
115 | }; |
116 | |
117 | Vector2 A_groove_1; |
118 | Vector2 A_groove_2; |
119 | Vector2 A_groove_normal; |
120 | Vector2 B_anchor; |
121 | Vector2 jn_acc; |
122 | Vector2 gbias; |
123 | real_t jn_max = 0.0; |
124 | real_t clamp = 0.0; |
125 | Vector2 xf_normal; |
126 | Vector2 rA, rB; |
127 | Vector2 k1, k2; |
128 | |
129 | bool correct = false; |
130 | |
131 | public: |
132 | virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_GROOVE; } |
133 | |
134 | virtual bool setup(real_t p_step) override; |
135 | virtual bool pre_solve(real_t p_step) override; |
136 | virtual void solve(real_t p_step) override; |
137 | |
138 | GodotGrooveJoint2D(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, GodotBody2D *p_body_a, GodotBody2D *p_body_b); |
139 | }; |
140 | |
141 | class GodotDampedSpringJoint2D : public GodotJoint2D { |
142 | union { |
143 | struct { |
144 | GodotBody2D *A; |
145 | GodotBody2D *B; |
146 | }; |
147 | |
148 | GodotBody2D *_arr[2] = { nullptr, nullptr }; |
149 | }; |
150 | |
151 | Vector2 anchor_A; |
152 | Vector2 anchor_B; |
153 | |
154 | real_t rest_length = 0.0; |
155 | real_t damping = 1.5; |
156 | real_t stiffness = 20.0; |
157 | |
158 | Vector2 rA, rB; |
159 | Vector2 n; |
160 | Vector2 j; |
161 | real_t n_mass = 0.0; |
162 | real_t target_vrn = 0.0; |
163 | real_t v_coef = 0.0; |
164 | |
165 | public: |
166 | virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_DAMPED_SPRING; } |
167 | |
168 | virtual bool setup(real_t p_step) override; |
169 | virtual bool pre_solve(real_t p_step) override; |
170 | virtual void solve(real_t p_step) override; |
171 | |
172 | void set_param(PhysicsServer2D::DampedSpringParam p_param, real_t p_value); |
173 | real_t get_param(PhysicsServer2D::DampedSpringParam p_param) const; |
174 | |
175 | GodotDampedSpringJoint2D(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, GodotBody2D *p_body_a, GodotBody2D *p_body_b); |
176 | }; |
177 | |
178 | #endif // GODOT_JOINTS_2D_H |
179 | |