Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/jolt_physics/joints/jolt_joint_3d.cpp
21152 views
1
/**************************************************************************/
2
/* jolt_joint_3d.cpp */
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
#include "jolt_joint_3d.h"
32
33
#include "../jolt_physics_server_3d.h"
34
#include "../jolt_project_settings.h"
35
#include "../misc/jolt_type_conversions.h"
36
#include "../objects/jolt_body_3d.h"
37
#include "../spaces/jolt_space_3d.h"
38
39
namespace {
40
41
constexpr int JOINT_DEFAULT_SOLVER_PRIORITY = 1;
42
43
} // namespace
44
45
void JoltJoint3D::_shift_reference_frames(const Vector3 &p_linear_shift, const Vector3 &p_angular_shift, Transform3D &r_shifted_ref_a, Transform3D &r_shifted_ref_b) {
46
Vector3 origin_a = local_ref_a.origin;
47
Vector3 origin_b = local_ref_b.origin;
48
49
if (body_a != nullptr) {
50
origin_a *= body_a->get_scale();
51
origin_a -= to_godot(body_a->get_jolt_shape()->GetCenterOfMass());
52
}
53
54
if (body_b != nullptr) {
55
origin_b *= body_b->get_scale();
56
origin_b -= to_godot(body_b->get_jolt_shape()->GetCenterOfMass());
57
}
58
59
const Basis &basis_a = local_ref_a.basis;
60
const Basis &basis_b = local_ref_b.basis;
61
62
const Basis shifted_basis_a = basis_a * Basis::from_euler(p_angular_shift, EulerOrder::ZYX);
63
const Vector3 shifted_origin_a = origin_a - basis_a.xform(p_linear_shift);
64
65
r_shifted_ref_a = Transform3D(shifted_basis_a, shifted_origin_a);
66
r_shifted_ref_b = Transform3D(basis_b, origin_b);
67
}
68
69
void JoltJoint3D::_wake_up_bodies() {
70
if (body_a != nullptr) {
71
body_a->wake_up();
72
}
73
74
if (body_b != nullptr) {
75
body_b->wake_up();
76
}
77
}
78
79
void JoltJoint3D::_update_enabled() {
80
if (jolt_ref != nullptr) {
81
jolt_ref->SetEnabled(enabled);
82
}
83
}
84
85
void JoltJoint3D::_update_iterations() {
86
if (jolt_ref != nullptr) {
87
jolt_ref->SetNumVelocityStepsOverride((JPH::uint)velocity_iterations);
88
jolt_ref->SetNumPositionStepsOverride((JPH::uint)position_iterations);
89
}
90
}
91
92
void JoltJoint3D::_enabled_changed() {
93
_update_enabled();
94
_wake_up_bodies();
95
}
96
97
void JoltJoint3D::_iterations_changed() {
98
_update_iterations();
99
_wake_up_bodies();
100
}
101
102
String JoltJoint3D::_bodies_to_string() const {
103
return vformat("'%s' and '%s'", body_a != nullptr ? body_a->to_string() : "<unknown>", body_b != nullptr ? body_b->to_string() : "<World>");
104
}
105
106
JoltJoint3D::JoltJoint3D(const JoltJoint3D &p_old_joint, JoltBody3D *p_body_a, JoltBody3D *p_body_b, const Transform3D &p_local_ref_a, const Transform3D &p_local_ref_b) :
107
enabled(p_old_joint.enabled),
108
collision_disabled(p_old_joint.collision_disabled),
109
body_a(p_body_a),
110
body_b(p_body_b),
111
rid(p_old_joint.rid),
112
local_ref_a(p_local_ref_a),
113
local_ref_b(p_local_ref_b) {
114
if (body_a != nullptr) {
115
body_a->add_joint(this);
116
}
117
118
if (body_b != nullptr) {
119
body_b->add_joint(this);
120
}
121
122
if (body_b == nullptr && JoltProjectSettings::joint_world_node == JOLT_JOINT_WORLD_NODE_A) {
123
// The joint scene nodes will, when omitting one of the two body nodes, always pass in a
124
// null `body_b` to indicate it being the "world node", regardless of which body node you
125
// leave blank. So we need to correct for that if we wish to use the arguably more intuitive
126
// alternative where `body_a` is the "world node" instead.
127
128
SWAP(body_a, body_b);
129
SWAP(local_ref_a, local_ref_b);
130
}
131
}
132
133
JoltJoint3D::~JoltJoint3D() {
134
if (body_a != nullptr) {
135
body_a->remove_joint(this);
136
}
137
138
if (body_b != nullptr) {
139
body_b->remove_joint(this);
140
}
141
142
destroy();
143
}
144
145
JoltSpace3D *JoltJoint3D::get_space() const {
146
if (body_a != nullptr && body_b != nullptr) {
147
JoltSpace3D *space_a = body_a->get_space();
148
JoltSpace3D *space_b = body_b->get_space();
149
150
if (space_a == nullptr || space_b == nullptr) {
151
return nullptr;
152
}
153
154
ERR_FAIL_COND_V_MSG(space_a != space_b, nullptr, vformat("Joint was found to connect bodies in different physics spaces. This joint will effectively be disabled. This joint connects %s.", _bodies_to_string()));
155
156
return space_a;
157
} else if (body_a != nullptr) {
158
return body_a->get_space();
159
} else if (body_b != nullptr) {
160
return body_b->get_space();
161
}
162
163
return nullptr;
164
}
165
166
void JoltJoint3D::set_enabled(bool p_enabled) {
167
if (enabled == p_enabled) {
168
return;
169
}
170
171
enabled = p_enabled;
172
173
_enabled_changed();
174
}
175
176
int JoltJoint3D::get_solver_priority() const {
177
return JOINT_DEFAULT_SOLVER_PRIORITY;
178
}
179
180
void JoltJoint3D::set_solver_priority(int p_priority) {
181
if (p_priority != JOINT_DEFAULT_SOLVER_PRIORITY) {
182
WARN_PRINT(vformat("Joint solver priority is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
183
}
184
}
185
186
void JoltJoint3D::set_solver_velocity_iterations(int p_iterations) {
187
if (velocity_iterations == p_iterations) {
188
return;
189
}
190
191
velocity_iterations = p_iterations;
192
193
_iterations_changed();
194
}
195
196
void JoltJoint3D::set_solver_position_iterations(int p_iterations) {
197
if (position_iterations == p_iterations) {
198
return;
199
}
200
201
position_iterations = p_iterations;
202
203
_iterations_changed();
204
}
205
206
void JoltJoint3D::set_collision_disabled(bool p_disabled) {
207
collision_disabled = p_disabled;
208
209
if (body_a == nullptr || body_b == nullptr) {
210
return;
211
}
212
213
JoltPhysicsServer3D *physics_server = JoltPhysicsServer3D::get_singleton();
214
215
if (collision_disabled) {
216
physics_server->body_add_collision_exception(body_a->get_rid(), body_b->get_rid());
217
physics_server->body_add_collision_exception(body_b->get_rid(), body_a->get_rid());
218
} else {
219
physics_server->body_remove_collision_exception(body_a->get_rid(), body_b->get_rid());
220
physics_server->body_remove_collision_exception(body_b->get_rid(), body_a->get_rid());
221
}
222
}
223
224
void JoltJoint3D::destroy() {
225
if (jolt_ref == nullptr) {
226
return;
227
}
228
229
JoltSpace3D *space = get_space();
230
231
if (space != nullptr) {
232
space->remove_joint(this);
233
}
234
235
jolt_ref = nullptr;
236
}
237
238