Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 141 lines (100 sloc) 3.518 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/*
Copyright (C) 2009 Andrew Caudwell (acaudwell@gmail.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "zoomcamera.h"

ZoomCamera::ZoomCamera() {
}

ZoomCamera::ZoomCamera(vec3f start, vec3f target, float min_distance, float max_distance) : Camera(start,target) {
    dest = start;
    up = vec3f(0.0f, -1.0f, 0.0f);

    setMinDistance(min_distance);
    setMaxDistance(max_distance);

    padding = 1.0;
    speed = 1.0;
    lockon = false;
    lockon_time = 0.0;
    reset();
}

void ZoomCamera::reset() {
    Camera::reset();
}

float ZoomCamera::getMaxDistance() { return max_distance; }
float ZoomCamera::getMinDistance() { return min_distance; }

void ZoomCamera::setPadding(float padding) {
    this->padding = padding;
}

void ZoomCamera::setMaxDistance(float max) {
    max_distance = max;
    zfar = max + 1.0;
}

void ZoomCamera::setMinDistance(float min) {
    min_distance = min;
}

void ZoomCamera::lockOn(bool lockon) {

    if(lockon) {
         lockon_time = 1.0;
    }

    this->lockon = lockon;
}

void ZoomCamera::setSpeed(float speed) {
    this->speed = speed;
}

void ZoomCamera::adjust(Bounds2D& bounds) {

    //center camera on bounds

    //scale by 10% so we dont have stuff right on the edge of the screen
    float width = bounds.width() * padding;
    float height = bounds.height() * padding;

    vec2f centre = bounds.centre();

    //adjust by screen ratio
    float dratio = display.height / (float) display.width;

      if(dratio > 1.0) {
          height /= dratio;
      } else {
          width *= dratio;
      }

    //calc visible width of the opposite wall at a distance of 1 this fov
    float toa = tan( getFov() * 0.5f * DEGREES_TO_RADIANS ) * 2.0;

    float distance;

    //TOA = tan = opposite/adjacent (distance = adjacent)
    //use the larger side of the box

    //cropping: vertical, horizontal or none
    if(gGourceSettings.crop_vertical) {
        distance = width / toa ;

    } else if (gGourceSettings.crop_horizontal) {
        distance = height / toa ;

    } else {

        if(width > height) {
            distance = width / toa ;
        } else {
            distance = height / toa ;
        }
    }

    //debugLog("toa %.2f, distance %.2f width %.2f height %.2f dratio %.2f\n", toa, distance, width, height, dratio);

    //check bounds are valid
    if(distance < min_distance) distance = min_distance;
    if(distance > max_distance) distance = max_distance;

    this->dest = vec3f(centre.x, centre.y, -distance);
}

void ZoomCamera::logic(float dt) {
    vec3f dp = (dest - pos);

    vec3f dpt = dp * dt * speed;

    if(lockon) {
        dpt = dpt * lockon_time + dp * (1.0-lockon_time);

        if(lockon_time>0.0) {
            lockon_time = std::max(0.0f, lockon_time-dt*0.5f);
        }
    }

    if(dpt.length2() > dp.length2()) dpt = dp;

    pos += dpt;

    target = vec3f(pos.x, pos.y, 0.0);
}
Something went wrong with that request. Please try again.