@@ -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