Refactor function calculate_length_and_rotation()
from Skeleton2D
This commit is contained in:
parent
3524346689
commit
6a05825b24
@ -433,31 +433,25 @@ PackedStringArray Bone2D::get_configuration_warnings() const {
|
||||
}
|
||||
|
||||
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.
|
||||
bool calculated = false;
|
||||
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));
|
||||
if (child) {
|
||||
Vector2 child_local_pos = to_local(child->get_global_position());
|
||||
Vector2 child_local_pos = global_inv.xform(child->get_global_position());
|
||||
length = child_local_pos.length();
|
||||
bone_angle = child_local_pos.normalized().angle();
|
||||
calculated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (calculated) {
|
||||
bone_angle = child_local_pos.angle();
|
||||
return; // Finished!
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void Bone2D::set_autocalculate_length_and_angle(bool p_autocalculate) {
|
||||
autocalculate_length_and_angle = p_autocalculate;
|
||||
if (autocalculate_length_and_angle) {
|
||||
|
Loading…
Reference in New Issue
Block a user