1
0

🏗️」 wip(Raycasting): Seems working but not

This commit is contained in:
2024-10-26 09:42:15 +02:00
parent fc702d8bec
commit bce9da0440
17 changed files with 122 additions and 113 deletions

1
.envrc
View File

@ -1 +0,0 @@
use flake

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/07 16:02:18 by adjoly #+# #+# #
# Updated: 2024/10/19 12:19:00 by adjoly ### ########.fr #
# Updated: 2024/10/19 15:15:37 by adjoly ### ########.fr #
# #
# **************************************************************************** #
@ -14,7 +14,7 @@ SHELL = bash
NAME = cub3D
CC = gcc
CC = cc
OBJSDIR = obj/

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 14:04:10 by adjoly #+# #+# */
/* Updated: 2024/10/13 21:28:23 by adjoly ### ########.fr */
/* Updated: 2024/10/23 15:00:05 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -42,7 +42,7 @@ void setup_dda(t_dda (*dda)[800], t_player *player);
* @param map The map as a char **
* @param map_coord The coordinate of the player on the map_grid
*/
void while_dda(t_dda (*dda)[800], char **map, t_coord map_coord);
void while_dda(t_dda (*dda)[800], char **map, t_player *player);
/// NEED TO COMBINE THOSE IF NOT USEFULL TO SEPARATE
/**

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */
/* Updated: 2024/10/08 21:42:52 by adjoly ### ########.fr */
/* Updated: 2024/10/24 11:38:56 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,9 +41,8 @@ int key_hook(int key, void *param);
*
* @param cub The address of a t_cub struct filled with mlx data and other
* thing
*
*/
void get_player_image(t_cub *cub, uint8_t key_pressed);
void get_player_image(t_cub *cub);
/**
* @brief This function is here to change the direction of the player
@ -54,7 +53,6 @@ void get_player_image(t_cub *cub, uint8_t key_pressed);
* @param speed The speed a which the player rotate
* @param clockwise The direction which the player rotate
* @param player A pointer to a t_player struct
*
*/
void change_direction(double speed, bool clockwise, t_player *player);
@ -65,7 +63,6 @@ void change_direction(double speed, bool clockwise, t_player *player);
* @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);

View File

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

View File

