gamnit: fix `UICamera::height` doc and double assignation
[nit.git] / lib / gamnit / cameras.nit
index 0d64395..042896a 100644 (file)
@@ -133,7 +133,7 @@ class EulerCamera
                view = view * rotation_matrix
 
                # Use a projection matrix with a depth
-               var projection = new Matrix.perspective(pi*field_of_view_y/2.0,
+               var projection = new Matrix.perspective(field_of_view_y,
                        display.aspect_ratio, near, far)
 
                return view * projection
@@ -162,6 +162,35 @@ class EulerCamera
                yaw = 0.0
                roll = 0.0
        end
+
+       # Convert the position `x, y` on screen, to world coordinates on the plane at `target_z`
+       #
+       # `target_z` defaults to `0.0` and specifies the Z coordinates of the plane
+       # on which to project the screen position `x, y`.
+       #
+       # This method assumes that the camera is looking along the Z axis towards higher values.
+       # Using it in a different orientation can be useful, but won't result in valid
+       # world coordinates.
+       fun camera_to_world(x, y: Numeric, target_z: nullable Float): Point[Float]
+       do
+               # TODO, this method could be tweaked to support projecting the 2D point,
+               # on the near plane (x,y) onto a given distance no matter to orientation
+               # of the camera.
+
+               target_z = target_z or else 0.0
+
+               # Convert from pixel units / window resolution to
+               # units on the near clipping wall to
+               # units on the target wall at Z = 0
+               var near_height = (field_of_view_y/2.0).tan * near
+               var cross_screen_to_near = near_height / (display.height.to_f/2.0)
+               var cross_near_to_target = (position.z - target_z) / near
+               var mod = cross_screen_to_near * cross_near_to_target
+
+               var wx = position.x + (x.to_f-display.width.to_f/2.0) * mod
+               var wy = position.y - (y.to_f-display.height.to_f/2.0) * mod
+               return new Point[Float](wx, wy)
+       end
 end
 
 # Orthogonal camera to draw UI objects with services to work with screens of different sizes
@@ -181,13 +210,13 @@ class UICamera
        # Width in world units, defaults to the width in pixels of the screen
        var width: Float = display.width.to_f is lazy
 
-       # Height in world units, defaults to the height in pixels of the screen
-       var height: Float = display.height.to_f is lazy
+       # Height in world units, defaults to 1080.0
+       #
+       # Set this value using `reset_height`.
+       var height = 1080.0
 
        # Reset the camera position so that `height` world units are visible on the Y axis
        #
-       # By default, `height` is set to `display.height`.
-       #
        # This can be used to set standardized UI units independently from the screen resolution.
        fun reset_height(height: nullable Float)
        do
@@ -205,25 +234,37 @@ class UICamera
 
                var wx = x.to_f * width / display.width.to_f - position.x
                var wy = y.to_f * height / display.height.to_f - position.y
-               return new Point[Float](wx, wy)
+               return new Point[Float](wx, -wy)
        end
 
        # Center of the screen, from the point of view of the camera, at z = 0
-       fun center: Point3d[Float] do return new Point3d[Float](position.x + width / 2.0, position.y + height / 2.0, 0.0)
+       fun center: Point3d[Float] do return new Point3d[Float](position.x + width / 2.0, position.y - height / 2.0, 0.0)
+
+       # Center of the top of the screen, at z = 0
+       fun top: Point3d[Float] do return new Point3d[Float](position.x + width / 2.0, position.y, 0.0)
+
+       # Center of the bottom of the screen, at z = 0
+       fun bottom: Point3d[Float] do return new Point3d[Float](position.x + width / 2.0, position.y - height, 0.0)
+
+       # Center of the left border of the screen, at z = 0
+       fun left: Point3d[Float] do return new Point3d[Float](position.x, position.y - height / 2.0, 0.0)
+
+       # Center of the right border of the screen, at z = 0
+       fun right: Point3d[Float] do return new Point3d[Float](position.x + width, position.y - height / 2.0, 0.0)
 
-       # Anchor in the top left corner of the screen, at z = 0
+       # Top left corner of the screen, at z = 0
        fun top_left: Point3d[Float] do return new Point3d[Float](position.x, position.y, 0.0)
 
-       # Anchor in the top right corner of the screen, at z = 0
+       # Top right corner of the screen, at z = 0
        fun top_right: Point3d[Float] do return new Point3d[Float](position.x + width, position.y, 0.0)
 
-       # Anchor in the bottom left corner of the screen, at z = 0
-       fun bottom_left: Point3d[Float] do return new Point3d[Float](position.x, position.y + height, 0.0)
+       # Bottom left corner of the screen, at z = 0
+       fun bottom_left: Point3d[Float] do return new Point3d[Float](position.x, position.y - height, 0.0)
 
-       # Anchor in the bottom right corner of the screen, at z = 0
-       fun bottom_right: Point3d[Float] do return new Point3d[Float](position.x + width, position.y + height, 0.0)
+       # Bottom right corner of the screen, at z = 0
+       fun bottom_right: Point3d[Float] do return new Point3d[Float](position.x + width, position.y - height, 0.0)
 
-       # TODO cache the anchors and the matrix
+       # TODO cache the anchors
 
        redef fun mvp_matrix
        do