map rendering: wip coordinate system screen -> world and world -> screen
authorLukas Krickl <lukas@krickl.dev>
Tue, 3 Mar 2026 18:44:09 +0000 (19:44 +0100)
committerLukas Krickl <lukas@krickl.dev>
Tue, 3 Mar 2026 18:44:09 +0000 (19:44 +0100)
screen to world is still broken, but there are (failing) test cases :^)

src/r_assets.c
src/t_camera.c
src/test.c
src/tests/t_math.c
src/u_math.c
src/u_math.h

index 05588a27ddddea19ed924a8c7f6bb908ab9ccf63..6661eabd5052b0384b6762a1d03ec4e73feb2745 100644 (file)
@@ -64,7 +64,7 @@ void r_draw_solid_isometric_tile(i32 x, i32 y, r_color color) {
        
        /* draw a tile texture from origin */
        for (i = 0; i < R_TILE_W; i++) {
-               for (j = 0; j < R_TILE_H-1; j++) {
+               for (j = 0; j < R_TILE_H; j++) {
                        final_color = r_asset_fallback_tile[i + j * R_TILE_W] & color;
                        r_draw_pixel(&r_framebuffer, t.x + i, t.y + j, final_color);
                }
index 896dd7984f01fbc9f91ba34a8a81300603a47a4a..3e2a83007ccb667b55882f6529d5efa319223c89 100644 (file)
@@ -24,9 +24,7 @@ lrts_bool t_camera_is_visible_screen(struct t_camera *c, i32 x, i32 y) {
 }
 
 lrts_bool t_camera_is_visible_world(struct t_camera *c, i32 x, i32 y) {
-       struct u_vec2 t;
-       t.x = c->x;
-       t.y = c->y;
+       struct u_vec2 t = u_point_to_screen(c->x, c->y);
        
        return u_point_in_rect(x, y, t.x, t.y, t.x + c->viewport_x, t.y + c->viewport_y);
 }
index 3e11b59c2f63e4d9b1a3d2b8e13c021e49326782..9482ffe3dbafffc9b086fe3b43a3b28d7bd53315 100644 (file)
@@ -31,6 +31,7 @@ int main(int argc, char **argv) {
        T_TESTCASE("argv", t_test_argv);
        T_TESTCASE("arena", t_test_arena);
        T_TESTCASE("tile-to-screen", t_test_tile_to_screen);
+       T_TESTCASE("point-to-screen", t_test_point_to_world_world_to_point);
        T_TESTCASE("point-in-rect", t_test_pointinrect);
        T_TESTCASE("fixed-point-macro", t_test_fp_macros);
        T_TESTCASE("fixed-point-sqrt", t_test_fp_sqrt);
index 3e4e40613d7b00cb521788cc6c02036b9de7905f..8323229f7203b43f52821858f16c0bedd3fb1b50 100644 (file)
@@ -14,10 +14,27 @@ int t_test_tile_to_screen(void) {
        T_ASSERT(res.x == 0 && res.y == 0, ("tile to screen %d/%d\n", res.x, res.y));
 
        res = u_tile_to_screen(1, 1);
-       T_ASSERT(res.x == 96 && res.y == 0, ("tile to screen %d/%d\n", res.x, res.y));
+       T_ASSERT(res.x == 0 && res.y == 48, ("tile to screen %d/%d\n", res.x, res.y));
 
        res = u_tile_to_screen(1, 2);
-       T_ASSERT(res.x == 144 && res.y == 24, ("tile to screen %d/%d\n", res.x, res.y));
+       T_ASSERT(res.x == -48 && res.y == 72, ("tile to screen %d/%d\n", res.x, res.y));
+       return 0;
+}
+
+int t_test_point_to_world_world_to_point(void) {
+       struct u_vec2 res = u_point_to_screen(1, 2);
+       T_ASSERT(res.x == -2 && res.y == 1, ("point to screen %d/%d\n", res.x, res.y));
+       
+       res = u_point_to_world(res.x, res.y);
+       T_ASSERT(res.x == 1 && res.y == 2, ("point to world %d/%d\n", res.x, res.y));
+
+
+       res = u_point_to_screen(32, 64);
+       T_ASSERT(res.x == -48 && res.y == 40, ("point to screen %d/%d\n", res.x, res.y));
+
+       res = u_point_to_world(res.x, res.y);
+       T_ASSERT(res.x == 32 && res.y == 64, ("point to world %d/%d\n", res.x, res.y));
+
        return 0;
 }
 
index e4c40a6b0c46a48cc6b640180cacb2985ecab91e..4ddfb8827988cc237f33e0dea59df56eb9fa3013 100644 (file)
@@ -11,19 +11,23 @@ struct u_vec2 u_vec2_mul(i32 n, struct u_vec2 v) {
 }
 
 struct u_vec2 u_tile_to_screen(i32 x, i32 y) {
+       return u_point_to_screen(x * R_TILE_W, y * R_TILE_H); 
+}
+
+struct u_vec2 u_point_to_screen(i32 x, i32 y) {
        struct u_vec2 r;
 
-       r.x = (x * R_TILE_W / 2) + (y * R_TILE_W / 2);
-       r.y = (y * R_TILE_H / 2) - (x * R_TILE_H / 2);
+       r.x = (x / 2 - y);
+       r.y = (y + x / 2) / 2;
 
        return r;
 }
 
-struct u_vec2 u_point_to_screen(i32 x, i32 y) {
+struct u_vec2 u_point_to_world(i32 x, i32 y) {
        struct u_vec2 r;
 
-       r.x = (x) + (y);
-       r.y = (y) - (x);
+       r.x = (y + x);
+       r.y = (2 * y - x) / 2;
 
        return r;
 }
index af63f6c7e38cf53f5e910fbe3094764c6ab64d9a..5dc7678c4a95f443b515bac536aa30e0f3b4e9e6 100644 (file)
@@ -45,14 +45,17 @@ struct u_vec2 {
 struct u_vec2 u_vec2_mul(i32 n, struct u_vec2 v);
 
 /*
- * converts a tile position to
+ * converts a world tile position to
  * a screen position
  */
 struct u_vec2 u_tile_to_screen(i32 x, i32 y);
 
-/* convers a point to screen space */
+/* convers a world point to screen space */
 struct u_vec2 u_point_to_screen(i32 x, i32 y);
 
+/* converts a screen point to world space */
+struct u_vec2 u_point_to_world(i32 x, i32 y);
+
 /* tests if a point is contained inside a rectangle */
 lrts_bool u_point_in_rect(i32 x, i32 y, i32 rx, i32 ry, i32 rw, i32 rh);