diff --git a/include/meta.h b/include/meta.h index 8f3cfaf..66d981c 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/02 19:51:20 by joppe ######## odam.nl */ +/* Updated: 2024/01/02 21:52:00 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -72,7 +72,7 @@ #define COLOR_PLAYER 0xFFFFFFFF // #define FOV 0.66 -#define FOV 0.80 +#define FOV 0.85 typedef bool (t_ray_hitfunc) (void *p, uint32_t x, uint32_t y); typedef struct s_meta t_meta; @@ -174,8 +174,6 @@ void draw_rect(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t widt 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); -// test_utils.c -void print_cell(t_cell_type cell); // keys.c void keys_update(mlx_key_data_t keydata, void *param); @@ -187,9 +185,6 @@ void cube_put_pixel(mlx_image_t* image, uint32_t x, uint32_t y, uint32_t color) // raycaster.c t_ray raycaster_cast(t_meta *meta, t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit); -// test_utils.c -void print_map(char *map, uint32_t w, uint32_t h); - // colors.c int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a); int32_t find_wall_color(t_side side); diff --git a/include/test_utils.h b/include/test_utils.h index 09027e1..3ba1dc5 100644 --- a/include/test_utils.h +++ b/include/test_utils.h @@ -1 +1,27 @@ -void print_map_cell(t_cell_type *level, uint32_t w, uint32_t h); \ No newline at end of file +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* test_utils.h :+: :+: */ +/* +:+ */ +/* By: joppe +#+ */ +/* +#+ */ +/* Created: 2024/01/02 19:56:05 by joppe #+# #+# */ +/* Updated: 2024/01/02 21:08:34 by joppe ######## odam.nl */ +/* */ +/* ************************************************************************** */ + + + +#ifndef TEST_UTILS_H +#define TEST_UTILS_H + +#include "meta.h" + + +// test_utils.c +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); + +#endif // !TEST_UTILS_H diff --git a/include/vector.h b/include/vector.h index 0aa61c7..3e71c04 100644 --- a/include/vector.h +++ b/include/vector.h @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2024/01/02 19:07:15 by joppe #+# #+# */ -/* Updated: 2024/01/02 19:07:43 by joppe ######## odam.nl */ +/* Updated: 2024/01/02 21:26:35 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -33,12 +33,14 @@ typedef struct s_vec2u { // vec2d_utils.c -t_vec2d vec2d_add(t_vec2d v1, t_vec2d v2); -t_vec2d vec2d_scalar_product(t_vec2d vec, double scalar); void print_vec2d(char *str, t_vec2d vector); +t_vec2d vec2d_scalar_product(t_vec2d vec, double scalar); t_vec2d vec2d_rotate(t_vec2d old, double radiant); +t_vec2d vec2d_add(t_vec2d v1, t_vec2d v2); t_vec2d vec2u_to_vec2d(t_vec2u v); double deg_to_rad(float deg); +t_vec2d vec2d_mul(t_vec2d v1, t_vec2d v2); +t_vec2d vec2d_normalize(t_vec2d vec); #endif // !VECTOR_H diff --git a/src/cub3d.c b/src/cub3d.c index 557efb7..2ec7b1b 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/07 15:36:26 by jboeve #+# #+# */ -/* Updated: 2024/01/02 18:58:50 by joppe ######## odam.nl */ +/* Updated: 2024/01/02 21:50:31 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/game/game.c b/src/game/game.c index 4963f6d..dfa7a83 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/08 22:35:05 by joppe #+# #+# */ -/* Updated: 2024/01/02 19:49:20 by joppe ######## odam.nl */ +/* Updated: 2024/01/02 20:58:27 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #include "vector.h" #include #include +#include "test_utils.h" void set_player_start_position(t_player *p, char dir) { diff --git a/src/game/keys.c b/src/game/keys.c index a6523ad..bdd3cfe 100644 --- a/src/game/keys.c +++ b/src/game/keys.c @@ -6,41 +6,70 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/12/15 14:05:30 by jboeve #+# #+# */ -/* Updated: 2024/01/02 19:37:53 by joppe ######## odam.nl */ +/* Updated: 2024/01/02 22:01:41 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ #include "MLX42/MLX42.h" +#include "libft.h" #include "meta.h" +#include "vector.h" #include #include -void keys_handle(t_meta *meta, double delta_time) +static void keys_handle_move(t_meta *meta, double delta_time) { t_player* const p = &meta->player; float speed = PLAYER_MOVE_SPEED * delta_time; - float rotate_speed = PLAYER_ROTATE_SPEED * delta_time; + t_vec2d trans; + bool pressed; + pressed = false; + ft_bzero(&trans, sizeof(t_vec2d)); if (mlx_is_key_down(meta->mlx, MLX_KEY_LEFT_SHIFT)) speed *= PLAYER_RUN_MODIFIER; - if (mlx_is_key_down(meta->mlx, MLX_KEY_LEFT_CONTROL)) - rotate_speed /= PLAYER_ROTATE_MODIFIER; - if (mlx_is_key_down(meta->mlx, MLX_KEY_ESCAPE)) - mlx_close_window(meta->mlx); if (mlx_is_key_down(meta->mlx, MLX_KEY_W)) - player_move(p, (t_vec2d) {p->direction.x * speed, p->direction.y * speed}); + { + pressed = true; + trans = vec2d_add(trans, (t_vec2d) {p->direction.x * speed, p->direction.y * speed}); + } if (mlx_is_key_down(meta->mlx, MLX_KEY_S)) - player_move(p, (t_vec2d) {p->direction.x * -speed, p->direction.y * -speed}); + { + pressed = true; + trans = vec2d_add(trans, (t_vec2d) {p->direction.x * -speed, p->direction.y * -speed}); + } if (mlx_is_key_down(meta->mlx, MLX_KEY_A)) - player_move(p, (t_vec2d) {(vec2d_rotate(p->direction, PI / 2).x) * -speed, (vec2d_rotate(p->direction, PI / 2).y) * -speed}); + { + 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 (mlx_is_key_down(meta->mlx, MLX_KEY_D)) - player_move(p, (t_vec2d) {(vec2d_rotate(p->direction, PI / 2).x) * speed, (vec2d_rotate(p->direction, PI / 2).y) * speed}); + { + 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); +} + +void keys_handle(t_meta *meta, double delta_time) +{ + t_player* const p = &meta->player; + float rotate_speed = PLAYER_ROTATE_SPEED * delta_time; + + if (mlx_is_key_down(meta->mlx, MLX_KEY_LEFT_CONTROL)) + rotate_speed /= PLAYER_ROTATE_MODIFIER; + if (mlx_is_key_down(meta->mlx, MLX_KEY_ESCAPE)) + mlx_close_window(meta->mlx); if (mlx_is_key_down(meta->mlx, MLX_KEY_Q)) player_turn(p, -rotate_speed); if (mlx_is_key_down(meta->mlx, MLX_KEY_E)) player_turn(p, rotate_speed); + keys_handle_move(meta, delta_time); } + void mouse_hook(double xpos, double ypos, void *param) { t_meta *meta = param; @@ -48,6 +77,8 @@ void mouse_hook(double xpos, double ypos, void *param) const float rotate_speed = 0.200f; static bool going_right = false; + (void) ypos; + static double time_old = 0; if (time_old == 0) time_old = mlx_get_time(); @@ -67,7 +98,7 @@ void mouse_hook(double xpos, double ypos, void *param) delta_time = mlx_get_time() - time_old; speed = delta_time * fabs(x_old - xpos) * rotate_speed; } - player_turn(meta, speed); + player_turn(&meta->player, speed); going_right = true; } else @@ -78,7 +109,7 @@ void mouse_hook(double xpos, double ypos, void *param) delta_time = mlx_get_time() - time_old; speed = delta_time * fabs(x_old - xpos) * rotate_speed; } - player_turn(meta, -speed); + player_turn(&meta->player, -speed); going_right = false; } // printf("delta_time [%lf]\n", delta_time); diff --git a/src/game/player.c b/src/game/player.c index 020e5ac..5e39f79 100644 --- a/src/game/player.c +++ b/src/game/player.c @@ -6,13 +6,15 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/10 02:25:34 by joppe #+# #+# */ -/* Updated: 2024/01/02 19:37:31 by joppe ######## odam.nl */ +/* Updated: 2024/01/02 22:04:30 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ #include "MLX42/MLX42.h" #include "libft.h" +#include "test_utils.h" #include "meta.h" +#include "vector.h" #include #include #include @@ -32,12 +34,15 @@ bool bound_check(void *param, uint32_t x, uint32_t y) void player_move(t_player *p, t_vec2d transform) { - t_vec2d new_position; + t_vec2d new_position; new_position.x = (p->position.x + (transform.x)); new_position.y = (p->position.y + (transform.y)); - // TODO Use raycast to check if we can move. - if (!bound_check(p->meta, new_position.x, new_position.y)) + + t_ray r = raycaster_cast(p->meta, p->position, vec2d_normalize(transform), bound_check); + // print_ray("bound_ray", &r); + + if (r.length > .5) { p->position.x += transform.x; p->position.y += transform.y; diff --git a/src/game/raycaster.c b/src/game/raycaster.c index 89f1bc7..f3de3c1 100644 --- a/src/game/raycaster.c +++ b/src/game/raycaster.c @@ -6,12 +6,13 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/12/15 15:20:09 by jboeve #+# #+# */ -/* Updated: 2024/01/02 18:55:06 by joppe ######## odam.nl */ +/* Updated: 2024/01/02 22:07:46 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ #include "meta.h" #include +#include inline static t_vec2d calculate_delta_dist(t_vec2d ray_direction) { @@ -65,18 +66,18 @@ inline static double calculate_ray_length(t_side hit_side, \ } inline static t_side ray_move(t_vec2d *side_dist, t_vec2d *delta_dist, \ - t_vec2d *step_size, t_vec2d *map_pos) + t_vec2d step_size, t_vec2d *map_pos) { if (side_dist->x < side_dist->y) { side_dist->x += delta_dist->x; - map_pos->x += step_size->x; + map_pos->x += step_size.x; return (HIT_NS); } else { side_dist->y += delta_dist->y; - map_pos->y += step_size->y; + map_pos->y += step_size.y; return (HIT_EW); } } @@ -94,12 +95,21 @@ t_ray raycaster_cast(t_meta *meta, t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit) delta_dist = calculate_delta_dist(dir); side_dist = calculate_side_dist(dir, pp, map_pos, delta_dist); step_size = calculate_step_size(dir); - while (1) + size_t limit = 25; + while (limit) { - ray.hit_side = ray_move(&side_dist, &delta_dist, &step_size, &map_pos); + ray.hit_side = ray_move(&side_dist, &delta_dist, step_size, &map_pos); + // print_vec2d("side_dist", side_dist); if (hit(meta, map_pos.x, map_pos.y)) + { + // TODO Get hit angle. break; + } + limit--; } + if (!limit) + UNIMPLEMENTED("Raycaster limit reached!"); ray.length = calculate_ray_length(ray.hit_side, side_dist, delta_dist); + ray.direction = dir; return (ray); } diff --git a/src/test_utils.c b/src/test_utils.c index 1ab8ced..8b6d051 100644 --- a/src/test_utils.c +++ b/src/test_utils.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/10 16:32:18 by joppe #+# #+# */ -/* Updated: 2023/12/20 18:34:36 by jboeve ######## odam.nl */ +/* Updated: 2024/01/02 21:23:42 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,6 +22,15 @@ const char *CELL_NAMES[] = { [MAP_SPACE] = "MAP_SPACE", }; +void print_ray(const char *s, const t_ray *r) +{ + char *side_text; + if (r->hit_side == HIT_NS) + side_text = "NS"; + else + side_text = "EW"; + printf("[%s] | ray_direction [%lf][%lf] | length [%lf] | hit_side [%s]\n", s, r->direction.x, r->direction.y, r->length, side_text); +} void print_cell(t_cell_type cell) { diff --git a/src/utils/vec2d_utils.c b/src/utils/vec2d_utils.c index 06cb788..306e4c5 100644 --- a/src/utils/vec2d_utils.c +++ b/src/utils/vec2d_utils.c @@ -6,11 +6,12 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/12/14 18:30:23 by jboeve #+# #+# */ -/* Updated: 2024/01/02 17:43:51 by joppe ######## odam.nl */ +/* Updated: 2024/01/02 21:26:28 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ #include "meta.h" +#include "vector.h" #include t_vec2d vec2d_add(t_vec2d v1, t_vec2d v2) @@ -27,6 +28,11 @@ t_vec2d vec2d_scalar_product(t_vec2d vec, double scalar) return ((t_vec2d){vec.x * scalar, vec.y * scalar}); } +t_vec2d vec2d_mul(t_vec2d v1, t_vec2d v2) +{ + return ((t_vec2d){v1.x * v2.x, v1.y * v2.y}); +} + void print_vec2d(char *str, t_vec2d vector) { printf("%s X: %lf | Y: %lf\n", str, vector.x, vector.y); @@ -51,9 +57,8 @@ double deg_to_rad(float deg) return deg * (PI / 180); } - -// t_vec2f vec2f_normalize(t_vec2f vec) -// { -// float m = sqrt(vec[VEC_X] * vec[VEC_X] + vec[VEC_Y] * vec[VEC_Y]); -// return (vec / m); -// } +t_vec2d vec2d_normalize(t_vec2d vec) +{ + float m = sqrt(vec.x * vec.x + vec.y * vec.y); + return ((t_vec2d) {vec.x / m, vec.y / m}); +}