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