From 316b7ca9bb9b0f26d74a1e17f68bd86aeb1dc33c Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Mon, 24 May 2004 23:02:34 +0000 Subject: [PATCH] added some comments to scrolling code SVN-Revision: 1317 --- src/camera.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/camera.cpp b/src/camera.cpp index 2c145dafb..6ef816884 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -80,21 +80,28 @@ Camera::scroll_normal(float elapsed_time) if(elapsed_time < EPSILON) return; + /****** Vertical Scrolling part ******/ bool do_y_scrolling = true; if(player->dying || level->height == 19) do_y_scrolling = false; if(do_y_scrolling) { - float target_y; + // target_y is the high we target our scrolling at. This is not always the + // high of the player, but if he is jumping upwards we should use the + // position where he last touched the ground. + float target_y; if(player->fall_mode == Player::JUMPING) target_y = player->last_ground_y + player->base.height; else target_y = player->base.y + player->base.height; + // delta_y is the distance we'd have to travel to directly reach target_y float delta_y = translation.y - (target_y - screen->h/2); + // speed is the speed the camera would need to reach target_y in this frame float speed_y = delta_y / elapsed_time; + // limit the camera speed when jumping upwards if(player->fall_mode != Player::FALLING && player->fall_mode != Player::TRAMPOLINE_JUMP) { if(speed_y > max_speed_y) @@ -103,6 +110,7 @@ Camera::scroll_normal(float elapsed_time) speed_y = -max_speed_y; } + // finally scroll with calculated speed translation.y -= speed_y * elapsed_time; // don't scroll before the start or after the level's end @@ -112,14 +120,24 @@ Camera::scroll_normal(float elapsed_time) translation.y = 0; } + /****** Horizontal scrolling part *******/ + + // our camera is either in leftscrolling, rightscrolling or nonscrollingmode. + + // when suddenly changing directions while scrolling into the other direction. + // abort scrolling, since tux might be going left/right at a relatively small + // part of the map (like when jumping upwards) if((player->dir == ::LEFT && scrollchange == RIGHT) || (player->dir == ::RIGHT && scrollchange == LEFT)) scrollchange = NONE; + // when in left 1/3rd of screen scroll left if(player->base.x < translation.x + screen->w/3 && level->back_scrolling) scrollchange = LEFT; + // scroll right when in right 1/3rd of screen else if(player->base.x > translation.x + screen->w/3*2) scrollchange = RIGHT; + // calculate our scroll target depending on scroll mode float target_x; if(scrollchange == LEFT) target_x = player->base.x - screen->w/3*2; @@ -128,15 +146,19 @@ Camera::scroll_normal(float elapsed_time) else target_x = translation.x; + // that's the distance we would have to travel to reach target_x float delta_x = translation.x - target_x; + // the speed we'd need to travel to reach target_x in this frame float speed_x = delta_x / elapsed_time; + // limit our speed float maxv = 1 + fabsf(player->physic.get_velocity_x() * 1.3); if(speed_x > maxv) speed_x = maxv; else if(speed_x < -maxv) speed_x = -maxv; - + + // apply scrolling translation.x -= speed_x * elapsed_time; // don't scroll before the start or after the level's end -- 2.11.0