Compare commits

...

2 commits

Author SHA1 Message Date
Theo Champion
077659cd25 feat: Added raycaster (concept only no drawing yet)
Almost copy pasted from https://github.com/herbievine/42/blob/main/cub3d
but it's also copied from Lodev's ressource
2025-07-30 13:34:36 +02:00
Theo Champion
7c42208d20 dev: added ray structure 2025-07-30 12:07:45 +02:00
2 changed files with 144 additions and 0 deletions

107
src/raycast/ray.c Normal file
View file

@ -0,0 +1,107 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ray.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/30 12:10:17 by tchampio #+# #+# */
/* Updated: 2025/07/30 12:36:11 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "ray.h"
#include "../player/player.h"
#include "../consts.h"
#include "../cub3d_data.h"
#include <math.h>
void init_ray(t_ray *ray, int pos_x, t_player *player)
{
ray->plane = 2 * pos_x / (double)WIDTH - 1;
ray->dir_x = player->dir_x + player->plane_x * ray->plane;
ray->dir_y = player->dir_y + player->plane_y * ray->plane;
ray->map_x = (int)player->x;
ray->map_y = (int)player->y;
ray->delta_dist_x = fabs(1 / ray->dir_x);
ray->delta_dist_y = fabs(1 / ray->dir_y);
}
void ray_calculate_step(t_ray *ray, t_player *player)
{
if (ray->dir_x < 0)
{
ray->step_x = -1;
ray->side_dist_x = (player->x - ray->map_x) * ray->delta_dist_x;
}
else
{
ray->step_x = 1;
ray->side_dist_x = (ray->map_x + 1.0 - player->x) * ray->delta_dist_x;
}
if (ray->dir_y < 0)
{
ray->step_y = -1;
ray->side_dist_y = (player->y - ray->map_y) * ray->delta_dist_y;
}
else
{
ray->step_y = 1;
ray->side_dist_y = (ray->map_y + 1.0 - player->y) * ray->delta_dist_y;
}
}
void calculate_wall_dist(t_ray *ray, char **map)
{
while (true)
{
if (ray->side_dist_x < ray->side_dist_y)
{
ray->side_dist_x += ray->delta_dist_x;
ray->map_x += ray->step_x;
ray->side = 0;
}
else
{
ray->side_dist_y += ray->delta_dist_y;
ray->map_y += ray->step_y;
ray->side = 1;
}
if (map[ray->map_y][ray->map_x] != '0')
break ;
}
if (ray->side == 0)
ray->wall_dist = ray->side_dist_x - ray->delta_dist_x;
else
ray->wall_dist = ray->side_dist_y - ray->delta_dist_y;
}
void calculate_wall_height(t_ray *ray, t_player *player)
{
ray->wall_height = (int)(HEIGHT / ray->wall_dist);
ray->draw_start = -ray->wall_height / 2 + HEIGHT / 2;
if (ray->draw_start < 0)
ray->draw_start = 0;
ray->draw_end = ray->wall_height / 2 + HEIGHT / 2;
if (ray->draw_end >= HEIGHT)
ray->draw_start = HEIGHT - 1;
if (ray->side == 0)
ray->wall_x = player->y + ray->wall_dist * ray->dir_y;
else
ray->wall_x = player->x + ray->wall_dist * ray->dir_x;
ray->wall_x -= floor(ray->wall_x);
}
void raycaster(t_cub3d_data *data, t_ray *ray)
{
int x;
x = 0;
while (x < WIDTH)
{
init_ray(ray, x, &data->player);
ray_calculate_step(ray, &data->player);
calculate_wall_dist(ray, data->map->map);
calculate_wall_height(ray, &data->player);
}
}

37
src/raycast/ray.h Normal file
View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ray.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/29 11:32:20 by tchampio #+# #+# */
/* Updated: 2025/07/29 11:35:17 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RAY_H
# define RAY_H
typedef struct s_ray
{
double plane;
double dir_x;
double dir_y;
int map_x;
int map_y;
int step_x;
int step_y;
double side_dist_x;
double side_dist_y;
double delta_dist_x;
double delta_dist_y;
double wall_dist;
int side;
int wall_height;
int draw_start;
int draw_end;
double wall_x;
} t_ray;
#endif // RAY_H