Skip to content

Commit add4da8

Browse files
authored
Merge pull request #4845 from bamless/log-zoom
[examples] use logarithmic zoom scaling in 2d camera examples
2 parents cd92069 + e140aca commit add4da8

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

examples/core/core_2d_camera.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
********************************************************************************************/
1515

1616
#include "raylib.h"
17+
#include <math.h>
1718

1819
#define MAX_BUILDINGS 100
1920

@@ -81,7 +82,8 @@ int main(void)
8182
else if (camera.rotation < -40) camera.rotation = -40;
8283

8384
// Camera zoom controls
84-
camera.zoom += ((float)GetMouseWheelMove()*0.05f);
85+
// Uses log scaling to provide consistent zoom speed
86+
camera.zoom = expf(logf(camera.zoom) + ((float)GetMouseWheelMove()*0.1f));
8587

8688
if (camera.zoom > 3.0f) camera.zoom = 3.0f;
8789
else if (camera.zoom < 0.1f) camera.zoom = 0.1f;

examples/core/core_2d_camera_mouse_zoom.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ int main ()
7373
camera.target = mouseWorldPos;
7474

7575
// Zoom increment
76-
float scaleFactor = 1.0f + (0.25f*fabsf(wheel));
77-
if (wheel < 0) scaleFactor = 1.0f/scaleFactor;
78-
camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f);
76+
// Uses log scaling to provide consistent zoom speed
77+
float scale = 0.2f*wheel;
78+
camera.zoom = Clamp(expf(logf(camera.zoom)+scale), 0.125f, 64.0f);
7979
}
8080
}
8181
else
@@ -96,10 +96,10 @@ int main ()
9696
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
9797
{
9898
// Zoom increment
99+
// Uses log scaling to provide consistent zoom speed
99100
float deltaX = GetMouseDelta().x;
100-
float scaleFactor = 1.0f + (0.01f*fabsf(deltaX));
101-
if (deltaX < 0) scaleFactor = 1.0f/scaleFactor;
102-
camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f);
101+
float scale = 0.005f*deltaX;
102+
camera.zoom = Clamp(expf(logf(camera.zoom)+scale), 0.125f, 64.0f);
103103
}
104104
}
105105
//----------------------------------------------------------------------------------
@@ -143,4 +143,4 @@ int main ()
143143
CloseWindow(); // Close window and OpenGL context
144144
//--------------------------------------------------------------------------------------
145145
return 0;
146-
}
146+
}

0 commit comments

Comments
 (0)