feat: utility func to get cardinal direction from angle

This commit is contained in:
Khaïs COLIN 2025-07-28 14:10:01 +02:00 committed by freddy_vqr
parent dd82d902a5
commit 8fd70cb18a
5 changed files with 78 additions and 14 deletions

23
src/consts.c Normal file
View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* consts.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/28 14:33:12 by kcolin #+# #+# */
/* Updated: 2025/07/28 15:03:16 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "consts.h"
#include <math.h>
const float g_north = M_PI;
const float g_south = 0;
const float g_east = M_PI / 2;
const float g_west = 3 * M_PI / 2;
const float g_northwest = 5 * M_PI / 4;
const float g_northeast = 3 * M_PI / 4;
const float g_southwest = 7 * M_PI / 4;
const float g_southeast = M_PI / 4;

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */
/* Updated: 2025/07/25 14:53:56 by tchampio ### ########.fr */
/* Updated: 2025/07/28 15:03:19 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,8 +15,19 @@
# define HEIGHT 800
# define WIDTH 800
# define SIZE 64
# define MAP_SIZE 10
extern const float g_north;
extern const float g_south;
extern const float g_east;
extern const float g_west;
extern const float g_northwest;
extern const float g_northeast;
extern const float g_southwest;
extern const float g_southeast;
# define RESSOURCE_DIR "ressources"
# define MOVEMENT_SPEED 6.4
# define ROTATION_SPEED 0.1

View file

@ -17,6 +17,8 @@
#include "draw/drawutils.h"
#include "map/map_checker.h"
#include "draw/draw_map.h"
#include "map/mapdata.h"
#include "player/player.h"
#include "utils/hooks.h"
#include "utils/frees.h"
#include <stdbool.h>
@ -29,8 +31,8 @@
void init_player(t_mapdata *mapdata, t_player *player)
{
player->health = 100;
player->x = mapdata->startx;
player->y = mapdata->starty;
player->x = mapdata->startx * SIZE;
player->y = mapdata->starty * SIZE;
if (mapdata->map[mapdata->starty][mapdata->startx] == 'N')
player->yaw = M_PI;
else if (mapdata->map[mapdata->starty][mapdata->startx] == 'S')
@ -53,10 +55,10 @@ bool touch(float px, float py, t_mapdata *map)
return (false);
}
void draw_line(t_cub3d_data *data, float start_x)
void draw_line(t_cub3d_data *data, float start_angle)
{
float cos_angle = cos(start_x);
float sin_angle = sin(start_x);
float cos_angle = cos(start_angle);
float sin_angle = sin(start_angle);
float ray_x = data->player.x;
float ray_y = data->player.y;
@ -68,19 +70,46 @@ void draw_line(t_cub3d_data *data, float start_x)
}
}
t_direction angle_dir(float angle)
{
if (angle >= g_southwest || (angle > g_south && angle < g_southeast))
return (SOUTH);
if (angle >= g_southeast && angle < g_northeast)
return (EAST);
if (angle >= g_northeast && angle < g_northwest)
return (NORTH);
return (WEST);
}
t_direction player_dir(t_player player)
{
return (angle_dir(player.yaw));
}
int game_loop(t_cub3d_data *data)
{
mlx_destroy_image(data->mlx, data->img_data->img);
data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
int column = 0;
float fraction = M_PI / 3 / WIDTH;
float start_angle = data->player.yaw - M_PI / 6;
if (player_dir(data->player) == SOUTH)
ft_printf("south\n");
if (player_dir(data->player) == EAST)
ft_printf("east\n");
if (player_dir(data->player) == NORTH)
ft_printf("north\n");
if (player_dir(data->player) == WEST)
ft_printf("west\n");
float ray_angle = data->player.yaw - M_PI / 6;
float fraction = M_PI / 3 / WIDTH;
int column = 0;
while (column < WIDTH)
{
draw_line(data, start_angle);
start_angle += fraction;
// hline check
/* float ray_atan = atan(ray_angle); */
draw_line(data, ray_angle);
column++;
ray_angle += fraction;
}
draw_map(data->map, &data->player, data->img_data);