From 4584e96d7044f10352076c318003baea950e9ef7 Mon Sep 17 00:00:00 2001 From: Joppe Boeve Date: Sat, 18 Nov 2023 15:43:35 +0100 Subject: [PATCH 1/3] Fix weird projection of rays --- include/meta.h | 11 +++-------- include/vector.h | 3 ++- src/main.c | 2 +- src/player.c | 6 +++--- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/include/meta.h b/include/meta.h index 972036e..c6c36b1 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: 2023/11/18 15:39:16 by jboeve ######## odam.nl */ +/* Updated: 2023/11/18 14:44:35 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,11 +16,6 @@ #include #include #include -#include -#include -#include -#include - #include "timer.h" #include "vector.h" #include "MLX42/MLX42.h" @@ -40,7 +35,7 @@ #define PI 3.1415926535 // Window settings -#define WINDOW_WIDTH 1024 +#define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 720 #define WINDOW_TITLE "Gammoe" @@ -52,7 +47,7 @@ #define PLAYER_WIDTH 16 #define PLAYER_HEIGHT 16 -#define PLAYER_RAY_COUNT 360 +#define PLAYER_RAY_COUNT 180 #define PLAYER_WALK_SPEED 15 #define PLAYER_ROTATE_SPEED 5 #define PLAYER_RUN_MODIFIER 2.5 diff --git a/include/vector.h b/include/vector.h index da91eea..85205b8 100644 --- a/include/vector.h +++ b/include/vector.h @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/11 03:51:14 by joppe #+# #+# */ -/* Updated: 2023/11/11 03:58:12 by joppe ######## odam.nl */ +/* Updated: 2023/11/18 13:21:55 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #define VECTOR_H #include +#include #define VEC_X 0 diff --git a/src/main.c b/src/main.c index 9179bd6..117a685 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/07 15:28:54 by jboeve #+# #+# */ -/* Updated: 2023/11/11 03:58:48 by joppe ######## odam.nl */ +/* Updated: 2023/11/18 14:38:50 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/player.c b/src/player.c index 8a10b7e..08bea90 100644 --- a/src/player.c +++ b/src/player.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/10 02:25:34 by joppe #+# #+# */ -/* Updated: 2023/11/13 21:31:05 by joppe ######## odam.nl */ +/* Updated: 2023/11/18 14:44:25 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -65,7 +65,7 @@ static t_ray raycast(t_map *map, t_vec2f start, t_vec2f angle, size_t depth) // Draws a line until we encounter a wall void player_raycast(t_player *p) { - const size_t depth = 500; + const size_t depth = 100; t_vec2f dir; size_t i; @@ -73,7 +73,7 @@ void player_raycast(t_player *p) while (i < PLAYER_RAY_COUNT) { dir = vec2f_normalize(vec2f_rotate2d(p->angle + deg_to_rad(i))); - p->rays[i] = raycast(&p->meta->map, p->position, p->direction * dir, depth); + p->rays[i] = raycast(&p->meta->map, p->position, dir, depth); i++; } } From 2ee9fde9b2b85d99061eef67b0d514c6a0e97e04 Mon Sep 17 00:00:00 2001 From: Joppe Boeve Date: Sat, 18 Nov 2023 17:39:41 +0100 Subject: [PATCH 2/3] More raycasting bby?!??! --- include/meta.h | 22 +++++++++++----------- src/game.c | 6 +++--- src/main.c | 2 +- src/player.c | 14 +++++++------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/include/meta.h b/include/meta.h index c6c36b1..056020d 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: 2023/11/18 14:44:35 by jboeve ######## odam.nl */ +/* Updated: 2023/11/18 15:49:12 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -47,7 +47,7 @@ #define PLAYER_WIDTH 16 #define PLAYER_HEIGHT 16 -#define PLAYER_RAY_COUNT 180 +#define PLAYER_RAY_COUNT 90 #define PLAYER_WALK_SPEED 15 #define PLAYER_ROTATE_SPEED 5 #define PLAYER_RUN_MODIFIER 2.5 @@ -97,7 +97,7 @@ typedef struct s_player { t_vec2f direction; t_vec2f beam; t_ray rays[PLAYER_RAY_COUNT]; - float angle; + float angle_rad; } t_player; typedef struct s_map { @@ -118,20 +118,20 @@ typedef struct s_meta { } t_meta; // cub3d.c -int cub3d(int argc, char *argv[]); +int cub3d(int argc, char *argv[]); // game.c -void game_init(t_meta *meta); -void game_loop(void* param); +void game_init(t_meta *meta); +void game_loop(void* param); // player.c -void player_move(t_player *p, t_vec2f transform); -void player_look(t_player *p, double angle); -void player_raycast(t_player *p); +void player_move(t_player *p, t_vec2f transform); +void player_look(t_player *p, double angle); +void player_raycast(t_player *p); // input.c -void key_hook(mlx_key_data_t keydata, void* param); -void cursor_hook(double xpos, double ypos, void* param); +void key_hook(mlx_key_data_t keydata, void* param); +void cursor_hook(double xpos, double ypos, void* param); // render.c t_vec2i render_get_draw_offset(); diff --git a/src/game.c b/src/game.c index 03fdef5..6efb074 100644 --- a/src/game.c +++ b/src/game.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/08 22:35:05 by joppe #+# #+# */ -/* Updated: 2023/11/12 20:03:30 by joppe ######## odam.nl */ +/* Updated: 2023/11/18 15:46:58 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -84,9 +84,9 @@ static void game_update(t_meta *meta, double time_delta) player_move(p, -move * p->direction); if (mlx_is_key_down(meta->mlx, MLX_KEY_A)) - player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle + (3 * PI / 2))) * move); + player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle_rad + (3 * PI / 2))) * move); if (mlx_is_key_down(meta->mlx, MLX_KEY_D)) - player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle + (PI / 2))) * move); + player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle_rad + (PI / 2))) * move); if (mlx_is_key_down(meta->mlx, MLX_KEY_Q)) player_look(p, -rotate); diff --git a/src/main.c b/src/main.c index 117a685..c4d551f 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/07 15:28:54 by jboeve #+# #+# */ -/* Updated: 2023/11/18 14:38:50 by jboeve ######## odam.nl */ +/* Updated: 2023/11/18 17:37:33 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/player.c b/src/player.c index 08bea90..9657b52 100644 --- a/src/player.c +++ b/src/player.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/10 02:25:34 by joppe #+# #+# */ -/* Updated: 2023/11/18 14:44:25 by jboeve ######## odam.nl */ +/* Updated: 2023/11/18 15:48:06 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -30,11 +30,11 @@ void player_look(t_player *p, double angle) { const uint32_t len = 50; - p->angle = fmod(p->angle + angle, 2 * PI); - if (p->angle < 0) - p->angle += 2 * PI; + p->angle_rad = fmod(p->angle_rad + angle, 2 * PI); + if (p->angle_rad < 0) + p->angle_rad += 2 * PI; - p->direction = vec2f_normalize(vec2f_rotate2d(p->angle)); + p->direction = vec2f_normalize(vec2f_rotate2d(p->angle_rad)); p->beam = p->position + p->direction * (t_vec2f) {len, len}; player_raycast(p); @@ -65,14 +65,14 @@ static t_ray raycast(t_map *map, t_vec2f start, t_vec2f angle, size_t depth) // Draws a line until we encounter a wall void player_raycast(t_player *p) { - const size_t depth = 100; + const size_t depth = 500; t_vec2f dir; size_t i; i = 0; while (i < PLAYER_RAY_COUNT) { - dir = vec2f_normalize(vec2f_rotate2d(p->angle + deg_to_rad(i))); + dir = vec2f_normalize(vec2f_rotate2d(p->angle_rad + deg_to_rad(i) - (PLAYER_RAY_COUNT / 2))); p->rays[i] = raycast(&p->meta->map, p->position, dir, depth); i++; } From 11a8037ac002af9473036b2945117088d853e208 Mon Sep 17 00:00:00 2001 From: Joppe Boeve Date: Sat, 18 Nov 2023 20:57:02 +0100 Subject: [PATCH 3/3] Can draw weird ass lines --- include/meta.h | 13 +++++++++---- src/draw.c | 8 ++++---- src/game.c | 6 ++---- src/main.c | 4 +++- src/player.c | 4 ++-- src/render.c | 42 ++++++++++++++++++++++++++++++++---------- 6 files changed, 52 insertions(+), 25 deletions(-) diff --git a/include/meta.h b/include/meta.h index 056020d..d90ff86 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: 2023/11/18 15:49:12 by jboeve ######## odam.nl */ +/* Updated: 2023/11/18 20:45:26 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -35,10 +35,14 @@ #define PI 3.1415926535 // Window settings -#define WINDOW_WIDTH 1280 -#define WINDOW_HEIGHT 720 +#define WINDOW_WIDTH 1920 +#define WINDOW_HEIGHT 1080 #define WINDOW_TITLE "Gammoe" +#define PLAYER_VIEWPORT_X 720 +#define PLAYER_VIEWPORT_Y 512 +#define PLAYER_VIEWPORT_WALL_WIDTH 8 + #define TICK_RATE (1.0 / 60.0) // TODO Move all this stuff to some kind of game.h @@ -135,6 +139,7 @@ void cursor_hook(double xpos, double ypos, void* param); // render.c t_vec2i render_get_draw_offset(); +void render_player_view(mlx_image_t *image, t_player *p); void render_player(mlx_image_t *image, t_player *p); void render_clear_bg(mlx_image_t *image); void render_map_grid(mlx_image_t *image, t_map *m); @@ -143,7 +148,7 @@ void render_map_grid(mlx_image_t *image, t_map *m); t_cell_type map_get_cell_type(t_map *m, t_vec2f pos); // draw.c -void draw_square(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color); +void draw_rect(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color); void draw_line(mlx_image_t *image, t_vec2i start, t_vec2i end, t_rgba c); void draw_put_pixel(mlx_image_t* image, uint32_t x, uint32_t y, uint32_t color); diff --git a/src/draw.c b/src/draw.c index 5524cb1..b7bfb32 100644 --- a/src/draw.c +++ b/src/draw.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/09 01:32:57 by joppe #+# #+# */ -/* Updated: 2023/11/10 01:58:06 by joppe ######## odam.nl */ +/* Updated: 2023/11/18 20:28:45 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,16 +18,16 @@ #include // TODO Struct that contains all this info because well. tHe nORm -void draw_square(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color) +void draw_rect(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color) { size_t x; size_t y; y = 0; - while (y < width) + while (y < height) { x = 0; - while (x < height) + while (x < width) { draw_put_pixel(image, x_pos + x, y_pos + y, color); x++; diff --git a/src/game.c b/src/game.c index 6efb074..b0275dc 100644 --- a/src/game.c +++ b/src/game.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/08 22:35:05 by joppe #+# #+# */ -/* Updated: 2023/11/18 15:46:58 by jboeve ######## odam.nl */ +/* Updated: 2023/11/18 20:39:45 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -62,9 +62,6 @@ void game_init(t_meta *meta) p->position[VEC_X] = (float) meta->map.width * CELL_WIDTH / 2; p->position[VEC_Y] = (float) meta->map.height * CELL_WIDTH/ 2; - // p->position[VEC_X] = 0; - // p->position[VEC_Y] = 0; - player_look(p, deg_to_rad(180.0f)); } @@ -118,5 +115,6 @@ void game_loop(void* param) render_clear_bg(meta->image); render_map_grid(meta->image, &meta->map); + render_player_view(meta->image, &meta->player); render_player(meta->image, &meta->player); } diff --git a/src/main.c b/src/main.c index c4d551f..e686978 100644 --- a/src/main.c +++ b/src/main.c @@ -6,11 +6,13 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/07 15:28:54 by jboeve #+# #+# */ -/* Updated: 2023/11/18 17:37:33 by jboeve ######## odam.nl */ +/* Updated: 2023/11/18 20:55:05 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ #include "meta.h" +#include "vector.h" +#include int main(int argc, char *argv[]) { diff --git a/src/player.c b/src/player.c index 9657b52..1455aa1 100644 --- a/src/player.c +++ b/src/player.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/10 02:25:34 by joppe #+# #+# */ -/* Updated: 2023/11/18 15:48:06 by jboeve ######## odam.nl */ +/* Updated: 2023/11/18 20:45:45 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -72,7 +72,7 @@ void player_raycast(t_player *p) i = 0; while (i < PLAYER_RAY_COUNT) { - dir = vec2f_normalize(vec2f_rotate2d(p->angle_rad + deg_to_rad(i) - (PLAYER_RAY_COUNT / 2))); + dir = vec2f_normalize(vec2f_rotate2d(p->angle_rad + deg_to_rad(i) - (deg_to_rad(PLAYER_RAY_COUNT) / 2))); p->rays[i] = raycast(&p->meta->map, p->position, dir, depth); i++; } diff --git a/src/render.c b/src/render.c index 593d9fd..6d192fc 100644 --- a/src/render.c +++ b/src/render.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/08 23:14:20 by joppe #+# #+# */ -/* Updated: 2023/11/13 21:24:56 by joppe ######## odam.nl */ +/* Updated: 2023/11/18 20:56:34 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,15 +41,15 @@ t_vec2i render_get_draw_offset() void draw_cell(mlx_image_t *image, t_map *m, uint32_t cell_x, uint32_t cell_y) { - const size_t x_offset = (cell_x * CELL_WIDTH) + cell_x + DRAW_OFFSET[VEC_X]; - const size_t y_offset = (cell_y * CELL_HEIGHT) + cell_y + DRAW_OFFSET[VEC_Y]; + // const size_t x_offset = (cell_x * CELL_WIDTH) + cell_x + DRAW_OFFSET[VEC_X]; + // const size_t y_offset = (cell_y * CELL_HEIGHT) + cell_y + DRAW_OFFSET[VEC_Y]; - // const size_t x_offset = (cell_x * CELL_WIDTH) + DRAW_OFFSET[VEC_X]; - // const size_t y_offset = (cell_y * CELL_HEIGHT) + DRAW_OFFSET[VEC_Y]; + const size_t x_offset = (cell_x * CELL_WIDTH) + DRAW_OFFSET[VEC_X]; + const size_t y_offset = (cell_y * CELL_HEIGHT) + DRAW_OFFSET[VEC_Y]; const t_cell_type cell = (m->level[(cell_y * m->width) + cell_x]); - draw_square(image, x_offset, y_offset, CELL_WIDTH, CELL_HEIGHT, CELL_COLORS[cell].value); + draw_rect(image, x_offset, y_offset, CELL_WIDTH, CELL_HEIGHT, CELL_COLORS[cell].value); } // The player is essentially just a single point/pixel, around which we draw a square with the "player point" in its center. @@ -60,20 +60,42 @@ void render_player(mlx_image_t *image, t_player *p) draw_pos[VEC_Y] -= ((float) PLAYER_HEIGHT / 2); // Draw the player square. - draw_square(image, draw_pos[VEC_X], draw_pos[VEC_Y], + draw_rect(image, draw_pos[VEC_X], draw_pos[VEC_Y], PLAYER_WIDTH, PLAYER_HEIGHT, COLOR_PLAYER); + size_t i = 0; + while (i < PLAYER_RAY_COUNT) + { + draw_line(image, vec2f_to_vec2i(p->rays[i].start) + DRAW_OFFSET, + vec2f_to_vec2i(p->rays[i].end) + DRAW_OFFSET, + (t_rgba) {0xFF1500FF}); + i++; + } + // Draw the player look direction. draw_line(image, vec2f_to_vec2i(p->position) + DRAW_OFFSET, vec2f_to_vec2i(p->beam) + DRAW_OFFSET, (t_rgba) {0x00FF00FF}); +} +float ray_distance(t_ray r) +{ + t_vec2f diff = r.end - r.start; + return sqrtf((r.end[VEC_X] - r.start[VEC_X]) * (r.end[VEC_X] - r.start[VEC_X]) + (r.end[VEC_Y] - r.start[VEC_Y]) * (r.end[VEC_Y] - r.start[VEC_Y])); +} + +void render_player_view(mlx_image_t *image, t_player *p) +{ size_t i = 0; + t_ray tmp; + int height = 1; + const int x_offset = PLAYER_VIEWPORT_X + 1; + while (i < PLAYER_RAY_COUNT) { - draw_line(image, vec2f_to_vec2i(p->rays[i].start) + DRAW_OFFSET, - vec2f_to_vec2i(p->rays[i].end) + DRAW_OFFSET, - (t_rgba) {0xFF1500FF}); + tmp = p->rays[i]; + height = 500 - ray_distance(tmp); + draw_rect(image, x_offset + (PLAYER_VIEWPORT_WALL_WIDTH * i), PLAYER_VIEWPORT_Y, PLAYER_VIEWPORT_WALL_WIDTH, height, 0xfc7b25ff); i++; } }