move_towards now compares absolutes of diff and dir to check if the destination will be reached
parent
fa2a911732
commit
1df623e669
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue