mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
feat: utility func to get cardinal direction from angle
This commit is contained in:
parent
6bd9b5e3c2
commit
0afec76ab1
5 changed files with 78 additions and 14 deletions
1
Makefile
1
Makefile
|
|
@ -9,6 +9,7 @@ endif
|
|||
IFLAGS = -I./mlx -I./libft
|
||||
|
||||
SOURCEFILES = \
|
||||
src/consts.c \
|
||||
src/draw/draw_map.c \
|
||||
src/draw/drawutils.c \
|
||||
src/main.c \
|
||||
|
|
|
|||
4
README
4
README
|
|
@ -18,9 +18,9 @@ faire un systeme de "cheats" soit par un menu de debug soit par une ligne de com
|
|||
--[ Correspondances des angles / points cardinaux
|
||||
|
||||
PI [ nord ]
|
||||
|
|
||||
5 PI / 4 [NO] | 3 PI / 4 [NE]
|
||||
|
|
||||
3 PI / 2 [ouest] ----------- PI / 2 [est]
|
||||
|
|
||||
|
|
||||
7 PI / 4 [SO] | PI / 4 [SE]
|
||||
0 [ sud ]
|
||||
|
|
|
|||
23
src/consts.c
Normal file
23
src/consts.c
Normal 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;
|
||||
13
src/consts.h
13
src/consts.h
|
|
@ -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
|
||||
|
|
|
|||
47
src/main.c
47
src/main.c
|
|
@ -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;
|
||||
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;
|
||||
float start_angle = data->player.yaw - M_PI / 6;
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue