|
1 | | -go.property("camera_url", msg.url("/camera#camera")) |
| 1 | +go.property("camera_url", msg.url("/camera#camera")) -- URL of the camera component |
2 | 2 | go.property("hud_url", msg.url("/ui#hud")) |
3 | 3 | go.property("angle", -45) -- we use this property to animate the rotation of the player around the center of the scene |
4 | 4 |
|
5 | | ---- Converts a world position to screen coordinates. |
6 | | --- This function transforms a 3D world position to 2D screen coordinates using the camera's |
7 | | --- view and projection matrices. The resulting coordinates are in screen space where (0,0) |
8 | | --- is the bottom-left corner of the screen. |
9 | | --- |
10 | | --- @param world_position vector3 The world position to convert. |
11 | | --- @param camera_url url|string The camera component URL to use for the transformation. |
12 | | --- @return number screen_x The X coordinate in screen space. |
13 | | --- @return number screen_y The Y coordinate in screen space. |
14 | | --- @return number screen_z Always returns 0 (depth information is not preserved). |
15 | | -local function world_to_screen(world_position, camera_url) |
16 | | - local proj = camera.get_projection(camera_url) |
17 | | - local view = camera.get_view(camera_url) |
18 | | - |
19 | | - local view_proj = proj * view |
20 | | - local scr_coord = view_proj * vmath.vector4(world_position.x, world_position.y, world_position.z, 1) |
21 | | - local w, h = window.get_size() |
22 | | - scr_coord.x = (scr_coord.x / scr_coord.w + 1) * 0.5 * w |
23 | | - scr_coord.y = (scr_coord.y / scr_coord.w + 1) * 0.5 * h |
24 | | - |
25 | | - return vmath.vector3(scr_coord.x, scr_coord.y, 0) |
26 | | -end |
27 | | - |
28 | 5 | function init(self) |
29 | 6 | -- Get the IDs of the player view and UI objects |
30 | 7 | self.player_view_id = go.get_id("player_view") |
@@ -54,7 +31,7 @@ function update(self, dt) |
54 | 31 | -- Update the world transform of the player UI object and convert the world position to screen coordinates |
55 | 32 | go.update_world_transform(self.player_ui_id) |
56 | 33 | local world_pos = go.get_world_position(self.player_ui_id) |
57 | | - local screen_pos = world_to_screen(world_pos, self.camera_url) |
| 34 | + local screen_pos = camera.world_to_screen(world_pos, self.camera_url) |
58 | 35 | -- Send the screen position to the HUD script |
59 | 36 | msg.post(self.hud_url, "update_data", { screen_position = screen_pos }) |
60 | 37 | end |
0 commit comments