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