From 87b6a8469bdd9e3a09aa6a16c1a0fa8c9867c5bf Mon Sep 17 00:00:00 2001 From: Ivan Yuriev Date: Sat, 19 Apr 2025 03:15:47 +0300 Subject: [PATCH] camera follows mouse --- lib/camera.lua | 18 ++++++++++++++---- main.lua | 3 +-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/camera.lua b/lib/camera.lua index bfbcfc9..86dde39 100644 --- a/lib/camera.lua +++ b/lib/camera.lua @@ -1,13 +1,23 @@ Camera = { target = Vec3 {}, position = Vec3 {}, - lerp_speed = 5.0 + lerp_speed = 5.0, + max_offset = 1, -- на сколько метров камера может отдалиться от целевой позиции + max_offset_distance = 5 -- на сколько метров надо отвести мышь для максимального отдаления камеры } function Camera:update(dt) if not self.target then return end - -- Плавное движение камеры к цели - local to_target = self.target - self.position - self.position = self.position + to_target:scale(dt * self.lerp_speed) + local mx, my = love.mouse.getPosition() + local screen_center = Vec3 { love.graphics.getWidth() / 2, love.graphics.getHeight() / 2, 0 } + local mouse_offset = Vec3 { mx, my, 0 } - screen_center + local offset_distance = mouse_offset:length() / PIXELS_PER_METER + if offset_distance > self.max_offset_distance then offset_distance = self.max_offset_distance end + + local adjusted_target = self.target + + mouse_offset:normalize() * (self.max_offset * offset_distance / self.max_offset_distance) + + local to_target = adjusted_target - self.position + self.position = self.position + to_target * (dt * self.lerp_speed) end diff --git a/main.lua b/main.lua index 93a749e..2ded9c4 100644 --- a/main.lua +++ b/main.lua @@ -98,8 +98,7 @@ function love.draw() love.graphics.setCanvas() - local scale = math.min(love.graphics.getDimensions()) / (15 * PIXELS_PER_METER); - love.graphics.draw(spriteCanvas, 0, 0, 0, scale, scale) + love.graphics.draw(spriteCanvas) love.graphics.setColor(1, 1, 1, 1) end