From daa53c6ef0537d884b529f117fe66998657d34f8 Mon Sep 17 00:00:00 2001 From: Sara Date: Sat, 24 Jun 2023 20:32:31 +0200 Subject: [PATCH] movetowards now also works in cases where x == 0 && y != 0 || x != 0 && y == 0 --- src/corelib/math/vec.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/corelib/math/vec.h b/src/corelib/math/vec.h index 1129da3..082c589 100644 --- a/src/corelib/math/vec.h +++ b/src/corelib/math/vec.h @@ -16,11 +16,11 @@ void clamp_magnitude(float* xx, float* yy, float max_magnitude) { } static inline -void normalize(float* xx, float* yy) { - float x = *xx, y = *yy; +void normalize(float x, float y, float* xx, float* yy) { if(x != 0 || y != 0) { const float m = sqrtf(x*x + y*y); - x /= m; y /= m; + *xx = x / m; *yy = y / m; + } else { *xx = x; *yy = y; } } @@ -40,7 +40,7 @@ int move_towards(float* out_x, float* out_y, float x, float y, float tx, float t const float m = sqrtf(diff_x*diff_x + diff_y*diff_y); const float dir_x = diff_x / m * max_delta, dir_y = diff_y / m * max_delta; - if(fabsf(dir_x) < fabsf(diff_x) && fabsf(dir_y) < fabsf(diff_y)) { + if(fabsf(dir_x) < fabsf(diff_x) || fabsf(dir_y) < fabsf(diff_y)) { *out_x = x + dir_x; *out_y = y + dir_y; return 0;