From 8b84c999d284c69aba55825db909f90ed3768916 Mon Sep 17 00:00:00 2001 From: Fabrizio Pietrucci Date: Wed, 19 Mar 2025 19:48:04 +0100 Subject: [PATCH 1/2] Use logarithmic zoom scaling in 2d camera zoom examples --- examples/core/core_2d_camera.c | 4 +++- examples/core/core_2d_camera_mouse_zoom.c | 14 +++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/examples/core/core_2d_camera.c b/examples/core/core_2d_camera.c index 0a551f68b..11c15d448 100644 --- a/examples/core/core_2d_camera.c +++ b/examples/core/core_2d_camera.c @@ -14,6 +14,7 @@ ********************************************************************************************/ #include "raylib.h" +#include #define MAX_BUILDINGS 100 @@ -81,7 +82,8 @@ int main(void) else if (camera.rotation < -40) camera.rotation = -40; // Camera zoom controls - camera.zoom += ((float)GetMouseWheelMove()*0.05f); + // Uses log scaling to provide consistent zoom speed + camera.zoom = expf(logf(camera.zoom) + ((float)GetMouseWheelMove()*0.05f)); if (camera.zoom > 3.0f) camera.zoom = 3.0f; else if (camera.zoom < 0.1f) camera.zoom = 0.1f; diff --git a/examples/core/core_2d_camera_mouse_zoom.c b/examples/core/core_2d_camera_mouse_zoom.c index cfdaf15aa..31aa7bc6b 100644 --- a/examples/core/core_2d_camera_mouse_zoom.c +++ b/examples/core/core_2d_camera_mouse_zoom.c @@ -73,9 +73,9 @@ int main () camera.target = mouseWorldPos; // Zoom increment - float scaleFactor = 1.0f + (0.25f*fabsf(wheel)); - if (wheel < 0) scaleFactor = 1.0f/scaleFactor; - camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f); + // Uses log scaling to provide consistent zoom speed + float scale = 0.2f*wheel; + camera.zoom = Clamp(expf(logf(camera.zoom)+scale), 0.125f, 64.0f); } } else @@ -96,10 +96,10 @@ int main () if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) { // Zoom increment + // Uses log scaling to provide consistent zoom speed float deltaX = GetMouseDelta().x; - float scaleFactor = 1.0f + (0.01f*fabsf(deltaX)); - if (deltaX < 0) scaleFactor = 1.0f/scaleFactor; - camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f); + float scale = 0.005f*deltaX; + camera.zoom = Clamp(expf(logf(camera.zoom)+scale), 0.125f, 64.0f); } } //---------------------------------------------------------------------------------- @@ -143,4 +143,4 @@ int main () CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; -} \ No newline at end of file +} From e140aca1b5ce2595336aad4a2300033bd9a9a452 Mon Sep 17 00:00:00 2001 From: Fabrizio Pietrucci Date: Wed, 19 Mar 2025 20:19:19 +0100 Subject: [PATCH 2/2] Increase zoom factor in `core_2d_camera.c` This compensate for slower zoom speed due to log scaling --- examples/core/core_2d_camera.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/core/core_2d_camera.c b/examples/core/core_2d_camera.c index 11c15d448..07766e0fb 100644 --- a/examples/core/core_2d_camera.c +++ b/examples/core/core_2d_camera.c @@ -83,7 +83,7 @@ int main(void) // Camera zoom controls // Uses log scaling to provide consistent zoom speed - camera.zoom = expf(logf(camera.zoom) + ((float)GetMouseWheelMove()*0.05f)); + camera.zoom = expf(logf(camera.zoom) + ((float)GetMouseWheelMove()*0.1f)); if (camera.zoom > 3.0f) camera.zoom = 3.0f; else if (camera.zoom < 0.1f) camera.zoom = 0.1f;