Archived
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

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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 17:27:44 by adjoly #+# #+# */
/* Updated: 2024/10/22 13:03:40 by adjoly ### ########.fr */
/* Created: 2024/10/26 15:47:29 by adjoly #+# #+# */
/* Updated: 2024/10/28 14:00:34 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/settings.h"
#include "game/dda.h"
#include <stdlib.h>
#include <game/type.h>
#include <math.h>
uint16_t *line_dda(float *wall_dist)
float vec2_dist(t_vec2 first, t_vec2 second)
{
uint16_t i;
uint16_t *line_size;
i = 0;
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];
i++;
}
return (line_size);
return (sqrt(powf(second.x - first.x, 2) + powf(second.y - first.y, 2)));
}
void vec2_add(t_vec2 *first, t_vec2 second)
{
first->x += second.x;
first->y += second.y;
}
void vec2_sub(t_vec2 *first, t_vec2 second)
{
first->x -= second.x;
first->y -= second.y;
}