1
0

🏗️」 wip(DDA): Fuck that was hard (thx @kbz-8)

This commit is contained in:
2024-10-28 14:41:48 +01:00
parent bce9da0440
commit efa0d6fbbd
12 changed files with 206 additions and 381 deletions

View File

@ -5,59 +5,38 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 14:04:10 by adjoly #+# #+# */ /* Created: 2024/10/28 13:04:36 by adjoly #+# #+# */
/* Updated: 2024/10/23 15:00:05 by adjoly ### ########.fr */ /* Updated: 2024/10/28 14:37:10 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
# ifndef DDA_H # ifndef DDA_H
# define DDA_H # define DDA_H
# include "typedef.h" # include <math.h>
# include "settings.h"
# include <stdint.h>
/** # include "game.h"
* @brief Function used to get all the ray angle #include "game/vectwo.h"
*
* @param player The address of the t_player struct
* @param dda The address of the t_dda struct
*/
void get_ray_angle(t_player *player, t_dda (*dda)[800]);
/** typedef struct s_dda
* @brief Function used to setup all the dda variable prior to casting {
* the rays bool h;
* int i;
* @param player The address of the t_player struct t_vec2 map;
* @param dda The address of the t_dda struct t_vec2 vert;
* t_vec2 hori;
*/ t_vec2 distance;
void setup_dda(t_dda (*dda)[800], t_player *player); } t_dda;
/** typedef struct s_ray
* @brief Function used to cast all the rays {
* t_vec2 pos;
* @param dda The address of the t_dda struct float tan;
* @param map The map as a char ** float angle;
* @param map_coord The coordinate of the player on the map_grid t_vec2 offset;
*/ float distance;
void while_dda(t_dda (*dda)[800], char **map, t_player *player); } t_ray;
/// NEED TO COMBINE THOSE IF NOT USEFULL TO SEPARATE void dda_algo(t_player *play, t_render *render);
/**
* @brief Function used to get the distance from the player of all rays
*
* @param dda The address of the t_dda struct
*/
float *dist_dda(t_dda dda[WINDOW_X / RAY_SIZE]);
/**
* @brief Function used to get the size of the line to be printed on the screen
*
* @param dda The array of the all the distance to the wall of the rays
*/
uint16_t *line_dda(float *wall_dist);
uint16_t *dda(t_player *player, char **map);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */ /* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */
/* Updated: 2024/10/24 11:38:56 by adjoly ### ########.fr */ /* Updated: 2024/10/28 14:08:09 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,10 +21,34 @@
# include <stddef.h> # include <stddef.h>
# include <stdio.h> # include <stdio.h>
# include "dda.h"
# include "settings.h" # include "settings.h"
# include "parsing.h"
# include "typedef.h" # include "vectwo.h"
# include "../parsing.h"
typedef struct s_map
{
char **arr;
char p_side;
t_coord p_spawnpoint;
t_coord size;
} t_map;
typedef struct s_render
{
t_map *world;
void *mlx;
void *win;
void *texture[4];
} t_render;
typedef struct s_player
{
t_vec2 coord;
t_coord map_coords;
double direction;
} t_player;
/** /**
* @brief This function is used to handle keypress * @brief This function is used to handle keypress
@ -36,13 +60,6 @@
*/ */
int key_hook(int key, void *param); int key_hook(int key, void *param);
/**
* @brief This function is used to create an image with the player position
*
* @param cub The address of a t_cub struct filled with mlx data and other
* thing
*/
void get_player_image(t_cub *cub);
/** /**
* @brief This function is here to change the direction of the player * @brief This function is here to change the direction of the player
@ -56,21 +73,4 @@ void get_player_image(t_cub *cub);
*/ */
void change_direction(double speed, bool clockwise, t_player *player); void change_direction(double speed, bool clockwise, t_player *player);
/**
* @brief Function used to draw a square
*
* @param cub The adress of a t_cub struct
* @param coord The coordinate of the printed square
* @param size The size of the printed square
* @param color The color of the printed square
*/
void draw_square(t_cub *cub, t_coord coord, uint16_t size, int color);
/**
* @brief Function used to draw the map
*
* @param cub The address of the t_cub struct
*/
void print_map(t_cub *cub);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 14:06:39 by adjoly #+# #+# */ /* Created: 2024/10/04 14:06:39 by adjoly #+# #+# */
/* Updated: 2024/10/23 11:18:38 by adjoly ### ########.fr */ /* Updated: 2024/10/28 14:04:25 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,11 +21,13 @@
# define D_KEY 7 # define D_KEY 7
# define WHITE 0xFFFFFFFF # define WHITE 0xFFFFFFFF
# define DEG_TO_RAD 0.0174533
# define WINDOW_Y 900 # define WINDOW_Y 900
# define WINDOW_X 1600 # define WINDOW_X 1600
# define PLAYER_ROT_SPEED (2 * M_PI) / 128 # define PLAYER_ROT_SPEED (2 * M_PI) / 128
# define PLAYER_SPEED 3 # define PLAYER_SPEED 3
# define MAP_CHUNK_SIZE 64 # define CHUNK_SIZE 64
# define RAY_SIZE 2 # define RAY_SIZE 2
# define FOV ( 60 * (M_PI / 180) ) # define FOV ( 60 * (M_PI / 180) )

View File

@ -1,68 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* typedef.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 14:28:24 by adjoly #+# #+# */
/* Updated: 2024/10/23 14:33:33 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STRUCT_H
# define STRUCT_H
# include <stdbool.h>
# include "parsing.h"
/**
* @brief Enum for the wall side that has been hit
*/
typedef enum s_wall_side
{
HORIZONTAL,
VERTICAL
} t_wall_side;
typedef struct s_coord_f
{
float x;
float y;
} t_coord_f;
typedef struct s_step
{
char x;
char y;
} t_step;
typedef struct s_player
{
t_coord coords;
t_coord map_coords;
double direction;
} t_player;
typedef struct s_cub
{
void *mlx;
void *win;
void *sprites;
void *img;
char **map;
t_player player;
} t_cub;
typedef struct s_dda
{
t_coord_f ray_dir;
t_coord_f side_dist;
t_coord_f delta_dist;
t_step step;
t_wall_side wall_side;
bool wall_hit;
} t_dda;
#endif

