movetowards now also works in cases where x == 0 && y != 0 || x != 0 && y == 0
parent
3df374b3b9
commit
daa53c6ef0
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue