From: florianf Date: Fri, 5 Mar 2010 07:42:10 +0000 (+0000) Subject: badguy/walking_badguy.cpp: Fix the acceleration code. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=f93f1520945161b09650472ffd54e39ceb07d14d;p=supertux.git badguy/walking_badguy.cpp: Fix the acceleration code. It should now be able to handle all situations, even when the badguy is thrown backwards faster than he would normally walk forward (was broken before) and frozen badguys are no longer sliding along without a move animation. git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6548 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- diff --git a/src/badguy/walking_badguy.cpp b/src/badguy/walking_badguy.cpp index d5e34264b..5df35555d 100644 --- a/src/badguy/walking_badguy.cpp +++ b/src/badguy/walking_badguy.cpp @@ -95,19 +95,41 @@ WalkingBadguy::active_update(float elapsed_time) { BadGuy::active_update(elapsed_time); - float abs_cur_walk_speed = fabs (physic.get_velocity_x ()); - if ((abs_cur_walk_speed > (walk_speed - 5.0)) - && (abs_cur_walk_speed < (walk_speed + 5.0))) + float current_x_velocity = physic.get_velocity_x (); + float dest_x_velocity = (dir == LEFT) ? -walk_speed : +walk_speed; + + if (frozen) + { + physic.set_velocity_x (0.0); + physic.set_acceleration_x (0.0); + } + /* We're very close to our target speed. Just set it to avoid oscillation */ + else if ((current_x_velocity > (dest_x_velocity - 5.0)) + && (current_x_velocity < (dest_x_velocity + 5.0))) { - physic.set_velocity_x ((dir == LEFT) ? -walk_speed : +walk_speed); + physic.set_velocity_x (dest_x_velocity); physic.set_acceleration_x (0.0); } - /* acceleration == walk-speed => it will take one second to get from zero to full speed. */ - else if (abs_cur_walk_speed < walk_speed) { - physic.set_acceleration_x ((dir == LEFT) ? -walk_speed : +walk_speed); + /* Check if we're going too slow or even in the wrong direction */ + else if (((dir == LEFT) && (current_x_velocity > dest_x_velocity)) + || ((dir == RIGHT) && (current_x_velocity < dest_x_velocity))) + { + /* acceleration == walk-speed => it will take one second to get from zero + * to full speed. */ + physic.set_acceleration_x (dest_x_velocity); + } + /* Check if we're going too fast */ + else if (((dir == LEFT) && (current_x_velocity < dest_x_velocity)) + || ((dir == RIGHT) && (current_x_velocity > dest_x_velocity))) + { + /* acceleration == walk-speed => it will take one second to get twice the + * speed to normal speed. */ + physic.set_acceleration_x ((-1.0) * dest_x_velocity); } - else if (abs_cur_walk_speed > walk_speed) { - physic.set_acceleration_x ((dir == LEFT) ? +walk_speed : -walk_speed); + else + { + /* The above should have covered all cases. */ + assert (23 == 42); } if (max_drop_height > -1) { @@ -173,6 +195,7 @@ WalkingBadguy::freeze() { BadGuy::freeze(); physic.set_velocity_x(0); + set_walk_speed (0.0); } void