Raycasting done ! Problem of FOV

This commit is contained in:
Etienne Rey-bethbeder 2023-05-03 17:48:36 +02:00
parent 3ad6814405
commit ebe8b84f78
12 changed files with 203 additions and 110 deletions

View File

@ -1,6 +1,6 @@
SRCS_GAME := main.c game/game.c game/init.c game/manage.c \ SRCS_GAME := main.c game/game.c game/init.c game/manage.c \
game/manage_keys.c game/raycasting.c game/manage_keys.c game/raycasting.c game/dda.c
SRCS_MAP := parsing.c parsing_header.c parsing_header2.c parsing_meta.c parsing_body.c SRCS_MAP := #parsing.c parsing_header.c parsing_header2.c parsing_meta.c parsing_body.c
SRCS_MAP := $(addprefix map/, $(SRCS_MAP)) SRCS_MAP := $(addprefix map/, $(SRCS_MAP))
SRCS := ${SRCS_MAP} \ SRCS := ${SRCS_MAP} \

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:34:04 by erey-bet #+# #+# */ /* Created: 2023/04/26 12:34:04 by erey-bet #+# #+# */
/* Updated: 2023/04/27 14:41:37 by erey-bet ### ########.fr */ /* Updated: 2023/05/03 14:51:05 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,7 +37,7 @@ typedef struct s_map
} t_map; } t_map;
/*INIT*/ /*INIT*/
t_map *map_parsing(char *path); t_map *map_parsing(t_map *map, char *path);
int start_game(t_map *map); int start_game(t_map map);
#endif #endif

Binary file not shown.

View File

