From: Daniel Karbach Date: Mon, 23 Dec 2013 08:31:15 +0000 (+0100) Subject: adjust cam speed to zoom level X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=08d0e47634e1632c96ebe3308535a86f5e625b40;p=space.git adjust cam speed to zoom level --- diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 34edd25..20c3718 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -141,6 +141,7 @@ void Application::Update(int dt) { controlled->acc = Vector(control * 10); cam.Update(delta); univ.Update(delta); + focus.SetSpeed(500 / cam.Zoom()); focus.Update(delta); } diff --git a/src/graphics/Camera.cpp b/src/graphics/Camera.cpp index ad2587f..ea2f97d 100644 --- a/src/graphics/Camera.cpp +++ b/src/graphics/Camera.cpp @@ -5,7 +5,8 @@ namespace space { Camera::Camera(int w, int h, const Vector &t) : target(&t) -, offset(w/2, h/2) +, size(w, h) +, offset(size / 2) , zoom(1) , zoomAcc(0) { @@ -13,7 +14,8 @@ Camera::Camera(int w, int h, const Vector &t) void Camera::Resize(int w, int h) { - offset = Vector(w/2, h/2); + size = Vector(w, h); + offset = size / 2; } void Camera::Update(float delta) { diff --git a/src/graphics/Camera.h b/src/graphics/Camera.h index 911f236..4d49cb6 100644 --- a/src/graphics/Camera.h +++ b/src/graphics/Camera.h @@ -15,6 +15,9 @@ public: void SetTarget(const Vector &t) { target = &t; } void SetZoom(float z) { zoom = z; } + Vector ScreenSize() const { return size; } + float Zoom() const { return zoom; } + void Resize(int w, int h); void Update(float deltaT); @@ -23,18 +26,27 @@ public: void StartShrink(); void StopShrink(); + /// transform v from world coords to screen coords Vector ToScreen(Vector v) const { - return Vector(OffsetOf(v)) + offset; - } - Vector OffsetOf(Vector v) const { - return ToScale(v - *target); + return Vector(ToScale(v - *target)) + offset; } + /// scale v to current zoom level Vector ToScale(Vector v) const { return v * zoom; } + /// transform v from screen coords to world coords + Vector FromScreen(Vector v) const { + return FromScale(v - offset) + *target; + } + /// scale v back from current zoom level + Vector FromScale(Vector v) const { + return v / zoom; + } + private: const Vector *target; + Vector size; Vector offset; float zoom; diff --git a/src/graphics/Moveable.h b/src/graphics/Moveable.h index b5fbfe5..71f5909 100644 --- a/src/graphics/Moveable.h +++ b/src/graphics/Moveable.h @@ -16,6 +16,7 @@ public: public: const Vector &Pos() const { return pos; } Vector Vel() const { return Vector(dir) * speed; } + void SetSpeed(float s) { speed = s; } void Update(Scalar delta) { pos += Vel() * delta;