@ -6,15 +6,15 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 14:28:24 by adjoly #+# #+# */
/* Updated: 2024/10/08 13:43:59 by adjoly ### ########.fr */
/* Updated: 2024/10/23 14:33:33 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STRUCT_H
# define STRUCT_H
# include "parsing.h"
# include <stdbool.h>
# include "parsing.h"
/**
* @brief Enum for the wall side that has been hit
@ -40,6 +40,7 @@ typedef struct s_step
typedef struct s_player
{
t_coord coords;
t_coord map_coords;
double direction;
} t_player;

View File

@ -6,7 +6,7 @@
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/12 12:49:16 by madumerg #+# #+# */
/* Updated: 2024/09/12 16:58:29 by madumerg ### ########.fr */
/* Updated: 2024/10/19 15:37:50 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,13 +6,16 @@
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */
/* Updated: 2024/10/14 12:32:14 by adjoly ### ########.fr */
/* Updated: 2024/10/24 12:51:06 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
#include "game/game.h"
#include "game/settings.h"
#include "libft.h"
#include "mlx.h"
#include <stdlib.h>
void *print_frame(t_cub *cub);
@ -20,26 +23,25 @@ int main(int ac, char **av)
{
t_cub cub;
cub.map = (char *[]){
"1111111\0",
"1000101\0",
"1000101\0",
"1000001\0",
"1111111\0",
NULL
};
cub.map = ft_calloc(6, sizeof(char *));
cub.map[0] = ft_strdup("111111");
cub.map[1] = ft_strdup("101001");
cub.map[2] = ft_strdup("100001");
cub.map[3] = ft_strdup("100001");
cub.map[4] = ft_strdup("111111");
cub.map[5] = NULL;
(void)ac;
(void)av;
cub.mlx = mlx_init();
cub.win = mlx_new_window(cub.mlx, WINDOW_X, WINDOW_Y, "WTF");
cub.player.coords.x = 100;
cub.player.coords.y = 100;
cub.player.coords.x = 128;
cub.player.coords.y = 128;
cub.player.direction = 0;
cub.img = print_frame(&cub);
//get_player_image(&cub, 0);
mlx_put_image_to_window(cub.mlx, cub.win, cub.img, 0, 0);
mlx_on_event(cub.mlx, cub.win, MLX_KEYDOWN, key_hook, &cub);
mlx_loop(cub.mlx);
mlx_destroy_image(cub.mlx, cub.img);
mlx_destroy_window(cub.mlx, cub.win);
mlx_destroy_display(cub.mlx);
return (0);

View File

@ -6,24 +6,26 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/07 16:55:09 by adjoly #+# #+# */
/* Updated: 2024/10/13 21:27:59 by adjoly ### ########.fr */
/* 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];
t_coord map_coord;
float *wall_dist;
uint16_t *line_size;
setup_dda(&dda, player);
map_coord.x = player->coords.x - player->coords.x % MAP_CHUNK_SIZE;
map_coord.y = player->coords.y - player->coords.y % MAP_CHUNK_SIZE;
while_dda(&dda, map, map_coord);
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);

View File

@ -6,12 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/08 13:26:19 by adjoly #+# #+# */
/* Updated: 2024/10/13 21:30:15 by adjoly ### ########.fr */
/* Updated: 2024/10/23 14:10:52 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/dda.h"
#include <stdint.h>
#include <stddef.h>
#include "game/settings.h"
#include "libft.h"
@ -22,17 +21,16 @@ float *dist_dda(t_dda dda[WINDOW_X / RAY_SIZE])
float *wall_dist;
i = 0;
wall_dist = ft_calloc(WINDOW_X / RAY_SIZE, sizeof(float));
wall_dist = ft_calloc(WINDOW_X / RAY_SIZE + 1, sizeof(float));
if (!wall_dist)
return (NULL);
while (dda)
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++;
dda++;
}
return (wall_dist);
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/29 15:26:29 by adjoly #+# #+# */
/* Updated: 2024/10/14 12:35:17 by adjoly ### ########.fr */
/* Updated: 2024/10/23 14:19:21 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,18 +16,17 @@
#include <stdint.h>
#include <stdio.h>
void get_ray_angle(t_player *player, t_dda (*dda)[800])
void get_ray_angle(t_player *player, t_dda (*dda)[WINDOW_X / RAY_SIZE])
{
uint16_t x;
float ray_angle;
x = 0;
printf("plyaer dir = %f", player->direction);
while (x < WINDOW_X)
{
ray_angle = player->direction - (FOV / 2) + (x / WINDOW_X) * FOV;
dda[x / RAY_SIZE]->ray_dir.x = cos(ray_angle);
dda[x / RAY_SIZE]->ray_dir.y = sin(ray_angle);
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

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 17:27:44 by adjoly #+# #+# */
/* Updated: 2024/10/13 21:28:12 by adjoly ### ########.fr */
/* Updated: 2024/10/22 13:03:40 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,6 @@
#include "game/dda.h"
#include <stdlib.h>
#include <stdint.h>
uint16_t *line_dda(float *wall_dist)
{
uint16_t i;
@ -22,9 +21,9 @@ uint16_t *line_dda(float *wall_dist)
i = 0;
line_size = malloc(WINDOW_X / RAY_SIZE * sizeof(uint16_t));
while (wall_dist[i])
while (i < WINDOW_X / RAY_SIZE)
{
line_size[i] = (WINDOW_Y / wall_dist[i]);
line_size[i] = WINDOW_Y * MAP_CHUNK_SIZE / wall_dist[i];
i++;
}
return (line_size);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 23:47:14 by adjoly #+# #+# */
/* Updated: 2024/10/08 13:28:24 by adjoly ### ########.fr */
/* Updated: 2024/10/24 12:40:00 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,27 +15,28 @@
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
void get_side_dist(t_dda *dda, t_coord incell_coords, t_player *player)
void get_side_dist(t_dda *dda, t_player *player)
{
if (dda->ray_dir.x < 0)
{
dda->side_dist.x = incell_coords.x * dda->delta_dist.x;
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 = (((int)player->coords.x + MAP_CHUNK_SIZE) - player->coords.x) * dda->delta_dist.x;
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 = incell_coords.y * dda->delta_dist.y;
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 = (((int)player->coords.y + MAP_CHUNK_SIZE) - player->coords.y) * dda->delta_dist.y;
dda->side_dist.y = (player->map_coords.y + MAP_CHUNK_SIZE - player->coords.y) * dda->delta_dist.y;
dda->step.y = MAP_CHUNK_SIZE;
}
}
@ -43,17 +44,18 @@ void get_side_dist(t_dda *dda, t_coord incell_coords, t_player *player)
void setup_dda(t_dda (*dda)[WINDOW_X / RAY_SIZE], t_player *player)
{
size_t i;
t_coord incell_coords;
i = 0;
get_ray_angle(player, dda);
incell_coords.x = player->coords.x % MAP_CHUNK_SIZE;
incell_coords.y = player->coords.y % MAP_CHUNK_SIZE;
while (i < WINDOW_X / RAY_SIZE)
{
dda[i]->delta_dist.x = abs((int)(MAP_CHUNK_SIZE / dda[i]->ray_dir.x));
dda[i]->delta_dist.y = abs((int)(MAP_CHUNK_SIZE / dda[i]->ray_dir.y));
get_side_dist(dda[i], incell_coords, player);
(*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

@ -6,30 +6,42 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/06 18:46:56 by adjoly #+# #+# */
/* Updated: 2024/10/13 21:34:01 by adjoly ### ########.fr */
/* 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_coord map_coord)
void while_dda(t_dda (*dda)[WINDOW_X / RAY_SIZE], char **map, t_player *player)
{
(*dda)->wall_hit = false;
while (!(*dda)->wall_hit)
uint16_t i;
t_coord map_coord;
i = 0;
while (i < WINDOW_X / RAY_SIZE)
{
if (( *dda )->side_dist.x < (*dda)->side_dist.y)
(*dda)[i].wall_hit = false;
map_coord.x = player->map_coords.x;
map_coord.y = player->map_coords.y;
while (!(*dda)[i].wall_hit)
{
(*dda)->side_dist.x += (*dda)->delta_dist.x;
map_coord.x += (*dda)->step.x;
(*dda)->wall_side = HORIZONTAL;
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)->side_dist.y += (*dda)->delta_dist.y;
map_coord.y += (*dda)->step.y;
(*dda)->wall_side = VERTICAL;
(*dda)[i].side_dist.y += (*dda)[i].delta_dist.y;
map_coord.y += (*dda)[i].step.y;
(*dda)[i].wall_side = VERTICAL;
}
if (map[map_coord.x][map_coord.y] == '1')
(*dda)->wall_hit = true;
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

@ -6,11 +6,13 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/13 11:05:02 by adjoly #+# #+# */
/* Updated: 2024/10/05 14:30:08 by adjoly ### ########.fr */
/* Updated: 2024/10/24 11:17:06 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/game.h"
#include "game/settings.h"
#include "math.h"
#include <stdio.h>
void change_direction(double speed, bool clockwise, t_player *player)
@ -20,27 +22,13 @@ void change_direction(double speed, bool clockwise, t_player *player)
else
player->direction += speed;
if (player->direction >= 2 * M_PI)
player->direction -= 2 * M_PI;
player->direction = 0;
else if (player->direction < 0)
player->direction += 2 * M_PI;
player->direction = 2 * M_PI;
}
void get_player_image(t_cub *cub, uint8_t key_pressed)
void get_player_image(t_cub *cub)
{
if (key_pressed == W_KEY)
{
cub->player.coords.x += PLAYER_SPEED * cos(cub->player.direction);
cub->player.coords.y += PLAYER_SPEED * sin(cub->player.direction);
}
else if (key_pressed == S_KEY)
{
cub->player.coords.x -= PLAYER_SPEED * cos(cub->player.direction);
cub->player.coords.y -= PLAYER_SPEED * sin(cub->player.direction);
}
else if (key_pressed == D_KEY)
change_direction(PLAYER_ROT_SPEED, false, &cub->player);
else if (key_pressed == A_KEY)
change_direction(PLAYER_ROT_SPEED, true, &cub->player);
print_map(cub);
draw_square(cub, (t_coord){cub->player.coords.x - 2, cub->player.coords.y - 2}, \
5, WHITE);

View File

@ -6,15 +6,15 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/11 16:37:56 by adjoly #+# #+# */
/* Updated: 2024/10/04 15:29:00 by adjoly ### ########.fr */
/* Updated: 2024/10/24 11:41:30 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "mlx.h"
#include "parsing.h"
#include "game/game.h"
void *print_frame(t_cub *cub);
int key_hook(int key, void *param)
{
t_cub *cub;
@ -24,15 +24,25 @@ int key_hook(int key, void *param)
if (key == ESCAPE_KEY)
{
mlx_loop_end(cub->mlx);
mlx_destroy_image(cub->mlx, cub->img);
return (0);
}
if (key == W_KEY || key == S_KEY || key == D_KEY || key == A_KEY)
if (key == W_KEY)
{
mlx_clear_window(cub->mlx, cub->win);
mlx_destroy_image(cub->mlx, cub->img);
cub->img = mlx_new_image(cub->mlx, WINDOW_X, WINDOW_Y);
get_player_image(cub, key);
mlx_put_image_to_window(cub->mlx, cub->win, cub->img, 0, 0);
cub->player.coords.x += PLAYER_SPEED * cos(cub->player.direction);
cub->player.coords.y += PLAYER_SPEED * sin(cub->player.direction);
}
else if (key == S_KEY)
{
cub->player.coords.x -= PLAYER_SPEED * cos(cub->player.direction);
cub->player.coords.y -= PLAYER_SPEED * sin(cub->player.direction);
}
else if (key == D_KEY)
change_direction(PLAYER_ROT_SPEED, false, &cub->player);
else if (key == A_KEY)
change_direction(PLAYER_ROT_SPEED, true, &cub->player);
mlx_destroy_image(cub->mlx, cub->img);
cub->img = print_frame(cub);
//get_player_image(cub);
mlx_put_image_to_window(cub->mlx, cub->win, cub->img, 0, 0);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 21:01:53 by adjoly #+# #+# */
/* Updated: 2024/10/13 21:37:17 by adjoly ### ########.fr */
/* Updated: 2024/10/23 11:19:58 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,7 +41,7 @@ void *print_frame(t_cub *cub)
size_t i;
void *img;
lines = dda(&cub->player, cub->map);
lines = dda(&(cub->player), cub->map);
img = mlx_new_image(cub->mlx, WINDOW_X, WINDOW_Y);
i = 0;
while (i < WINDOW_X / RAY_SIZE)
@ -52,7 +52,7 @@ void *print_frame(t_cub *cub)
stop = lines[i] / 2 + WINDOW_Y / 2;
if (stop >= WINDOW_Y)
stop = WINDOW_Y - 1;
draw_rectangle(cub, img, (t_coord){i * RAY_SIZE, start}, (t_coord){i * RAY_SIZE + RAY_SIZE - 1, stop});
draw_rectangle(cub, img, (t_coord){i * RAY_SIZE, start}, (t_coord){i * RAY_SIZE + RAY_SIZE + 1, stop});
i++;
}
return (img);