move_towards now compares absolutes of diff and dir to check if the destination will be reached

pull/8/head
Sara 2023-06-18 16:09:43 +02:00
parent fa2a911732
commit 1df623e669
1 changed files with 5 additions and 3 deletions

View File

@ -26,10 +26,12 @@ void normalize(float* xx, float* yy) {
static inline
int move_towards(float* out_x, float* out_y, float x, float y, float tx, float ty, float max_delta) {
const float diff_x = tx - x, diff_y = ty - y;
const float diff_x = tx - x,
diff_y = ty - y;
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(dir_x < diff_x && dir_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)) {
*out_x = x + dir_x;
*out_y = y + dir_y;
return 0;