From 750047ab86ff25f0c7adbbe670411a45d1b3d1db Mon Sep 17 00:00:00 2001 From: Joppe Boeve Date: Thu, 18 Jan 2024 17:34:22 +0100 Subject: [PATCH 1/7] Solution not really working nicely with wide angles --- Makefile | 4 ++-- include/meta.h | 10 +++++----- include/test_utils.h | 20 ++++++++++---------- src/game/keys.c | 5 ++--- src/game/player.c | 40 +++++++++++++++++++++++++++++++++++++-- src/game/render_minimap.c | 4 ++-- src/test_utils.c | 16 ++++++++++++++-- test_maps/valid_tex.cub | 6 +++--- 8 files changed, 76 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index ff55b90..fb11456 100644 --- a/Makefile +++ b/Makefile @@ -15,8 +15,8 @@ RUN_CMD := ./$(NAME) test_maps/valid_tex.cub # RUN_CMD := ./$(NAME) test_maps/small.cub # CFLAGS += -Wall -Wextra -Werror -CFLAGS += -Wall -Wextra -# CFLAGS += -g -fsanitize=address +# CFLAGS += -Wall -Wextra +CFLAGS += -g -fsanitize=address # CFLAGS += -g # CFLAGS += -Ofast -flto -march=native diff --git a/include/meta.h b/include/meta.h index d0fb002..d3ee9a7 100644 --- a/include/meta.h +++ b/include/meta.h @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/01 20:07:37 by jboeve#+##+# */ -/* Updated: 2024/01/18 11:56:06 by jboeve ######## odam.nl */ +/* Updated: 2024/01/18 14:09:20 by jboeve ######## odam.nl */ /**/ /* ************************************************************************** */ @@ -126,10 +126,10 @@ typedef union s_rgba typedef enum e_side { - SIDE_N, - SIDE_S, - SIDE_E, - SIDE_W, + SIDE_N = 0, + SIDE_S = 1, + SIDE_E = 2, + SIDE_W = 3, } t_side; typedef struct s_ray { diff --git a/include/test_utils.h b/include/test_utils.h index 528b3b6..9a4fbc7 100644 --- a/include/test_utils.h +++ b/include/test_utils.h @@ -1,13 +1,13 @@ /* ************************************************************************** */ -/**/ -/* :::::::: */ -/* test_utils.h :+::+: */ -/*+:+ */ -/* By: joppe +#+ */ -/* +#+ */ -/* Created: 2024/01/02 19:56:05 by joppe #+##+# */ -/* Updated: 2024/01/02 23:20:17 by joppe ######## odam.nl */ -/**/ +/* */ +/* :::::::: */ +/* test_utils.h :+: :+: */ +/* +:+ */ +/* By: jboeve +#+ */ +/* +#+ */ +/* Created: 2024/01/18 13:59:19 by jboeve #+# #+# */ +/* Updated: 2024/01/18 14:10:07 by jboeve ######## odam.nl */ +/* */ /* ************************************************************************** */ @@ -23,5 +23,5 @@ void print_cell(t_cell_type cell); void print_map(char *map, uint32_t w, uint32_t h); void print_map_cell(t_cell_type *level, uint32_t w, uint32_t h); void print_ray(const char *s, const t_ray *r); - +void print_direction(t_side dir); #endif // !TEST_UTILS_H diff --git a/src/game/keys.c b/src/game/keys.c index 2cbd08a..96d6916 100644 --- a/src/game/keys.c +++ b/src/game/keys.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* keys.c :+: :+: :+: */ +/* keys.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:07 by yzaim #+# #+# */ -/* Updated: 2024/01/08 15:27:11 by yzaim ### ########.fr */ +/* Updated: 2024/01/18 12:50:58 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -49,7 +49,6 @@ static void keys_handle_move(t_meta *meta, double delta_time) pressed = true; trans = vec2d_add(trans, (t_vec2d) {(vec2d_rotate(p->direction, PI / 2).x) * speed, (vec2d_rotate(p->direction, PI / 2).y) * speed}); } - if (pressed) player_move(p, trans); } diff --git a/src/game/player.c b/src/game/player.c index fc524c4..22cca78 100644 --- a/src/game/player.c +++ b/src/game/player.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */ -/* Updated: 2024/01/17 15:55:18 by jboeve ######## odam.nl */ +/* Updated: 2024/01/18 17:10:32 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -43,10 +43,46 @@ void player_move(t_player *p, t_vec2d transform) t_ray r = raycaster_cast(p->position, vec2d_normalize(transform), bound_check, p->meta); // print_ray("bound_ray", &r); + const double margin = 0.005; + + float angle = (atan2(p->direction.x, p->direction.y) / PI * 180 + 180); + printf("angle [%f]\n", angle); if (r.length > .5) { - p->position.x += transform.x; p->position.y += transform.y; + p->position.x += transform.x; + } + else if (r.length > .1) + { + printf("angle [%f]\n", angle); + + double res = 0.0; + + // Colission with a wall the y-axis + if ((angle > 0 && angle < 45) || (angle > 315)) + { + printf("Gliding N\n"); + res = transform.x * -p->direction.y; + r = raycaster_cast(p->position, (t_vec2d) {res, 0.0}, bound_check, p->meta); + if (r.length > .1) + p->position.x += res; + } + else if (angle > 90 && angle < 270) + { + printf("Gliding S\n"); + res = transform.x * p->direction.y; + r = raycaster_cast(p->position, (t_vec2d) {res, 0.0}, bound_check, p->meta); + if (r.length > .1) + p->position.x += res; + } + + + // if (angle == 1 || angle == 2) + // { + // double res = transform.y * -p->direction.x; + // r = raycaster_cast(p->position, (t_vec2d) {0.0, res}, bound_check, p->meta); + // p->position.y += res; + // } } player_raycast(p); } diff --git a/src/game/render_minimap.c b/src/game/render_minimap.c index 0a07de1..c5199a5 100644 --- a/src/game/render_minimap.c +++ b/src/game/render_minimap.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* render_minimap.c :+: :+: :+: */ +/* render_minimap.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:53 by yzaim #+# #+# */ -/* Updated: 2024/01/08 15:27:58 by yzaim ### ########.fr */ +/* Updated: 2024/01/18 12:43:48 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/test_utils.c b/src/test_utils.c index 8c23404..1156ac0 100644 --- a/src/test_utils.c +++ b/src/test_utils.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* test_utils.c :+: :+: :+: */ +/* test_utils.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:25:38 by yzaim #+# #+# */ -/* Updated: 2024/01/08 15:25:49 by yzaim ### ########.fr */ +/* Updated: 2024/01/18 14:10:17 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -31,6 +31,18 @@ void print_ray(const char *s, const t_ray *r) printf("[%s] | ray_direction [%lf][%lf] | length [%lf] | ray_end [%lf][%lf] | hit_side [%s]\n", s, r->direction.x, r->direction.y, r->length, r->end.x, r->end.y, side_text); } +void print_direction(t_side dir) +{ + const char *text[4] = { + [SIDE_N] = "N", + [SIDE_E] = "E", + [SIDE_S] = "S", + [SIDE_W] = "W", + }; + + printf("[%s]\n", text[dir]); +} + void print_cell(t_cell_type cell) { printf("cell [%s]\n", CELL_NAMES[cell]); diff --git a/test_maps/valid_tex.cub b/test_maps/valid_tex.cub index 1b23e53..e1ae7f1 100644 --- a/test_maps/valid_tex.cub +++ b/test_maps/valid_tex.cub @@ -1,7 +1,7 @@ -NO texture_examples/purplestone2.png +NO texture_examples/purplestone1.png WE texture_examples/redbrick.png EA texture_examples/redbrick.png -SO texture_examples/purplestone2.png +SO texture_examples/purplestone1.png F 255,255,255 C 12, 12, 12 @@ -13,5 +13,5 @@ C 12, 12, 12 1000100001 100000000111 100000000001 -1000000E011 +1000000N011 1111111111 From 34688bdf48e167170e20bee1c6d284600f795d91 Mon Sep 17 00:00:00 2001 From: joppe Date: Sat, 20 Jan 2024 00:22:57 +0100 Subject: [PATCH 2/7] More experimentation --- Makefile | 2 +- include/vector.h | 5 +-- src/game/player.c | 68 +++++++++++++++-------------------------- src/vector/vec2d.c | 10 ++++-- test_maps/valid_tex.cub | 12 ++++---- 5 files changed, 43 insertions(+), 54 deletions(-) diff --git a/Makefile b/Makefile index fb11456..8f05cea 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ RUN_CMD := ./$(NAME) test_maps/valid_tex.cub # CFLAGS += -Wall -Wextra -Werror # CFLAGS += -Wall -Wextra -CFLAGS += -g -fsanitize=address +# CFLAGS += -g -fsanitize=address # CFLAGS += -g # CFLAGS += -Ofast -flto -march=native diff --git a/include/vector.h b/include/vector.h index d7e9269..a1cbd08 100644 --- a/include/vector.h +++ b/include/vector.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /**/ /* :::::::: */ -/* vector.h :+::+: */ +/* vector.h :+: :+: */ /*+:+ */ /* By: joppe +#+ */ /* +#+ */ /* Created: 2024/01/02 19:07:15 by joppe #+##+# */ -/* Updated: 2024/01/03 18:35:23 by joppe ######## odam.nl */ +/* Updated: 2024/01/19 23:36:47 by joppe ######## odam.nl */ /**/ /* ************************************************************************** */ @@ -35,6 +35,7 @@ typedef struct s_vec2u { t_vec2d vec2d_add(t_vec2d v1, t_vec2d v2); t_vec2d vec2d_sub(t_vec2d v1, t_vec2d v2); t_vec2d vec2d_scalar_product(t_vec2d vec, double scalar); +double vec2d_dot_product(t_vec2d v1, t_vec2d v2); t_vec2d vec2d_mul(t_vec2d v1, t_vec2d v2); t_vec2d vec2d_rotate(t_vec2d old, double radiant); t_vec2d vec2d_normalize(t_vec2d vec); diff --git a/src/game/player.c b/src/game/player.c index 22cca78..284c35a 100644 --- a/src/game/player.c +++ b/src/game/player.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */ -/* Updated: 2024/01/18 17:10:32 by jboeve ######## odam.nl */ +/* Updated: 2024/01/20 00:22:42 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,58 +32,39 @@ bool bound_check(void *param, uint32_t x, uint32_t y) } } -void player_move(t_player *p, t_vec2d transform) +void print_angle(t_player *p) { - t_vec2d new_position; + // NORTH = 0 + const float angle = (atan2(p->direction.x, p->direction.y) / PI * 180 + 180); - new_position.x = (p->position.x + (transform.x)); - new_position.y = (p->position.y + (transform.y)); + if (angle > 45.0 && angle < 135.0) + { + printf("N\n"); + } + else if (angle > 135.0 && angle < 225.0) + { + printf("E\n"); + } + else if (angle > 225.0 && angle < 315.0) + { + printf("S\n"); + } + else if (angle > 315.0 || angle < 45.0) + { + printf("W\n"); + } +} - // TODO Fix. +void player_move(t_player *p, t_vec2d transform) +{ t_ray r = raycaster_cast(p->position, vec2d_normalize(transform), bound_check, p->meta); - // print_ray("bound_ray", &r); - - const double margin = 0.005; - float angle = (atan2(p->direction.x, p->direction.y) / PI * 180 + 180); - printf("angle [%f]\n", angle); if (r.length > .5) { p->position.y += transform.y; p->position.x += transform.x; } - else if (r.length > .1) - { - printf("angle [%f]\n", angle); - - double res = 0.0; - - // Colission with a wall the y-axis - if ((angle > 0 && angle < 45) || (angle > 315)) - { - printf("Gliding N\n"); - res = transform.x * -p->direction.y; - r = raycaster_cast(p->position, (t_vec2d) {res, 0.0}, bound_check, p->meta); - if (r.length > .1) - p->position.x += res; - } - else if (angle > 90 && angle < 270) - { - printf("Gliding S\n"); - res = transform.x * p->direction.y; - r = raycaster_cast(p->position, (t_vec2d) {res, 0.0}, bound_check, p->meta); - if (r.length > .1) - p->position.x += res; - } - - - // if (angle == 1 || angle == 2) - // { - // double res = transform.y * -p->direction.x; - // r = raycaster_cast(p->position, (t_vec2d) {0.0, res}, bound_check, p->meta); - // p->position.y += res; - // } - } + print_angle(p); player_raycast(p); } @@ -92,6 +73,7 @@ void player_turn(t_player *p, float radiant) { p->direction = vec2d_rotate(p->direction, radiant); p->cam_plane = vec2d_rotate(p->cam_plane, radiant); + print_angle(p); player_raycast(p); } diff --git a/src/vector/vec2d.c b/src/vector/vec2d.c index a3c90aa..46e2c44 100644 --- a/src/vector/vec2d.c +++ b/src/vector/vec2d.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* vec2d.c :+: :+: */ +/* vec2d.c :+: :+: */ /* +:+ */ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 16:09:26 by yzaim #+# #+# */ -/* Updated: 2024/01/08 16:09:27 by yzaim ######## odam.nl */ +/* Updated: 2024/01/19 23:37:30 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -31,6 +31,12 @@ t_vec2d vec2d_scalar_product(t_vec2d vec, double scalar) return ((t_vec2d){vec.x * scalar, vec.y * scalar}); } +double vec2d_dot_product(t_vec2d v1, t_vec2d v2) +{ + return ((v1.x * v2.x) + (v1.y * v2.y)); +} + + t_vec2d vec2d_mul(t_vec2d v1, t_vec2d v2) { return ((t_vec2d){v1.x * v2.x, v1.y * v2.y}); diff --git a/test_maps/valid_tex.cub b/test_maps/valid_tex.cub index e1ae7f1..823e33f 100644 --- a/test_maps/valid_tex.cub +++ b/test_maps/valid_tex.cub @@ -6,12 +6,12 @@ F 255,255,255 C 12, 12, 12 11111111111111 -1000010001 -1000010001 -1000110001 -1000100001 -1000100001 +1000000001 +1000N00001 +1000000001 +1000000001 +1000000001 100000000111 100000000001 -1000000N011 +10000000011 1111111111 From cb3d7df3f3415a9160f541567bb766877708744a Mon Sep 17 00:00:00 2001 From: joppe Date: Sat, 20 Jan 2024 00:41:28 +0100 Subject: [PATCH 3/7] GLIDING WORKSKSS?!??!?!?!? (kinda) Can get kinda "in" the wall. --- src/game/player.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/game/player.c b/src/game/player.c index 284c35a..a3b89f0 100644 --- a/src/game/player.c +++ b/src/game/player.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */ -/* Updated: 2024/01/20 00:22:42 by joppe ######## odam.nl */ +/* Updated: 2024/01/20 00:39:14 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,7 +36,6 @@ void print_angle(t_player *p) { // NORTH = 0 const float angle = (atan2(p->direction.x, p->direction.y) / PI * 180 + 180); - if (angle > 45.0 && angle < 135.0) { printf("N\n"); @@ -64,6 +63,24 @@ void player_move(t_player *p, t_vec2d transform) p->position.y += transform.y; p->position.x += transform.x; } + else + { + const int comp = (r.hit_side == SIDE_N || r.hit_side == SIDE_S); + const t_vec2d normal = {comp, !comp}; + const double dot_product = vec2d_dot_product(transform, normal); + + t_vec2d delta_pos; + delta_pos.x = transform.x - normal.x * dot_product; + delta_pos.y = transform.y - normal.y * dot_product; + r = raycaster_cast(p->position, vec2d_normalize(transform), bound_check, p->meta); + + if (r.length > .3) + { + p->position.x += delta_pos.x; + p->position.y += delta_pos.y; + } + + } print_angle(p); player_raycast(p); } From d58113640b4d1421398d75cdf9b70d41920d4f52 Mon Sep 17 00:00:00 2001 From: joppe Date: Sat, 20 Jan 2024 00:52:37 +0100 Subject: [PATCH 4/7] Fix compiler warning. Lower movement speed --- MLX42/include/MLX42/MLX42_Int.h | 2 +- include/meta.h | 7 +++---- src/game/player.c | 33 +++++++++++++-------------------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/MLX42/include/MLX42/MLX42_Int.h b/MLX42/include/MLX42/MLX42_Int.h index 5fa5558..f59c8ef 100644 --- a/MLX42/include/MLX42/MLX42_Int.h +++ b/MLX42/include/MLX42/MLX42_Int.h @@ -6,7 +6,7 @@ /* By: W2Wizard +#+ */ /* +#+ */ /* Created: 2021/12/27 23:55:34 by W2Wizard #+# #+# */ -/* Updated: 2024/01/17 12:49:14 by jboeve ######## odam.nl */ +/* Updated: 2024/01/20 00:49:21 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/include/meta.h b/include/meta.h index d3ee9a7..fa10e2d 100644 --- a/include/meta.h +++ b/include/meta.h @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/01 20:07:37 by jboeve#+##+# */ -/* Updated: 2024/01/18 14:09:20 by jboeve ######## odam.nl */ +/* Updated: 2024/01/20 00:47:06 by joppe ######## odam.nl */ /**/ /* ************************************************************************** */ @@ -61,11 +61,10 @@ // Game #define TICK_RATE (1.0 / 60.0) -#define PLAYER_MOVE_SPEED 5.0 +#define PLAYER_MOVE_SPEED 3.5 #define PLAYER_RUN_MODIFIER 2.5 -#define PLAYER_ROTATE_SPEED 5.0 +#define PLAYER_ROTATE_SPEED 3.0 #define PLAYER_ROTATE_MODIFIER 2.5 -#define PLAYER_MOV_SPEED 0.08 #define MINIMAP_WIDTH 350 #define MINIMAP_HEIGHT 230 diff --git a/src/game/player.c b/src/game/player.c index a3b89f0..1ae663d 100644 --- a/src/game/player.c +++ b/src/game/player.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */ -/* Updated: 2024/01/20 00:39:14 by joppe ######## odam.nl */ +/* Updated: 2024/01/20 00:52:20 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ #include #include -bool bound_check(void *param, uint32_t x, uint32_t y) +bool bound_check(const void *param, uint32_t x, uint32_t y) { t_meta *const meta = (t_meta *) param; if (x < meta->map.width && y < meta->map.height) @@ -58,30 +58,25 @@ void player_move(t_player *p, t_vec2d transform) { t_ray r = raycaster_cast(p->position, vec2d_normalize(transform), bound_check, p->meta); - if (r.length > .5) - { - p->position.y += transform.y; - p->position.x += transform.x; - } + if (r.length > 0.5) + p->position = vec2d_add(p->position, transform); else { - const int comp = (r.hit_side == SIDE_N || r.hit_side == SIDE_S); - const t_vec2d normal = {comp, !comp}; - const double dot_product = vec2d_dot_product(transform, normal); + const int comp = (r.hit_side == SIDE_N || r.hit_side == SIDE_S); + const t_vec2d normal = {comp, !comp}; + const double dot_product = vec2d_dot_product(transform, normal); t_vec2d delta_pos; delta_pos.x = transform.x - normal.x * dot_product; delta_pos.y = transform.y - normal.y * dot_product; r = raycaster_cast(p->position, vec2d_normalize(transform), bound_check, p->meta); - if (r.length > .3) + if (r.length > 0.3) { p->position.x += delta_pos.x; p->position.y += delta_pos.y; } - } - print_angle(p); player_raycast(p); } @@ -90,22 +85,20 @@ void player_turn(t_player *p, float radiant) { p->direction = vec2d_rotate(p->direction, radiant); p->cam_plane = vec2d_rotate(p->cam_plane, radiant); - print_angle(p); player_raycast(p); } void player_raycast(t_player *p) { - uint32_t w = p->meta->image->width; - uint32_t col; - t_vec2d ray_start; - double camera_x; + uint32_t col; + double camera_x; + t_vec2d ray_start; // TODO Just create the player.plane here instead of saving it. col = 0; - while(col < w) + while (col < p->meta->image->width) { - camera_x = (2 * col / ((double) w) - 1); + camera_x = (2 * col / ((double) p->meta->image->width) - 1); ray_start = vec2d_add(p->direction, vec2d_scalar_product(p->cam_plane, camera_x)); p->rays[col] = raycaster_cast(p->position, ray_start, bound_check, p->meta); // printf("wall x: %f\n", p->rays[col].wall_x); From d1c8fe0687243ab7214d06ebde7cd39747debc8c Mon Sep 17 00:00:00 2001 From: joppe Date: Sat, 20 Jan 2024 01:19:24 +0100 Subject: [PATCH 5/7] Swapping texture for memory allignment kinda works. --- Makefile | 2 +- include/meta.h | 3 ++- src/game/game.c | 30 +++++++++++++++++++++++++++++- src/game/render_viewport.c | 2 +- src/renderer/pixel_picker.c | 4 ++-- test_maps/valid_tex.cub | 10 +++++----- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 8f05cea..fb11456 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ RUN_CMD := ./$(NAME) test_maps/valid_tex.cub # CFLAGS += -Wall -Wextra -Werror # CFLAGS += -Wall -Wextra -# CFLAGS += -g -fsanitize=address +CFLAGS += -g -fsanitize=address # CFLAGS += -g # CFLAGS += -Ofast -flto -march=native diff --git a/include/meta.h b/include/meta.h index fa10e2d..9a3b0b1 100644 --- a/include/meta.h +++ b/include/meta.h @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/01 20:07:37 by jboeve#+##+# */ -/* Updated: 2024/01/20 00:47:06 by joppe ######## odam.nl */ +/* Updated: 2024/01/20 01:14:53 by joppe ######## odam.nl */ /**/ /* ************************************************************************** */ @@ -56,6 +56,7 @@ #define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 720 +#define BPP 4 // Game diff --git a/src/game/game.c b/src/game/game.c index e506713..a30b409 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */ -/* Updated: 2024/01/17 13:04:46 by jboeve ######## odam.nl */ +/* Updated: 2024/01/20 01:19:00 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,7 +14,9 @@ #include "meta.h" #include "timer.h" #include "vector.h" +#include #include +#include #include #include "test_utils.h" @@ -54,12 +56,38 @@ void set_player_start_position(t_player *p, char dir) p->position.y += 0.5; } + + + +void swap_tex(mlx_texture_t *tex) +{ + assert(tex->width == tex->height); + + for (size_t x = 0; x < tex->width; x++) + { + for (size_t y = 0; y < x; y++) + { + uint32_t pixel = tex->pixels[(tex->width * y + x) * BPP]; + tex->pixels[(tex->width * y + x) * BPP] = tex->pixels[(tex->width * x + y) * BPP]; + tex->pixels[(tex->width * x + y) * BPP] = pixel; + } + } +} + void game_init(t_meta *meta) { t_player* const p = &meta->player; timer_init(&meta->update_timer, mlx_get_time); timer_start(&meta->update_timer); + + swap_tex(meta->attributes.n.tex); + swap_tex(meta->attributes.s.tex); + swap_tex(meta->attributes.e.tex); + swap_tex(meta->attributes.w.tex); + + + p->meta = meta; set_player_start_position(&meta->player, meta->map.player_start_dir); player_move(p, (t_vec2d) {0.0, 0.0}); // to draw the image at the start (?) diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index a1ec418..7a5bdf8 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */ -/* Updated: 2024/01/18 12:27:01 by jboeve ######## odam.nl */ +/* Updated: 2024/01/20 01:18:16 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/pixel_picker.c b/src/renderer/pixel_picker.c index 50d1a47..9fe2150 100644 --- a/src/renderer/pixel_picker.c +++ b/src/renderer/pixel_picker.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */ -/* Updated: 2024/01/17 15:42:56 by jboeve ######## odam.nl */ +/* Updated: 2024/01/20 01:16:30 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -50,7 +50,7 @@ uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y) int32_t index; // printf("x: %u | y: %u\n", x, y); - index = y * texture->width * 4 + x * 4; + index = x * texture->width * BPP + y * BPP; // printf("index: %d\n", index); r = (uint32_t)texture->pixels[index]; g = (uint32_t)texture->pixels[index + 1]; diff --git a/test_maps/valid_tex.cub b/test_maps/valid_tex.cub index 823e33f..113ecd3 100644 --- a/test_maps/valid_tex.cub +++ b/test_maps/valid_tex.cub @@ -6,12 +6,12 @@ F 255,255,255 C 12, 12, 12 11111111111111 +1000010001 +1000N10001 +1000010001 1000000001 -1000N00001 1000000001 -1000000001 -1000000001 -100000000111 -100000000001 +111000000111 +100000100001 10000000011 1111111111 From 1725e337db8bde61add35acbc2522ea4bc4f5821 Mon Sep 17 00:00:00 2001 From: Joppe Boeve Date: Thu, 25 Jan 2024 13:22:00 +0100 Subject: [PATCH 6/7] Change collsion distance --- src/game/player.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/game/player.c b/src/game/player.c index 1ae663d..c5af66e 100644 --- a/src/game/player.c +++ b/src/game/player.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */ -/* Updated: 2024/01/20 00:52:20 by joppe ######## odam.nl */ +/* Updated: 2024/01/25 13:21:42 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,20 +58,21 @@ void player_move(t_player *p, t_vec2d transform) { t_ray r = raycaster_cast(p->position, vec2d_normalize(transform), bound_check, p->meta); - if (r.length > 0.5) + if (r.length > 0.4) p->position = vec2d_add(p->position, transform); else { const int comp = (r.hit_side == SIDE_N || r.hit_side == SIDE_S); - const t_vec2d normal = {comp, !comp}; + const t_vec2d normal = {comp, !comp}; // 1, 0 // 0, 1 const double dot_product = vec2d_dot_product(transform, normal); + t_vec2d delta_pos; delta_pos.x = transform.x - normal.x * dot_product; delta_pos.y = transform.y - normal.y * dot_product; r = raycaster_cast(p->position, vec2d_normalize(transform), bound_check, p->meta); - if (r.length > 0.3) + if (r.length > 0.35) { p->position.x += delta_pos.x; p->position.y += delta_pos.y; From 07d83046aaf5dff0d090832e6c6d696ee021187a Mon Sep 17 00:00:00 2001 From: Joppe Boeve Date: Thu, 25 Jan 2024 13:22:33 +0100 Subject: [PATCH 7/7] Remove tex swap --- src/game/game.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/game/game.c b/src/game/game.c index a30b409..ec96f83 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */ -/* Updated: 2024/01/20 01:19:00 by joppe ######## odam.nl */ +/* Updated: 2024/01/25 13:22:31 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -80,14 +80,6 @@ void game_init(t_meta *meta) timer_init(&meta->update_timer, mlx_get_time); timer_start(&meta->update_timer); - - swap_tex(meta->attributes.n.tex); - swap_tex(meta->attributes.s.tex); - swap_tex(meta->attributes.e.tex); - swap_tex(meta->attributes.w.tex); - - - p->meta = meta; set_player_start_position(&meta->player, meta->map.player_start_dir); player_move(p, (t_vec2d) {0.0, 0.0}); // to draw the image at the start (?)