@ -6,48 +6,53 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 15:49:00 by erey-bet #+# #+# */ /* Created: 2023/05/02 15:49:00 by erey-bet #+# #+# */
/* Updated: 2023/05/02 16:58:31 by erey-bet ### ########.fr */ /* Updated: 2023/05/03 17:37:31 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game.h"
double ft_abs(double nb)
{
if (nb < 0)
return (-nb);
return (nb);
}
void init_dda(t_game *game, t_dda *dda) void init_dda(t_game *game, t_dda *dda)
{ {
t_ray *ray t_ray *ray;
t_ply *ply;
ray = game->ray; ray = &game->ray;
dda->map_x = (int)game->ply->pos_x; ply = &game->ply;
dda->map_y = (int)game->ply->pos_y; dda->map_x = (int)ply->pos_x;
dda->map_y = (int)ply->pos_y;
if (ray->dir_x == 0) if (ray->dir_x == 0)
dda->delta_dist_x = 1e30; dda->delta_dist_x = 1e30;
else else
dda->delta_dist_x = abs(1 / ray->dir_x); dda->delta_dist_x = ft_abs(1 / ray->dir_x);
if (ray->dir_y == 0) if (ray->dir_y == 0)
dda->delta_dist_y = 1e30; dda->delta_dist_y = 1e30;
else else
dda->delta_dist_y = abs(1 / ray->dir_y); dda->delta_dist_y = ft_abs(1 / ray->dir_y);
dda->hit = 0; dda->step_x = 1;
dda->step_y = 1;
if (ray->dir_x < 0) if (ray->dir_x < 0)
{ {
dda->step_x = -1; dda->step_x = -1;
dda->side_dist_x = (game->ply->pos_x - dda->map_x) * dda->delta_dist_x; dda->side_dist_x = (ply->pos_x - dda->map_x) * dda->delta_dist_x;
} }
else else
{ dda->side_dist_x = (dda->map_x + 1.0 - ply->pos_x) * dda->delta_dist_x;
dda->step_x = 1;
dda->side_dist_x = (dda->map_x + 1.0 - game->ply->pos_y) * dda->delta_dist_x;
}
if (ray->dir_y < 0) if (ray->dir_y < 0)
{ {
dda->step_y = -1; dda->step_y = -1;
dda->side_dist_y = (game->ply->pos_y - dda->map_y) * dda->delta_dist_y; dda->side_dist_y = (ply->pos_y - dda->map_y) * dda->delta_dist_y;
} }
else else
{ dda->side_dist_y = (dda->map_y + 1.0 - ply->pos_y) * dda->delta_dist_y;
dda->step_y = 1; dda->hit = 0;
dda->side_dist_y = (dda->map_y + 1.0 - game->ply->pos_y) * dda->delta_dist_y;
}
} }
void loop_dda(t_game *game, t_dda *dda) void loop_dda(t_game *game, t_dda *dda)
@ -66,56 +71,82 @@ void loop_dda(t_game *game, t_dda *dda)
dda->map_y += dda->step_y; dda->map_y += dda->step_y;
dda->side = 1; dda->side = 1;
} }
if (game->map->map[dda->map_x][dda->map_y] > 0) if (dda->map_x >= 0 && dda->map_y >= 0 &&
game->map.map[dda->map_y][dda->map_x] == '1')
dda->hit = 1; dda->hit = 1;
} }
} }
void ray_dda(t_game *game, t_dda *dda) void ray_dda(t_dda *dda)
{ {
if(dda->side == 0) if(dda->side == 0)
dda->perp_wall_dist = (dda->side_dist_x - dda->delta_dist_x); dda->perp_wall_dist = dda->side_dist_x - dda->delta_dist_x;
else else
dda->perp_wall_dist = (dda->side_dist_y - dda->delta_dist_y); dda->perp_wall_dist = dda->side_dist_y - dda->delta_dist_y;
if (dda->perp_wall_dist == 0)
dda->perp_wall_dist = 1e30;
dda->line_height = (int)(HEIGHT / dda->perp_wall_dist); dda->line_height = (int)(HEIGHT / dda->perp_wall_dist);
dda->draw_start = -dda->line_height / 2 + HEIGHT / 2; dda->draw_start = -(dda->line_height) / 2 + HEIGHT / 2;
if(dda->draw_start < 0) if(dda->draw_start < 0)
dda->draw_start = 0; dda->draw_start = 0;
dda->draw_end = dda->line_height / 2 + HEIGHT / 2; dda->draw_end = dda->line_height / 2 + HEIGHT / 2;
if(draw_end >= HEIGHT) if(dda->draw_end >= HEIGHT)
draw_end = HEIGHT - 1; dda->draw_end = HEIGHT - 1;
} }
int create_trgb(int t, int r, int g, int b) unsigned int get_rgba(int r, int g, int b, int a)
{ {
return (t << 24 | r << 16 | g << 8 | b); return (a << 24 | r << 16 | g << 8 | b);
} }
#include <time.h> void draw_line(t_game *game, t_dda *dda, unsigned int color, int x)
#include <stdlib.h>
void draw_dda(t_game *game, t_dda *dda)
{ {
int color; int y;
unsigned int white;
srand(time(NULL)); white = 0xFFFFFFFF;
int r = rand() % 4; y = 0;
if (r == 0) while (y < HEIGHT)
color = create_trgv(255, 150, 10, 200); {
else if (r == 1) if (y < dda->draw_start)
color = create_trgv(255, 10, 150, 200); {
else if (r == 2) mlx_put_pixel(game->window, x, y, white);
color = create_trgv(255, 200, 150, 10); }
else if (r == 3) else if (y >= dda->draw_start && y <= dda->draw_end)
color = create_trgv(255, 10, 200, 150); {
mlx_put_pixel(game->window, x, y, color);
mlx_put_pixel(game->mlx->window, abs(x), y, }
else if (y > dda->draw_end)
{
mlx_put_pixel(game->window, x, y, white);
}
y ++;
}
} }
void dda(t_game *game, t_dda *dda)
void draw_dda(t_game *game, t_dda *dda, int x)
{ {
init_dda(game, dda); unsigned int color;
loop_dda(game, dda);
ray_dda(game, dda); if (dda->step_x == -1 && dda->side == 0) // EAST
draw_dda(game, dda); color = get_rgba(255, 0, 0, 255);
else if (dda->step_x == 1 && dda->side == 0) // WEST
color = get_rgba(0, 255, 0, 255);
else if (dda->step_y == 1 && dda->side == 1) // NORTH
color = get_rgba(0, 0, 255, 255);
else // SOUTH
color = get_rgba(100, 100, 100, 255);
draw_line(game, dda, color, x);
}
void dda(t_game *game, int x)
{
t_dda dda;
init_dda(game, &dda);
loop_dda(game, &dda);
ray_dda(&dda);
draw_dda(game, &dda, x);
} }

View File

@ -6,13 +6,13 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */ /* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */
/* Updated: 2023/05/03 12:47:12 by erey-bet ### ########.fr */ /* Updated: 2023/05/03 14:50:16 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game.h"
int start_game(t_map *map) int start_game(t_map map)
{ {
t_game game; t_game game;
@ -22,15 +22,16 @@ int start_game(t_map *map)
game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT); game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT);
if (!game.window) if (!game.window)
{ {
mlx_close_window(game.mlx); mlx_terminate(game.mlx);
return(1); return(1);
} }
if (mlx_image_to_window(game.mlx, game.window, 0, 0) == -1) if (mlx_image_to_window(game.mlx, game.window, 0, 0) == -1)
{ {
mlx_close_window(game.mlx); mlx_terminate(game.mlx);
return(1); return(1);
} }
raycasting(&game);
mlx_key_hook(game.mlx, manage, &game); mlx_key_hook(game.mlx, manage, &game);
mlx_loop(game.mlx); mlx_loop(game.mlx);
mlx_terminate(game.mlx); mlx_terminate(game.mlx);

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */ /* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */
/* Updated: 2023/05/03 12:42:31 by erey-bet ### ########.fr */ /* Updated: 2023/05/03 16:51:50 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,9 +15,38 @@
# include "../cube3D.h" # include "../cube3D.h"
# include <math.h> # include <math.h>
# include <stdlib.h>
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
# include <time.h>
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
# define WIDTH 1920 # define WIDTH 1920
# define HEIGHT 1080 # define HEIGHT 1080
# define MOVESPEED 0.1
# define ROTATIONSPEED 10
typedef struct s_player typedef struct s_player
{ {
@ -57,13 +86,13 @@ typedef struct s_game
{ {
mlx_t *mlx; mlx_t *mlx;
mlx_image_t *window; mlx_image_t *window;
t_ply *ply; t_ply ply;
t_map *map; t_map map;
t_ray *ray; t_ray ray;
} t_game; } t_game;
/* INIT */ /* INIT */
void init(t_map *map, t_game *game); void init(t_map map, t_game *game);
/* MANAGE */ /* MANAGE */
void manage(mlx_key_data_t keydata, void *param); void manage(mlx_key_data_t keydata, void *param);
@ -74,4 +103,6 @@ int manage_keys(mlx_key_data_t keydata, t_game *game);
/* RAYCASTING */ /* RAYCASTING */
int raycasting(t_game *game); int raycasting(t_game *game);
/* DDA ALGO */
void dda(t_game *game, int x);
#endif #endif

