movetowards now also works in cases where x == 0 && y != 0 || x != 0 && y == 0

pull/7/head
Sara 2023-06-24 20:32:31 +02:00
parent 3df374b3b9
commit daa53c6ef0
1 changed files with 4 additions and 4 deletions

View File

@ -16,11 +16,11 @@ void clamp_magnitude(float* xx, float* yy, float max_magnitude) {
} }
static inline static inline
void normalize(float* xx, float* yy) { void normalize(float x, float y, float* xx, float* yy) {
float x = *xx, y = *yy;
if(x != 0 || y != 0) { if(x != 0 || y != 0) {
const float m = sqrtf(x*x + y*y); const float m = sqrtf(x*x + y*y);
x /= m; y /= m; *xx = x / m; *yy = y / m;
} else {
*xx = x; *yy = y; *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 m = sqrtf(diff_x*diff_x + diff_y*diff_y);
const float dir_x = diff_x / m * max_delta, const float dir_x = diff_x / m * max_delta,
dir_y = diff_y / 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_x = x + dir_x;
*out_y = y + dir_y; *out_y = y + dir_y;
return 0; return 0;