Merge duplicate branch conditions to one.

Use SWAP template for swapping values.
This commit is contained in:
Anilforextra 2021-09-26 21:18:15 +05:45
parent 9293c76636
commit 41e43e0959
2 changed files with 27 additions and 53 deletions

View file

@ -72,37 +72,25 @@ real_t SkeletonModification3D::clamp_angle(real_t p_angle, real_t p_min_bound, r
p_max_bound = Math_TAU + p_max_bound;
}
if (p_min_bound > p_max_bound) {
real_t tmp = p_min_bound;
p_min_bound = p_max_bound;
p_max_bound = tmp;
SWAP(p_min_bound, p_max_bound);
}
bool is_beyond_bounds = (p_angle < p_min_bound || p_angle > p_max_bound);
bool is_within_bounds = (p_angle > p_min_bound && p_angle < p_max_bound);
// Note: May not be the most optimal way to clamp, but it always constraints to the nearest angle.
if (p_invert == false) {
if (p_angle < p_min_bound || p_angle > p_max_bound) {
Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound));
Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound));
Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle));
if ((!p_invert && is_beyond_bounds) || (p_invert && is_within_bounds)) {
Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound));
Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound));
Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle));
if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) {
p_angle = p_min_bound;
} else {
p_angle = p_max_bound;
}
}
} else {
if (p_angle > p_min_bound && p_angle < p_max_bound) {
Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound));
Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound));
Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle));
if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) {
p_angle = p_min_bound;
} else {
p_angle = p_max_bound;
}
if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) {
p_angle = p_min_bound;
} else {
p_angle = p_max_bound;
}
}
return p_angle;
}