mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
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
This commit is contained in:
parent
7c42208d20
commit
077659cd25
1 changed files with 107 additions and 0 deletions
107
src/raycast/ray.c
Normal file
107
src/raycast/ray.c
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue