Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/2d/skeleton/skeleton_modification_2d_jiggle.cpp
9904 views
1
/**************************************************************************/
2
/* skeleton_modification_2d_jiggle.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 "skeleton_modification_2d_jiggle.h"
32
33
#include "scene/2d/skeleton_2d.h"
34
#include "scene/resources/world_2d.h"
35
36
bool SkeletonModification2DJiggle::_set(const StringName &p_path, const Variant &p_value) {
37
String path = p_path;
38
39
if (path.begins_with("joint_data/")) {
40
int which = path.get_slicec('/', 1).to_int();
41
String what = path.get_slicec('/', 2);
42
ERR_FAIL_INDEX_V(which, jiggle_data_chain.size(), false);
43
44
if (what == "bone2d_node") {
45
set_jiggle_joint_bone2d_node(which, p_value);
46
} else if (what == "bone_index") {
47
set_jiggle_joint_bone_index(which, p_value);
48
} else if (what == "override_defaults") {
49
set_jiggle_joint_override(which, p_value);
50
} else if (what == "stiffness") {
51
set_jiggle_joint_stiffness(which, p_value);
52
} else if (what == "mass") {
53
set_jiggle_joint_mass(which, p_value);
54
} else if (what == "damping") {
55
set_jiggle_joint_damping(which, p_value);
56
} else if (what == "use_gravity") {
57
set_jiggle_joint_use_gravity(which, p_value);
58
} else if (what == "gravity") {
59
set_jiggle_joint_gravity(which, p_value);
60
} else {
61
return false;
62
}
63
} else if (path == "use_colliders") {
64
set_use_colliders(p_value);
65
} else if (path == "collision_mask") {
66
set_collision_mask(p_value);
67
} else {
68
return false;
69
}
70
return true;
71
}
72
73
bool SkeletonModification2DJiggle::_get(const StringName &p_path, Variant &r_ret) const {
74
String path = p_path;
75
76
if (path.begins_with("joint_data/")) {
77
int which = path.get_slicec('/', 1).to_int();
78
String what = path.get_slicec('/', 2);
79
ERR_FAIL_INDEX_V(which, jiggle_data_chain.size(), false);
80
81
if (what == "bone2d_node") {
82
r_ret = get_jiggle_joint_bone2d_node(which);
83
} else if (what == "bone_index") {
84
r_ret = get_jiggle_joint_bone_index(which);
85
} else if (what == "override_defaults") {
86
r_ret = get_jiggle_joint_override(which);
87
} else if (what == "stiffness") {
88
r_ret = get_jiggle_joint_stiffness(which);
89
} else if (what == "mass") {
90
r_ret = get_jiggle_joint_mass(which);
91
} else if (what == "damping") {
92
r_ret = get_jiggle_joint_damping(which);
93
} else if (what == "use_gravity") {
94
r_ret = get_jiggle_joint_use_gravity(which);
95
} else if (what == "gravity") {
96
r_ret = get_jiggle_joint_gravity(which);
97
} else {
98
return false;
99
}
100
} else if (path == "use_colliders") {
101
r_ret = get_use_colliders();
102
} else if (path == "collision_mask") {
103
r_ret = get_collision_mask();
104
} else {
105
return false;
106
}
107
return true;
108
}
109
110
void SkeletonModification2DJiggle::_get_property_list(List<PropertyInfo> *p_list) const {
111
p_list->push_back(PropertyInfo(Variant::BOOL, "use_colliders", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
112
if (use_colliders) {
113
p_list->push_back(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS, "", PROPERTY_USAGE_DEFAULT));
114
}
115
116
for (int i = 0; i < jiggle_data_chain.size(); i++) {
117
String base_string = "joint_data/" + itos(i) + "/";
118
119
p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
120
p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D", PROPERTY_USAGE_DEFAULT));
121
p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "override_defaults", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
122
123
if (jiggle_data_chain[i].override_defaults) {
124
p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "stiffness", PROPERTY_HINT_RANGE, "0, 1000, 0.01", PROPERTY_USAGE_DEFAULT));
125
p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "mass", PROPERTY_HINT_RANGE, "0, 1000, 0.01", PROPERTY_USAGE_DEFAULT));
126
p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "damping", PROPERTY_HINT_RANGE, "0, 1, 0.01", PROPERTY_USAGE_DEFAULT));
127
p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "use_gravity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
128
if (jiggle_data_chain[i].use_gravity) {
129
p_list->push_back(PropertyInfo(Variant::VECTOR2, base_string + "gravity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
130
}
131
}
132
}
133
}
134
135
void SkeletonModification2DJiggle::_execute(float p_delta) {
136
ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
137
"Modification is not setup and therefore cannot execute!");
138
if (!enabled) {
139
return;
140
}
141
if (target_node_cache.is_null()) {
142
WARN_PRINT_ONCE("Target cache is out of date. Attempting to update...");
143
update_target_cache();
144
return;
145
}
146
Node2D *target = ObjectDB::get_instance<Node2D>(target_node_cache);
147
if (!target || !target->is_inside_tree()) {
148
ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!");
149
return;
150
}
151
152
for (int i = 0; i < jiggle_data_chain.size(); i++) {
153
_execute_jiggle_joint(i, target, p_delta);
154
}
155
}
156
157
void SkeletonModification2DJiggle::_execute_jiggle_joint(int p_joint_idx, Node2D *p_target, float p_delta) {
158
// Adopted from: https://wiki.unity3d.com/index.php/JiggleBone
159
// With modifications by TwistedTwigleg.
160
161
if (jiggle_data_chain[p_joint_idx].bone_idx <= -1 || jiggle_data_chain[p_joint_idx].bone_idx > stack->skeleton->get_bone_count()) {
162
ERR_PRINT_ONCE("Jiggle joint " + itos(p_joint_idx) + " bone index is invalid. Cannot execute modification on joint...");
163
return;
164
}
165
166
if (jiggle_data_chain[p_joint_idx].bone2d_node_cache.is_null() && !jiggle_data_chain[p_joint_idx].bone2d_node.is_empty()) {
167
WARN_PRINT_ONCE("Bone2D cache for joint " + itos(p_joint_idx) + " is out of date. Updating...");
168
jiggle_joint_update_bone2d_cache(p_joint_idx);
169
}
170
171
Bone2D *operation_bone = stack->skeleton->get_bone(jiggle_data_chain[p_joint_idx].bone_idx);
172
if (!operation_bone) {
173
ERR_PRINT_ONCE("Jiggle joint " + itos(p_joint_idx) + " does not have a Bone2D node or it cannot be found!");
174
return;
175
}
176
177
Transform2D operation_bone_trans = operation_bone->get_global_transform();
178
Vector2 target_position = p_target->get_global_position();
179
180
jiggle_data_chain.write[p_joint_idx].force = (target_position - jiggle_data_chain[p_joint_idx].dynamic_position) * jiggle_data_chain[p_joint_idx].stiffness * p_delta;
181
182
if (jiggle_data_chain[p_joint_idx].use_gravity) {
183
jiggle_data_chain.write[p_joint_idx].force += jiggle_data_chain[p_joint_idx].gravity * p_delta;
184
}
185
186
jiggle_data_chain.write[p_joint_idx].acceleration = jiggle_data_chain[p_joint_idx].force / jiggle_data_chain[p_joint_idx].mass;
187
jiggle_data_chain.write[p_joint_idx].velocity += jiggle_data_chain[p_joint_idx].acceleration * (1 - jiggle_data_chain[p_joint_idx].damping);
188
189
jiggle_data_chain.write[p_joint_idx].dynamic_position += jiggle_data_chain[p_joint_idx].velocity + jiggle_data_chain[p_joint_idx].force;
190
jiggle_data_chain.write[p_joint_idx].dynamic_position += operation_bone_trans.get_origin() - jiggle_data_chain[p_joint_idx].last_position;
191
jiggle_data_chain.write[p_joint_idx].last_position = operation_bone_trans.get_origin();
192
193
// Collision detection/response
194
if (use_colliders) {
195
if (execution_mode == SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_physics_process) {
196
Ref<World2D> world_2d = stack->skeleton->get_world_2d();
197
ERR_FAIL_COND(world_2d.is_null());
198
PhysicsDirectSpaceState2D *space_state = PhysicsServer2D::get_singleton()->space_get_direct_state(world_2d->get_space());
199
PhysicsDirectSpaceState2D::RayResult ray_result;
200
201
PhysicsDirectSpaceState2D::RayParameters ray_params;
202
ray_params.from = operation_bone_trans.get_origin();
203
ray_params.to = jiggle_data_chain[p_joint_idx].dynamic_position;
204
ray_params.collision_mask = collision_mask;
205
206
// Add exception support?
207
bool ray_hit = space_state->intersect_ray(ray_params, ray_result);
208
209
if (ray_hit) {
210
jiggle_data_chain.write[p_joint_idx].dynamic_position = jiggle_data_chain[p_joint_idx].last_noncollision_position;
211
jiggle_data_chain.write[p_joint_idx].acceleration = Vector2(0, 0);
212
jiggle_data_chain.write[p_joint_idx].velocity = Vector2(0, 0);
213
} else {
214
jiggle_data_chain.write[p_joint_idx].last_noncollision_position = jiggle_data_chain[p_joint_idx].dynamic_position;
215
}
216
} else {
217
WARN_PRINT_ONCE("Jiggle 2D modifier: You cannot detect colliders without the stack mode being set to _physics_process!");
218
}
219
}
220
221
// Rotate the bone using the dynamic position!
222
operation_bone_trans = operation_bone_trans.looking_at(jiggle_data_chain[p_joint_idx].dynamic_position);
223
operation_bone_trans.set_rotation(operation_bone_trans.get_rotation() - operation_bone->get_bone_angle());
224
225
// Reset scale
226
operation_bone_trans.set_scale(operation_bone->get_global_scale());
227
228
operation_bone->set_global_transform(operation_bone_trans);
229
stack->skeleton->set_bone_local_pose_override(jiggle_data_chain[p_joint_idx].bone_idx, operation_bone->get_transform(), stack->strength, true);
230
}
231
232
void SkeletonModification2DJiggle::_update_jiggle_joint_data() {
233
for (int i = 0; i < jiggle_data_chain.size(); i++) {
234
if (!jiggle_data_chain[i].override_defaults) {
235
set_jiggle_joint_stiffness(i, stiffness);
236
set_jiggle_joint_mass(i, mass);
237
set_jiggle_joint_damping(i, damping);
238
set_jiggle_joint_use_gravity(i, use_gravity);
239
set_jiggle_joint_gravity(i, gravity);
240
}
241
}
242
}
243
244
void SkeletonModification2DJiggle::_setup_modification(SkeletonModificationStack2D *p_stack) {
245
stack = p_stack;
246
247
if (stack) {
248
is_setup = true;
249
250
if (stack->skeleton) {
251
for (int i = 0; i < jiggle_data_chain.size(); i++) {
252
int bone_idx = jiggle_data_chain[i].bone_idx;
253
if (bone_idx > 0 && bone_idx < stack->skeleton->get_bone_count()) {
254
Bone2D *bone2d_node = stack->skeleton->get_bone(bone_idx);
255
jiggle_data_chain.write[i].dynamic_position = bone2d_node->get_global_position();
256
}
257
258
jiggle_joint_update_bone2d_cache(i);
259
}
260
}
261
262
update_target_cache();
263
}
264
}
265
266
void SkeletonModification2DJiggle::update_target_cache() {
267
if (!is_setup || !stack) {
268
if (is_setup) {
269
ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");
270
}
271
return;
272
}
273
274
target_node_cache = ObjectID();
275
if (stack->skeleton) {
276
if (stack->skeleton->is_inside_tree()) {
277
if (stack->skeleton->has_node(target_node)) {
278
Node *node = stack->skeleton->get_node(target_node);
279
ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
280
"Cannot update target cache: node is this modification's skeleton or cannot be found!");
281
ERR_FAIL_COND_MSG(!node->is_inside_tree(),
282
"Cannot update target cache: node is not in scene tree!");
283
target_node_cache = node->get_instance_id();
284
}
285
}
286
}
287
}
288
289
void SkeletonModification2DJiggle::jiggle_joint_update_bone2d_cache(int p_joint_idx) {
290
ERR_FAIL_INDEX_MSG(p_joint_idx, jiggle_data_chain.size(), "Cannot update bone2d cache: joint index out of range!");
291
if (!is_setup || !stack) {
292
if (is_setup) {
293
ERR_PRINT_ONCE("Cannot update Jiggle " + itos(p_joint_idx) + " Bone2D cache: modification is not properly setup!");
294
}
295
return;
296
}
297
298
jiggle_data_chain.write[p_joint_idx].bone2d_node_cache = ObjectID();
299
if (stack->skeleton) {
300
if (stack->skeleton->is_inside_tree()) {
301
if (stack->skeleton->has_node(jiggle_data_chain[p_joint_idx].bone2d_node)) {
302
Node *node = stack->skeleton->get_node(jiggle_data_chain[p_joint_idx].bone2d_node);
303
ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
304
"Cannot update Jiggle joint " + itos(p_joint_idx) + " Bone2D cache: node is this modification's skeleton or cannot be found!");
305
ERR_FAIL_COND_MSG(!node->is_inside_tree(),
306
"Cannot update Jiggle joint " + itos(p_joint_idx) + " Bone2D cache: node is not in scene tree!");
307
jiggle_data_chain.write[p_joint_idx].bone2d_node_cache = node->get_instance_id();
308
309
Bone2D *bone = Object::cast_to<Bone2D>(node);
310
if (bone) {
311
jiggle_data_chain.write[p_joint_idx].bone_idx = bone->get_index_in_skeleton();
312
} else {
313
ERR_FAIL_MSG("Jiggle joint " + itos(p_joint_idx) + " Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");
314
}
315
}
316
}
317
}
318
}
319
320
void SkeletonModification2DJiggle::set_target_node(const NodePath &p_target_node) {
321
target_node = p_target_node;
322
update_target_cache();
323
}
324
325
NodePath SkeletonModification2DJiggle::get_target_node() const {
326
return target_node;
327
}
328
329
void SkeletonModification2DJiggle::set_stiffness(float p_stiffness) {
330
ERR_FAIL_COND_MSG(p_stiffness < 0, "Stiffness cannot be set to a negative value!");
331
stiffness = p_stiffness;
332
_update_jiggle_joint_data();
333
}
334
335
float SkeletonModification2DJiggle::get_stiffness() const {
336
return stiffness;
337
}
338
339
void SkeletonModification2DJiggle::set_mass(float p_mass) {
340
ERR_FAIL_COND_MSG(p_mass < 0, "Mass cannot be set to a negative value!");
341
mass = p_mass;
342
_update_jiggle_joint_data();
343
}
344
345
float SkeletonModification2DJiggle::get_mass() const {
346
return mass;
347
}
348
349
void SkeletonModification2DJiggle::set_damping(float p_damping) {
350
ERR_FAIL_COND_MSG(p_damping < 0, "Damping cannot be set to a negative value!");
351
ERR_FAIL_COND_MSG(p_damping > 1, "Damping cannot be more than one!");
352
damping = p_damping;
353
_update_jiggle_joint_data();
354
}
355
356
float SkeletonModification2DJiggle::get_damping() const {
357
return damping;
358
}
359
360
void SkeletonModification2DJiggle::set_use_gravity(bool p_use_gravity) {
361
use_gravity = p_use_gravity;
362
_update_jiggle_joint_data();
363
}
364
365
bool SkeletonModification2DJiggle::get_use_gravity() const {
366
return use_gravity;
367
}
368
369
void SkeletonModification2DJiggle::set_gravity(Vector2 p_gravity) {
370
gravity = p_gravity;
371
_update_jiggle_joint_data();
372
}
373
374
Vector2 SkeletonModification2DJiggle::get_gravity() const {
375
return gravity;
376
}
377
378
void SkeletonModification2DJiggle::set_use_colliders(bool p_use_colliders) {
379
use_colliders = p_use_colliders;
380
notify_property_list_changed();
381
}
382
383
bool SkeletonModification2DJiggle::get_use_colliders() const {
384
return use_colliders;
385
}
386
387
void SkeletonModification2DJiggle::set_collision_mask(int p_mask) {
388
collision_mask = p_mask;
389
}
390
391
int SkeletonModification2DJiggle::get_collision_mask() const {
392
return collision_mask;
393
}
394
395
// Jiggle joint data functions
396
int SkeletonModification2DJiggle::get_jiggle_data_chain_length() {
397
return jiggle_data_chain.size();
398
}
399
400
void SkeletonModification2DJiggle::set_jiggle_data_chain_length(int p_length) {
401
ERR_FAIL_COND(p_length < 0);
402
jiggle_data_chain.resize(p_length);
403
notify_property_list_changed();
404
}
405
406
void SkeletonModification2DJiggle::set_jiggle_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node) {
407
ERR_FAIL_INDEX_MSG(p_joint_idx, jiggle_data_chain.size(), "Jiggle joint out of range!");
408
jiggle_data_chain.write[p_joint_idx].bone2d_node = p_target_node;
409
jiggle_joint_update_bone2d_cache(p_joint_idx);
410
411
notify_property_list_changed();
412
}
413
414
NodePath SkeletonModification2DJiggle::get_jiggle_joint_bone2d_node(int p_joint_idx) const {
415
ERR_FAIL_INDEX_V_MSG(p_joint_idx, jiggle_data_chain.size(), NodePath(), "Jiggle joint out of range!");
416
return jiggle_data_chain[p_joint_idx].bone2d_node;
417
}
418
419
void SkeletonModification2DJiggle::set_jiggle_joint_bone_index(int p_joint_idx, int p_bone_idx) {
420
ERR_FAIL_INDEX_MSG(p_joint_idx, jiggle_data_chain.size(), "Jiggle joint out of range!");
421
ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
422
423
if (is_setup) {
424
if (stack->skeleton) {
425
ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");
426
jiggle_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
427
jiggle_data_chain.write[p_joint_idx].bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();
428
jiggle_data_chain.write[p_joint_idx].bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));
429
} else {
430
WARN_PRINT("Cannot verify the Jiggle joint " + itos(p_joint_idx) + " bone index for this modification...");
431
jiggle_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
432
}
433
} else {
434
jiggle_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
435
}
436
437
notify_property_list_changed();
438
}
439
440
int SkeletonModification2DJiggle::get_jiggle_joint_bone_index(int p_joint_idx) const {
441
ERR_FAIL_INDEX_V_MSG(p_joint_idx, jiggle_data_chain.size(), -1, "Jiggle joint out of range!");
442
return jiggle_data_chain[p_joint_idx].bone_idx;
443
}
444
445
void SkeletonModification2DJiggle::set_jiggle_joint_override(int p_joint_idx, bool p_override) {
446
ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
447
jiggle_data_chain.write[p_joint_idx].override_defaults = p_override;
448
_update_jiggle_joint_data();
449
notify_property_list_changed();
450
}
451
452
bool SkeletonModification2DJiggle::get_jiggle_joint_override(int p_joint_idx) const {
453
ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), false);
454
return jiggle_data_chain[p_joint_idx].override_defaults;
455
}
456
457
void SkeletonModification2DJiggle::set_jiggle_joint_stiffness(int p_joint_idx, float p_stiffness) {
458
ERR_FAIL_COND_MSG(p_stiffness < 0, "Stiffness cannot be set to a negative value!");
459
ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
460
jiggle_data_chain.write[p_joint_idx].stiffness = p_stiffness;
461
}
462
463
float SkeletonModification2DJiggle::get_jiggle_joint_stiffness(int p_joint_idx) const {
464
ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), -1);
465
return jiggle_data_chain[p_joint_idx].stiffness;
466
}
467
468
void SkeletonModification2DJiggle::set_jiggle_joint_mass(int p_joint_idx, float p_mass) {
469
ERR_FAIL_COND_MSG(p_mass < 0, "Mass cannot be set to a negative value!");
470
ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
471
jiggle_data_chain.write[p_joint_idx].mass = p_mass;
472
}
473
474
float SkeletonModification2DJiggle::get_jiggle_joint_mass(int p_joint_idx) const {
475
ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), -1);
476
return jiggle_data_chain[p_joint_idx].mass;
477
}
478
479
void SkeletonModification2DJiggle::set_jiggle_joint_damping(int p_joint_idx, float p_damping) {
480
ERR_FAIL_COND_MSG(p_damping < 0, "Damping cannot be set to a negative value!");
481
ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
482
jiggle_data_chain.write[p_joint_idx].damping = p_damping;
483
}
484
485
float SkeletonModification2DJiggle::get_jiggle_joint_damping(int p_joint_idx) const {
486
ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), -1);
487
return jiggle_data_chain[p_joint_idx].damping;
488
}
489
490
void SkeletonModification2DJiggle::set_jiggle_joint_use_gravity(int p_joint_idx, bool p_use_gravity) {
491
ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
492
jiggle_data_chain.write[p_joint_idx].use_gravity = p_use_gravity;
493
notify_property_list_changed();
494
}
495
496
bool SkeletonModification2DJiggle::get_jiggle_joint_use_gravity(int p_joint_idx) const {
497
ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), false);
498
return jiggle_data_chain[p_joint_idx].use_gravity;
499
}
500
501
void SkeletonModification2DJiggle::set_jiggle_joint_gravity(int p_joint_idx, Vector2 p_gravity) {
502
ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
503
jiggle_data_chain.write[p_joint_idx].gravity = p_gravity;
504
}
505
506
Vector2 SkeletonModification2DJiggle::get_jiggle_joint_gravity(int p_joint_idx) const {
507
ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), Vector2(0, 0));
508
return jiggle_data_chain[p_joint_idx].gravity;
509
}
510
511
void SkeletonModification2DJiggle::_bind_methods() {
512
ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DJiggle::set_target_node);
513
ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DJiggle::get_target_node);
514
515
ClassDB::bind_method(D_METHOD("set_jiggle_data_chain_length", "length"), &SkeletonModification2DJiggle::set_jiggle_data_chain_length);
516
ClassDB::bind_method(D_METHOD("get_jiggle_data_chain_length"), &SkeletonModification2DJiggle::get_jiggle_data_chain_length);
517
518
ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &SkeletonModification2DJiggle::set_stiffness);
519
ClassDB::bind_method(D_METHOD("get_stiffness"), &SkeletonModification2DJiggle::get_stiffness);
520
ClassDB::bind_method(D_METHOD("set_mass", "mass"), &SkeletonModification2DJiggle::set_mass);
521
ClassDB::bind_method(D_METHOD("get_mass"), &SkeletonModification2DJiggle::get_mass);
522
ClassDB::bind_method(D_METHOD("set_damping", "damping"), &SkeletonModification2DJiggle::set_damping);
523
ClassDB::bind_method(D_METHOD("get_damping"), &SkeletonModification2DJiggle::get_damping);
524
ClassDB::bind_method(D_METHOD("set_use_gravity", "use_gravity"), &SkeletonModification2DJiggle::set_use_gravity);
525
ClassDB::bind_method(D_METHOD("get_use_gravity"), &SkeletonModification2DJiggle::get_use_gravity);
526
ClassDB::bind_method(D_METHOD("set_gravity", "gravity"), &SkeletonModification2DJiggle::set_gravity);
527
ClassDB::bind_method(D_METHOD("get_gravity"), &SkeletonModification2DJiggle::get_gravity);
528
529
ClassDB::bind_method(D_METHOD("set_use_colliders", "use_colliders"), &SkeletonModification2DJiggle::set_use_colliders);
530
ClassDB::bind_method(D_METHOD("get_use_colliders"), &SkeletonModification2DJiggle::get_use_colliders);
531
ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &SkeletonModification2DJiggle::set_collision_mask);
532
ClassDB::bind_method(D_METHOD("get_collision_mask"), &SkeletonModification2DJiggle::get_collision_mask);
533
534
// Jiggle joint data functions
535
ClassDB::bind_method(D_METHOD("set_jiggle_joint_bone2d_node", "joint_idx", "bone2d_node"), &SkeletonModification2DJiggle::set_jiggle_joint_bone2d_node);
536
ClassDB::bind_method(D_METHOD("get_jiggle_joint_bone2d_node", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_bone2d_node);
537
ClassDB::bind_method(D_METHOD("set_jiggle_joint_bone_index", "joint_idx", "bone_idx"), &SkeletonModification2DJiggle::set_jiggle_joint_bone_index);
538
ClassDB::bind_method(D_METHOD("get_jiggle_joint_bone_index", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_bone_index);
539
ClassDB::bind_method(D_METHOD("set_jiggle_joint_override", "joint_idx", "override"), &SkeletonModification2DJiggle::set_jiggle_joint_override);
540
ClassDB::bind_method(D_METHOD("get_jiggle_joint_override", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_override);
541
ClassDB::bind_method(D_METHOD("set_jiggle_joint_stiffness", "joint_idx", "stiffness"), &SkeletonModification2DJiggle::set_jiggle_joint_stiffness);
542
ClassDB::bind_method(D_METHOD("get_jiggle_joint_stiffness", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_stiffness);
543
ClassDB::bind_method(D_METHOD("set_jiggle_joint_mass", "joint_idx", "mass"), &SkeletonModification2DJiggle::set_jiggle_joint_mass);
544
ClassDB::bind_method(D_METHOD("get_jiggle_joint_mass", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_mass);
545
ClassDB::bind_method(D_METHOD("set_jiggle_joint_damping", "joint_idx", "damping"), &SkeletonModification2DJiggle::set_jiggle_joint_damping);
546
ClassDB::bind_method(D_METHOD("get_jiggle_joint_damping", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_damping);
547
ClassDB::bind_method(D_METHOD("set_jiggle_joint_use_gravity", "joint_idx", "use_gravity"), &SkeletonModification2DJiggle::set_jiggle_joint_use_gravity);
548
ClassDB::bind_method(D_METHOD("get_jiggle_joint_use_gravity", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_use_gravity);
549
ClassDB::bind_method(D_METHOD("set_jiggle_joint_gravity", "joint_idx", "gravity"), &SkeletonModification2DJiggle::set_jiggle_joint_gravity);
550
ClassDB::bind_method(D_METHOD("get_jiggle_joint_gravity", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_gravity);
551
552
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");
553
ADD_PROPERTY(PropertyInfo(Variant::INT, "jiggle_data_chain_length", PROPERTY_HINT_RANGE, "0,100,1"), "set_jiggle_data_chain_length", "get_jiggle_data_chain_length");
554
ADD_GROUP("Default Joint Settings", "");
555
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stiffness"), "set_stiffness", "get_stiffness");
556
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass"), "set_mass", "get_mass");
557
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0, 1, 0.01"), "set_damping", "get_damping");
558
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_gravity"), "set_use_gravity", "get_use_gravity");
559
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity"), "set_gravity", "get_gravity");
560
ADD_GROUP("", "");
561
}
562
563
SkeletonModification2DJiggle::SkeletonModification2DJiggle() {
564
stack = nullptr;
565
is_setup = false;
566
jiggle_data_chain = Vector<Jiggle_Joint_Data2D>();
567
stiffness = 3;
568
mass = 0.75;
569
damping = 0.75;
570
use_gravity = false;
571
gravity = Vector2(0, 6.0);
572
enabled = true;
573
editor_draw_gizmo = false; // Nothing to really show in a gizmo right now.
574
}
575
576
SkeletonModification2DJiggle::~SkeletonModification2DJiggle() {
577
}
578
579