From df6d323e42cdf3c38dbcee34b73e0d746b5bc4ba Mon Sep 17 00:00:00 2001 From: Jan Mrna Date: Sun, 12 Oct 2025 16:19:38 +0200 Subject: [PATCH] Fix collision radius size when zooming --- cpp/src/camera.hpp | 12 ++++++++++++ cpp/src/gameloop.cpp | 7 ++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cpp/src/camera.hpp b/cpp/src/camera.hpp index f57b441..b321baa 100644 --- a/cpp/src/camera.hpp +++ b/cpp/src/camera.hpp @@ -15,6 +15,18 @@ public: WorldPos WindowToWorld(WindowPos) const; WindowSize WorldToWindowSize(WorldSize) const; WorldSize WindowToWorldSize(WindowSize) const; + + template + requires std::floating_point + T WindowToWorldSize(T window_size) const { + return window_size / static_cast(m_Zoom); + } + + template + requires std::floating_point + T WorldToWindowSize(T world_size) const { + return world_size * static_cast(m_Zoom); + } private: // TODO this should be replaced with a matrix diff --git a/cpp/src/gameloop.cpp b/cpp/src/gameloop.cpp index 4cf4097..81aad3e 100644 --- a/cpp/src/gameloop.cpp +++ b/cpp/src/gameloop.cpp @@ -43,13 +43,14 @@ void GameLoop::Draw() { // draw all the entities (player etc) for (auto &entity : m_Game->GetEntities()) { const auto &camera = m_Game->GetCamera(); - auto entity_pos_window = camera.WorldToWindow(entity->GetPosition()); - m_Window->DrawSprite(entity_pos_window, + auto entity_pos = camera.WorldToWindow(entity->GetPosition()); + m_Window->DrawSprite(entity_pos, entity->GetSprite(), camera.GetZoom()); if (entity->IsCollisionBoxVisible()) { - m_Window->DrawCircle(entity_pos_window, entity->GetCollisionRadius()); + float collision_radius = camera.WorldToWindowSize(entity->GetCollisionRadius()); + m_Window->DrawCircle(entity_pos, collision_radius); } }