View File

@ -1,18 +1,38 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* constant.h :+: :+: :+: */ /* vectwo.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 16:39:48 by adjoly #+# #+# */ /* Created: 2024/10/28 13:32:14 by adjoly #+# #+# */
/* Updated: 2024/10/04 16:40:10 by adjoly ### ########.fr */ /* Updated: 2024/10/28 14:24:56 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef CONSTANT_H #ifndef VECTWO_H
# define CONSTANT_H # define VECTWO_H
# define WHITE 0xFFFFFFFF typedef struct s_vec2
{
float x;
float y;
} t_vec2;
/* ------------------ Vec2 Utils ------------------ */
/**
* @brief Gives you the distance between two point
*/
float vec2_dist(t_vec2 first, t_vec2 second);
/**
* @brief Just add second to first
*/
void vec2_add(t_vec2 *first, t_vec2 second);
/**
* @brief Just substract second to first
*/
void vec2_sub(t_vec2 *first, t_vec2 second);
#endif #endif

105
src/raycasting/dda.c Normal file
View File

@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dda.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/07 16:55:09 by adjoly #+# #+# */
/* Updated: 2024/10/28 14:40:14 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/vectwo.h"
#include <game/settings.h>
#include <game/dda.h>
#include <math.h>
void setup_dda_hor(t_player *play, t_ray *ray, t_dda *dda)
{
dda->h = true;
ray->tan = tan(ray->angle);
if (cos(ray->angle) < -0.001)
{
ray->pos.x = (((int)play->coord.x >> 6) << 6) - 0.001;
ray->pos.y -= (play->coord.x - ray->pos.x) * ray->tan + play->coord.y;
ray->offset.x = -CHUNK_SIZE;
ray->offset.y = -ray->offset.x * ray->tan;
}
else if(cos(ray->angle) > 0.001)
{
ray->pos.x = (((int)play->coord.x >> 6) << 6) + CHUNK_SIZE;
ray->pos.y -= (play->coord.x - ray->pos.x) * ray->tan + play->coord.y;
ray->offset.x = CHUNK_SIZE;
ray->offset.y = -ray->offset.x * ray->tan;
}
}
void setup_dda_ver(t_player *play, t_ray *ray, t_dda *dda)
{
dda->h = false;
ray->tan = 1 / tan(ray->angle);
if (sin(ray->angle) < -0.001)
{
ray->pos.y = (((int)play->coord.y >> 6) << 6) - 0.001;
ray->pos.x = (play->coord.y - ray->pos.y) * ray->tan + play->coord.x;
ray->offset.y = -CHUNK_SIZE;
ray->offset.x = -ray->offset.y * ray->tan;
}
else if(sin(ray->angle) > 0.001)
{
ray->pos.y = (((int)play->coord.y >> 6) << 6) + CHUNK_SIZE;
ray->pos.x -= (play->coord.y - ray->pos.y) * ray->tan + play->coord.x;
ray->offset.y = CHUNK_SIZE;
ray->offset.x = -ray->offset.y * ray->tan;
}
}
void dda_loop(t_dda *dda, t_ray *ray, t_map *map, t_player *play)
{
uint16_t w;
uint16_t h;
w = map->size.x;
h = map->size.y;
while (dda->i)
{
dda->map.x = ((int)ray->pos.x >> 6);
dda->map.y = ((int)ray->pos.x >> 6);
if (dda->map.x >= 0 && dda->map.x < w && dda->map.y >= 0 && dda->map.y < h && \
map->arr[(int)dda->map.y][(int)dda->map.x] == '1')
{
dda->i = 0;
if (dda->h)
dda->distance.x = vec2_dist(play->coord, ray->pos);
}
else
{
vec2_add(&ray->pos, ray->offset);
dda->i--;
}
}
}
void dda(t_player *player, t_render *render)
{
t_dda dda;
t_ray ray;
setup_dda_hor(player, &ray, &dda);
dda_loop(&dda, &ray, render->world, player);
setup_dda_ver(player, &ray, &dda);
dda_loop(&dda, &ray, render->world, player);
if (dda.distance.x < dda.distance.y)
{
ray.pos.x = fabsf(dda.hori.x);
ray.pos.y = fabsf(dda.hori.y);
ray.distance = dda.distance.x;
}
else
{
ray.pos.x = fabsf(dda.vert.x);
ray.pos.y = fabsf(dda.vert.y);
ray.distance = dda.distance.y;
}
}

View File

@ -1,38 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dda.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/07 16:55:09 by adjoly #+# #+# */
/* Updated: 2024/10/23 15:04:45 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/dda.h"
#include "game/settings.h"
#include "sys/types.h"
#include <stdint.h>
#include <stdio.h>
uint16_t *dda(t_player *player, char **map)
{
t_dda dda[WINDOW_X / RAY_SIZE];
float *wall_dist;
uint16_t *line_size;
setup_dda(&dda, player);
player->map_coords.x = player->coords.x - player->coords.x % MAP_CHUNK_SIZE;
player->map_coords.y = player->coords.y - player->coords.y % MAP_CHUNK_SIZE;
while_dda(&dda, map, player);
wall_dist = dist_dda(dda);
line_size = line_dda(wall_dist);
return (line_size);
}
/**
* calculate size on screen
* screen_height / per_wall_dist
* (and get orientation)
*/

