Skip to content

Commit 02f6093

Browse files
committed
document transform
1 parent ffe81e6 commit 02f6093

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

src/bin/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl ApplicationHandler for Application {
182182
WindowEvent::CursorMoved { position, .. } => {
183183
let logical_position = position.to_logical(self.painter.get_hidpi_factor() / 2.0);
184184

185-
let screen_to_global = self.app_state.screen.screen_to_world(self.app_state.zoom);
185+
let screen_to_global = self.app_state.screen.pixel_to_world(self.app_state.zoom);
186186
let new_pos = screen_to_global
187187
* vec4(
188188
logical_position.x as f32,

src/lib/math/screen.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,24 @@ impl Screen {
6161
glm::scaling(&scale)
6262
}
6363

64-
pub fn screen_to_world(&self, z: f32) -> glm::TMat4<f32> {
65-
// TODO: make logical width instead of phyiscal.
66-
glm::translation2d(&glm::vec2(dbg!(self.center.x), self.center.y))
67-
* glm::scaling2d(&glm::vec2(
68-
1.0 / 2.0 * f32::powf(2.0, -z) / self.tile_size(),
69-
1.0 / 2.0 * f32::powf(2.0, -z) / self.tile_size(),
70-
));
71-
64+
/// Transforms coordinates from screen space to world space.
65+
///
66+
/// This means the ranges get transformed as follows:
67+
/// - [0, width] => [0, 1]
68+
/// - [0, height] => [0, 1]
69+
///
70+
/// First we scale to the world space and then we also translate according to where the screen rect is currently.
71+
pub fn pixel_to_world(&self, z: f32) -> glm::TMat4<f32> {
7272
let matrix = self.global_to_screen(z);
7373
let screen_to_global = nalgebra_glm::inverse(&matrix);
7474

75-
let pixel_to_screen = glm::translation(&glm::vec3(-1.0, -1.0, 0.0))
76-
* glm::scaling(&glm::vec3(
77-
1.0 / (self.width / 2.0),
78-
1.0 / (self.height / 2.0),
79-
1.0,
80-
));
75+
let translate_screen = glm::translation(&glm::vec3(-1.0, -1.0, 0.0));
76+
let scale_to_screen = glm::scaling(&glm::vec3(
77+
1.0 / (self.width / 2.0),
78+
1.0 / (self.height / 2.0),
79+
1.0,
80+
));
81+
let pixel_to_screen = translate_screen * scale_to_screen;
8182

8283
screen_to_global * pixel_to_screen
8384
}

0 commit comments

Comments
 (0)