View File

@ -6,16 +6,20 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:50:22 by erey-bet #+# #+# */ /* Created: 2023/04/27 14:50:22 by erey-bet #+# #+# */
/* Updated: 2023/05/03 12:44:19 by erey-bet ### ########.fr */ /* Updated: 2023/05/03 17:47:38 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game.h"
void init_ply(t_map *map, t_ply *ply) void init_ply(t_map map, t_ply *ply)
{ {
ply->pos_x = map->ply_x; ply->pos_x = map.ply_x;
ply->pos_y = map->ply_y; ply->pos_y = map.ply_y;
ply->dir_x = -1;
ply->dir_y = 0;
ply->pla_x = 0;
ply->pla_y = 0.90;
} }
void init_ray(t_ray *ray) void init_ray(t_ray *ray)
@ -24,17 +28,12 @@ void init_ray(t_ray *ray)
ray->dir_y = 0; ray->dir_y = 0;
} }
void init(t_map *map, t_game *game) void init(t_map map, t_game *game)
{ {
t_ply ply;
t_ray ray;
game->mlx = mlx_init(WIDTH, HEIGHT, "jan lili", true); game->mlx = mlx_init(WIDTH, HEIGHT, "jan lili", true);
if (!game->mlx) if (!game->mlx)
return ; return ;
game->map = map; game->map = map;
init_ply(map, &ply); init_ply(map, &game->ply);
game->ply = &ply; init_ray(&game->ray);
init_ray(&ray);
game->ray = &ray;
} }

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 12:52:14 by erey-bet #+# #+# */ /* Created: 2023/04/27 12:52:14 by erey-bet #+# #+# */
/* Updated: 2023/04/27 14:31:50 by erey-bet ### ########.fr */ /* Updated: 2023/05/03 14:53:14 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,22 +6,53 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */ /* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */
/* Updated: 2023/05/03 12:44:41 by erey-bet ### ########.fr */ /* Updated: 2023/05/03 16:16:34 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game.h"
int movement(t_ply *ply, int key) void rotate(t_ply *ply, bool right)
{ {
double old_dir_x;
double old_plane_x;
double speed;
old_dir_x = ply->dir_x;
old_plane_x = ply->pla_x;
speed = ROTATIONSPEED * (M_1_PI / 180.0);
if (right)
speed = -speed;
ply->dir_x = ply->dir_x * cos(speed) - ply->dir_y * sin(speed);
ply->dir_y = old_dir_x * sin(speed) + ply->dir_y * cos(speed);
ply->pla_x = ply->pla_x * cos(speed) - ply->pla_y * sin(speed);
ply->pla_y = old_plane_x * sin(speed) + ply->pla_y * cos(speed);
}
void movement(t_game *game, t_ply *ply, double x, double y)
{
if(game->map.map[(int)(ply->pos_x + ply->dir_x * MOVESPEED)][(int)ply->pos_y] == '0')
ply->pos_x += x;
if(game->map.map[(int)ply->pos_x][(int)(ply->pos_y + ply->dir_y * MOVESPEED)] == '0')
ply->pos_y += y;
printf("ply->pos_x: %f, ply->pos_y: %f\n", ply->pos_x, ply->pos_y);
printf("x: %f, y: %f\n", x, y);
}
int manage_movement(t_game *game, int key)
{
t_ply *ply;
ply = &game->ply;
if (key == MLX_KEY_W) if (key == MLX_KEY_W)
ply->pos_y -= 0.1; movement(game, ply, ply->dir_x * MOVESPEED, ply->dir_y * MOVESPEED);
else if (key == MLX_KEY_S) else if (key == MLX_KEY_S)
ply->pos_y += 0.1; movement(game, ply, -(ply->dir_x * MOVESPEED), -(ply->dir_y * MOVESPEED));
else if (key == MLX_KEY_D) else if (key == MLX_KEY_D)
ply->pos_x += 0.1; rotate(ply, true);
else if (key == MLX_KEY_A) else if (key == MLX_KEY_A)
ply->pos_x -= 0.1; rotate(ply, false);
return (1); return (1);
} }
@ -36,11 +67,7 @@ int manage_keys(mlx_key_data_t keys, t_game *game)
|| keys.key == MLX_KEY_S || keys.key == MLX_KEY_S
|| keys.key == MLX_KEY_D || keys.key == MLX_KEY_D
|| keys.key == MLX_KEY_A) || keys.key == MLX_KEY_A)
is_moving = movement(game->ply, keys.key); is_moving = manage_movement(game, keys.key);
/*else if (key.key == MLX_KEY_RIGHT)
game->ply->direc += 0.1;
else if (key.key == MLX_KEY_LEFT)
game->ply->direc -= 0.1;*/
if (is_moving) if (is_moving)
return (1); return (1);
return (0); return (0);

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:30:29 by erey-bet #+# #+# */ /* Created: 2023/04/27 14:30:29 by erey-bet #+# #+# */
/* Updated: 2023/05/03 12:45:15 by erey-bet ### ########.fr */ /* Updated: 2023/05/03 17:44:24 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,16 +17,19 @@ int ray(t_game *game)
t_ply *p; t_ply *p;
t_ray *ray; t_ray *ray;
double camera; double camera;
int i; int x;
i = 0; x = 0;
p = game->ply; p = &game->ply;
ray = game->ray;
while (i <= WIDTH) ray = &game->ray;
while (x <= WIDTH)
{ {
camera = 2 * i / (double)WIDTH - 1; //x-coordinate in camera space camera = 2 * x / ((double)(WIDTH - 1));
ray->dir_x = p->dir_x + p->pla_x * camera; ray->dir_x = p->dir_x + p->pla_x * camera;
ray->dir_y = p->dir_y + p->pla_y * camera; ray->dir_y = p->dir_y + p->pla_y * camera;
dda(game, x);
x++;
} }
return(0); return(0);
} }

29
main.c
View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:44:55 by erey-bet #+# #+# */ /* Created: 2023/04/26 12:44:55 by erey-bet #+# #+# */
/* Updated: 2023/04/27 14:43:10 by erey-bet ### ########.fr */ /* Updated: 2023/05/03 17:33:16 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,30 +14,31 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_map *map; t_map map;
if (argc != 2) if (argc != 2)
{ {
printf("No argument"); printf("No argument");
return (1); return (1);
} }
/*map = map_parsing(argv[1]); /*map = map_parsing(&map, argv[1]);
if (!map) if (!map)
{ {
return (2); return (2);
}*/ }*/
(void)argv; (void)argv;
map = malloc(sizeof(t_map)); map.map = malloc(sizeof(char *) * 10);
map->map = malloc(sizeof(char *) * 5); map.map[0] = "111111";
map->map[0] = "11111"; map.map[1] = "100101";
map->map[1] = "10001"; map.map[2] = "101001";
map->map[2] = "10S01"; map.map[3] = "100001";
map->map[3] = "10101"; map.map[4] = "10N001";
map->map[4] = "11111"; map.map[5] = "101001";
map->size_x = 5; map.map[6] = "111111";
map->size_y = 5; map.size_x = 5;
map->ply_x = 3; map.size_y = 5;
map->ply_y = 3; map.ply_x = 2;
map.ply_y = 4;
if (start_game(map)) if (start_game(map))
{ {
return (3); return (3);

BIN
map/parsing.o Normal file

Binary file not shown.