Refactor function calculate_length_and_rotation() from Skeleton2D

This commit is contained in:
thiagola92 2023-08-14 16:43:22 -03:00
parent 3524346689
commit 6a05825b24

View File

@ -433,29 +433,23 @@ PackedStringArray Bone2D::get_configuration_warnings() const {
} }
void Bone2D::calculate_length_and_rotation() { void Bone2D::calculate_length_and_rotation() {
// if there is at least a single child Bone2D node, we can calculate // If there is at least a single child Bone2D node, we can calculate
// the length and direction. We will always just use the first Bone2D for this. // the length and direction. We will always just use the first Bone2D for this.
bool calculated = false;
int child_count = get_child_count(); int child_count = get_child_count();
if (child_count > 0) { Transform2D global_inv = get_global_transform().affine_inverse();
for (int i = 0; i < child_count; i++) {
Bone2D *child = Object::cast_to<Bone2D>(get_child(i)); for (int i = 0; i < child_count; i++) {
if (child) { Bone2D *child = Object::cast_to<Bone2D>(get_child(i));
Vector2 child_local_pos = to_local(child->get_global_position()); if (child) {
length = child_local_pos.length(); Vector2 child_local_pos = global_inv.xform(child->get_global_position());
bone_angle = child_local_pos.normalized().angle(); length = child_local_pos.length();
calculated = true; bone_angle = child_local_pos.angle();
break; return; // Finished!
}
} }
} }
if (calculated) {
return; // Finished! WARN_PRINT("No Bone2D children of node " + get_name() + ". Cannot calculate bone length or angle reliably.\nUsing transform rotation for bone angle.");
} else { bone_angle = get_transform().get_rotation();
WARN_PRINT("No Bone2D children of node " + get_name() + ". Cannot calculate bone length or angle reliably.\nUsing transform rotation for bone angle");
bone_angle = get_transform().get_rotation();
return;
}
} }
void Bone2D::set_autocalculate_length_and_angle(bool p_autocalculate) { void Bone2D::set_autocalculate_length_and_angle(bool p_autocalculate) {