View File

@ -1,36 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dist_dda.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/08 13:26:19 by adjoly #+# #+# */
/* Updated: 2024/10/23 14:10:52 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/dda.h"
#include <stddef.h>
#include "game/settings.h"
#include "libft.h"
float *dist_dda(t_dda dda[WINDOW_X / RAY_SIZE])
{
size_t i;
float *wall_dist;
i = 0;
wall_dist = ft_calloc(WINDOW_X / RAY_SIZE + 1, sizeof(float));
if (!wall_dist)
return (NULL);
while (i < WINDOW_X / RAY_SIZE)
{
if (dda[i].wall_side == HORIZONTAL)
wall_dist[i] = dda[i].side_dist.x - dda[i].delta_dist.x;
else
wall_dist[i] = dda[i].side_dist.y - dda[i].delta_dist.y;
i++;
}
return (wall_dist);
}

View File

@ -1,32 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_ray_angle.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/29 15:26:29 by adjoly #+# #+# */
/* Updated: 2024/10/23 14:19:21 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/dda.h"
#include "game/settings.h"
#include <math.h>
#include <stdint.h>
#include <stdio.h>
void get_ray_angle(t_player *player, t_dda (*dda)[WINDOW_X / RAY_SIZE])
{
uint16_t x;
float ray_angle;
x = 0;
while (x < WINDOW_X)
{
ray_angle = (float)(player->direction - (FOV / 2) + ((float)x / WINDOW_X) * FOV);
(*dda)[x / RAY_SIZE].ray_dir.x = cos(ray_angle);
(*dda)[x / RAY_SIZE].ray_dir.y = sin(ray_angle);
x += RAY_SIZE;
}
}

View File

@ -1,61 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* setup_dda.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 23:47:14 by adjoly #+# #+# */
/* Updated: 2024/10/24 12:40:00 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/settings.h"
#include "game/dda.h"
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
void get_side_dist(t_dda *dda, t_player *player)
{
if (dda->ray_dir.x < 0)
{
dda->side_dist.x = (player->coords.x - player->map_coords.x) * dda->delta_dist.x;
dda->step.x = -MAP_CHUNK_SIZE;
}
else
{
dda->side_dist.x = ((player->map_coords.x + MAP_CHUNK_SIZE) - player->coords.x) * dda->delta_dist.x;
dda->step.x = MAP_CHUNK_SIZE;
}
if (dda->ray_dir.y < 0)
{
dda->side_dist.y = (player->coords.y - player->map_coords.y) * dda->delta_dist.y;
dda->step.y = -MAP_CHUNK_SIZE;
}
else
{
dda->side_dist.y = (player->map_coords.y + MAP_CHUNK_SIZE - player->coords.y) * dda->delta_dist.y;
dda->step.y = MAP_CHUNK_SIZE;
}
}
void setup_dda(t_dda (*dda)[WINDOW_X / RAY_SIZE], t_player *player)
{
size_t i;
i = 0;
get_ray_angle(player, dda);
while (i < WINDOW_X / RAY_SIZE)
{
(*dda)[i].delta_dist.x = fabsf(MAP_CHUNK_SIZE / (*dda)[i].ray_dir.x);
(*dda)[i].delta_dist.y = fabsf(MAP_CHUNK_SIZE / (*dda)[i].ray_dir.y);
printf("%f \n", (*dda)[i].delta_dist.x);
printf("%f \n", (*dda)[i].delta_dist.y);
get_side_dist(&(*dda)[i], player);
printf("side %f \n", (*dda)[i].side_dist.x);
printf("side %f \n", (*dda)[i].side_dist.y);
i++;
}
}

View File

@ -1,47 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* while_dda.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/06 18:46:56 by adjoly #+# #+# */
/* Updated: 2024/10/23 15:03:03 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/dda.h"
#include "game/settings.h"
#include <stdio.h>
void while_dda(t_dda (*dda)[WINDOW_X / RAY_SIZE], char **map, t_player *player)
{
uint16_t i;
t_coord map_coord;
i = 0;
while (i < WINDOW_X / RAY_SIZE)
{
(*dda)[i].wall_hit = false;
map_coord.x = player->map_coords.x;
map_coord.y = player->map_coords.y;
while (!(*dda)[i].wall_hit)
{
if ((*dda)[i].side_dist.x < (*dda)[i].side_dist.y)
{
(*dda)[i].side_dist.x += (*dda)[i].delta_dist.x;
map_coord.x += (*dda)[i].step.x;
(*dda)[i].wall_side = HORIZONTAL;
}
else
{
(*dda)[i].side_dist.y += (*dda)[i].delta_dist.y;
map_coord.y += (*dda)[i].step.y;
(*dda)[i].wall_side = VERTICAL;
}
if (map_coord.x < 0 || map_coord.y < 0 || map[map_coord.x / MAP_CHUNK_SIZE][map_coord.y / MAP_CHUNK_SIZE] == '1')
(*dda)[i].wall_hit = true;
}
i++;
}
}

View File

@ -1,30 +1,31 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* line_dda.c :+: :+: :+: */ /* maths.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 17:27:44 by adjoly #+# #+# */ /* Created: 2024/10/26 15:47:29 by adjoly #+# #+# */
/* Updated: 2024/10/22 13:03:40 by adjoly ### ########.fr */ /* Updated: 2024/10/28 14:00:34 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game/settings.h" #include <game/type.h>
#include "game/dda.h" #include <math.h>
#include <stdlib.h>
uint16_t *line_dda(float *wall_dist) float vec2_dist(t_vec2 first, t_vec2 second)
{ {
uint16_t i; return (sqrt(powf(second.x - first.x, 2) + powf(second.y - first.y, 2)));
uint16_t *line_size; }
i = 0; void vec2_add(t_vec2 *first, t_vec2 second)
line_size = malloc(WINDOW_X / RAY_SIZE * sizeof(uint16_t));
while (i < WINDOW_X / RAY_SIZE)
{ {
line_size[i] = WINDOW_Y * MAP_CHUNK_SIZE / wall_dist[i]; first->x += second.x;
i++; first->y += second.y;
} }
return (line_size);
void vec2_sub(t_vec2 *first, t_vec2 second)
{
first->x -= second.x;
first->y -= second.y;
} }