From 3df374b3b9cfd47f5a973288e4de27c5348a3fd2 Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 18 Jun 2023 22:45:06 +0200 Subject: [PATCH] interpolate move will now immediately stop if destination is reached --- src/corelib/world.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/corelib/world.c b/src/corelib/world.c index fc15d77..5ed847b 100644 --- a/src/corelib/world.c +++ b/src/corelib/world.c @@ -171,7 +171,10 @@ void interpolate_move(object_t* object, float target_x, float target_y, float ma float dx = target_x - object->sprite.x, dy = target_y - object->sprite.y; // calculate direction x,y float m = sqrtf(dx*dx + dy*dy); - dx /= m; dy /= m; + if(dx != 0) + dx /= m; + if(dy != 0) + dy /= m; dx *= max_step_size; dy *= max_step_size; int step_count = max_step_size / m; @@ -187,7 +190,7 @@ void interpolate_move(object_t* object, float target_x, float target_y, float ma * 1. move towards target * 2. check collision with every other object */ - for(int steps = 0; steps < step_count; ++steps) { + for(int steps = 0; steps < step_count && (object->sprite.x != target_x || object->sprite.y != target_y); ++steps) { // move towards target, snap to target if distance is too low const float old_x = object->sprite.x, old_y = object->sprite.y; float new_x, new_y;