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