Merge branch 'Algo' into _mylan2

This commit is contained in:
mcolonna 2024-10-15 14:03:58 +02:00
commit 59041e5e6d
5 changed files with 180 additions and 199 deletions

View file

@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: greg <greg@student.42.fr> +#+ +:+ +#+ #
# By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/07/29 13:08:42 by greg #+# #+# #
# Updated: 2024/10/15 12:53:36 by marvin ### ########.fr #
# Updated: 2024/10/15 13:01:09 by mcolonna ### ########.fr #
# #
# **************************************************************************** #

34
algo.c
View file

@ -6,48 +6,16 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
/* Updated: 2024/10/15 12:54:19 by mcolonna ### ########.fr */
/* Updated: 2024/10/15 13:13:13 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
#include <math.h>
#include <stdio.h>
#include <X11/X.h>
#include <stdbool.h>
void *g_mlx = NULL;
void *g_win = NULL;
t_ray g_ray;
// int worldMap[mapWidth][mapHeight]=
// {
// {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
// {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1},
// {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1},
// {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1},
// {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
// {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
// };
int worldMap[mapWidth][mapHeight] =
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},

8
algo.h
View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
/* Updated: 2024/10/15 12:54:12 by mcolonna ### ########.fr */
/* Updated: 2024/10/15 13:10:34 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,8 +19,9 @@
#include "Minilibx/mlx.h"
#include "Minilibx/mlx_int.h"
# include "Libft/libft.h"
#define mapWidth 24 // cases
#define mapHeight 24 // cases
#define mapWidth 24 // cases (TODO test)
#define mapHeight 24 // cases (TODO test)
#define MOVE_SPEED 0.1 // cases
#define PI 3.1415926535
#define ROT_SPEED (PI / 16) // rad
@ -37,6 +38,7 @@ typedef struct s_ray
double posX;
// pos player on map (cases)
double posY;
// player rotation (rad)
double rot;
} t_ray;

66
move.c
View file

@ -6,68 +6,64 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */
/* Updated: 2024/10/15 12:52:05 by mcolonna ### ########.fr */
/* Updated: 2024/10/15 13:27:10 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
int keypress(int keycode)
static int move_forward(void)
{
double dirX, dirY, planeX, planeY;
double dirX, dirY;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
vector_from_rotation(&planeX, &planeY, g_ray.rot + PI/2, FOV);
//move forward if no wall in front of you
if (keycode == XK_Up || keycode == XK_z || keycode == XK_w)
{
if (worldMap[(int)((g_ray.posX + dirX * MOVE_SPEED))][(int)(g_ray.posY)] != 1)
g_ray.posX += dirX * MOVE_SPEED;
if (worldMap[(int)(g_ray.posX)][(int)(g_ray.posY + dirY * MOVE_SPEED)] != 1)
g_ray.posY += dirY * MOVE_SPEED;
return(0);
}
//move backwards if no wall behind you
if (keycode == XK_Down || keycode == XK_s)
static int move_backward(void)
{
double dirX, dirY;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
if (worldMap[(int)(g_ray.posX - dirX * MOVE_SPEED)][(int)(g_ray.posY)] != 1)
g_ray.posX -= dirX * MOVE_SPEED;
if (worldMap[(int)(g_ray.posX)][(int)(g_ray.posY - dirY * MOVE_SPEED)] != 1)
g_ray.posY -= dirY * MOVE_SPEED;
return(0);
}
//rotate to the right
if (keycode == XK_Right || keycode == XK_d)
static int move_right(void)
{
g_ray.rot += ROT_SPEED;
// double oldDirX;
// double oldPlaneX;
// //both camera direction and camera plane must be rotated
// oldDirX = dirX;
// dirX = dirX * cos(-ROT_SPEED) - dirY * sin(-ROT_SPEED);
// dirY = oldDirX * sin(-ROT_SPEED) + dirY * cos(-ROT_SPEED);
// oldPlaneX = planeX;
// planeX = planeX * cos(-ROT_SPEED) - planeY * sin(-ROT_SPEED);
// planeY = oldPlaneX * sin(-ROT_SPEED) + planeY * cos(-ROT_SPEED);
return (0);
}
//rotate to the left
if (keycode == XK_Left || keycode == XK_q || keycode == XK_a)
static int move_left(void)
{
g_ray.rot -= ROT_SPEED;
// double oldDirX;
// double oldPlaneX;
// //both camera direction and camera plane must be rotated
// oldDirX = dirX;
// dirX = dirX * cos(ROT_SPEED) - dirY * sin(ROT_SPEED);
// dirY = oldDirX * sin(ROT_SPEED) + dirY * cos(ROT_SPEED);
// oldPlaneX = planeX;
// planeX = planeX * cos(ROT_SPEED) - planeY * sin(ROT_SPEED);
// planeY = oldPlaneX * sin(ROT_SPEED) + planeY * cos(ROT_SPEED);
return (0);
}
int keypress(int keycode)
{
if (keycode == XK_Up || keycode == XK_z || keycode == XK_w)
move_forward();
if (keycode == XK_Down || keycode == XK_s)
move_backward();
if (keycode == XK_Right || keycode == XK_d)
move_right();
if (keycode == XK_Left || keycode == XK_q || keycode == XK_a)
move_left();
if (keycode == XK_Escape)
exit(0);
printf("x:%f y:%f\n", g_ray.posX, g_ray.posY);
// render the updated frame after key press
{
mlx_destroy_window(g_mlx, g_win);
exit(0); // TODO leaks?
}
// render the updated frame after key pressd
draw_screen();
return (0);
}

137
render.c
View file

@ -5,81 +5,81 @@
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:40:52 by mcolonna #+# #+# */
/* Updated: 2024/10/15 12:51:25 by mcolonna ### ########.fr */
/* Created: 2024/10/14 14:55:05 by greg #+# #+# */
/* Updated: 2024/10/15 13:52:35 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
int render(u_int32_t *img_data)
static void calculate_perpwalldist3(int x,
double *raydirX, double *raydirY,
double *deltadistX, double *deltadistY)
{
int x; // px
// clear image data
ft_bzero(img_data, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(int)); // TODO why
// 1
double dirX, dirY, planeX, planeY;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
vector_from_rotation(&planeX, &planeY, g_ray.rot + PI/2, FOV);
x = 0;
while (x < SCREEN_WIDTH)
{
// case player on map (cases)
int mapX = (int)g_ray.posX;
// case player on map (cases)
int mapY = (int)g_ray.posY;
// vector ray direction (1)
double raydirX, raydirY;
{
// ray direction in [-1;+1]. the highter the more to the right (1)
double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1;
raydirX = dirX + planeX * ray_direction;
raydirY = dirY + planeY * ray_direction;
double dirX;
double dirY;
double planeX;
double planeY;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
vector_from_rotation(&planeX, &planeY, g_ray.rot + PI / 2, 1);
*raydirX = dirX + planeX * ray_direction;
*raydirY = dirY + planeY * ray_direction;
*deltadistX = (raydirX == 0) ? 1e30 : fabs(1 / *raydirX);
*deltadistY = (raydirY == 0) ? 1e30 : fabs(1 / *raydirY);
}
// longueur du rayon dans une même colonne de cases (cases)
double deltadistX = (raydirX == 0) ? 1e30 : fabs(1 / raydirX);
// longueur du rayon dans une même ligne de cases (cases)
double deltadistY = (raydirY == 0) ? 1e30 : fabs(1 / raydirY);
// longueur du rayon entre pos actuelle et pos du prochain coté ouest ou est d'une box
double sidedistX;
// longueur du rayon entre pos actuelle et pos du prochain coté nord ou sub d'une box
double sidedistY;
// direction du rayon sur x (+1 or -1)
int stepX;
// direction du rayon sur y (+1 or -1)
int stepY;
static void calculate_perpwalldist2(int x,
double *sidedistX, double *sidedistY,
double *deltadistX, double *deltadistY,
int *stepX, int *stepY)
{
int mapX = (int)g_ray.posX;
int mapY = (int)g_ray.posY;
double raydirX;
double raydirY;
calculate_perpwalldist3(x, &raydirX, &raydirY, deltadistX, deltadistY);
if (raydirX < 0)
{
stepX = -1;
sidedistX = (g_ray.posX - mapX) * deltadistX;
*stepX = -1;
*sidedistX = (g_ray.posX - mapX) * *deltadistX;
}
else
{
stepX = 1;
sidedistX = (mapX + 1.0 - g_ray.posX) * deltadistX;
*stepX = 1;
*sidedistX = (mapX + 1.0 - g_ray.posX) * *deltadistX;
}
if (raydirY < 0)
{
stepY = -1;
sidedistY = (g_ray.posY - mapY) * deltadistY;
*stepY = -1;
*sidedistY = (g_ray.posY - mapY) * *deltadistY;
}
else
{
stepY = 1;
sidedistY = (mapY + 1.0 - g_ray.posY) * deltadistY;
*stepY = 1;
*sidedistY = (mapY + 1.0 - g_ray.posY) * *deltadistY;
}
}
bool hit = false;
/// @brief Calculate the distance of the wall perpendicularly to the camera
/// plane.
///
/// @param x Ray direction x between 0 and 1.
/// @return Result.
static double calculate_perpwalldist(int x)
{
int mapX = (int)g_ray.posX;
int mapY = (int)g_ray.posY;
bool side;
while(!hit)
double sidedistX;
double sidedistY;
double deltadistX;
double deltadistY;
int stepX;
int stepY;
calculate_perpwalldist2(x, &sidedistX, &sidedistY, &deltadistX, &deltadistY, &stepX, &stepY);
while(worldMap[mapX][mapY] != 1)
{
// jump to next map square, either in x-direction, or in y-direction
if(sidedistX < sidedistY)
@ -94,30 +94,28 @@ int render(u_int32_t *img_data)
mapY += stepY;
side = true;
}
// check if ray has hit a wall
if(worldMap[mapX][mapY] == 1)
hit = true;
}
// calcul lenght of ray (shortest perpendicular distance between wall and camera plane) (1)
double perpwalldist;
if(!side)
perpwalldist = (sidedistX - deltadistX);
else
perpwalldist = (sidedistY - deltadistY);
return (perpwalldist);
}
// calculate height of line to draw on screen (px)
/// @brief Draw a vertical line according to the ray direction x.
///
/// @param x Ray direction x between 0 and 1.
static void draw_vertical_line(int x, u_int32_t *img_data)
{
double perpwalldist = calculate_perpwalldist(x);
int lineHeight = (int)(SCREEN_HEIGHT / perpwalldist);
// calculate lowest and highest pixel to fill in current stripe
int drawStart = -lineHeight / 2 + SCREEN_HEIGHT / 2;
if(drawStart < 0)
drawStart = 0;
int drawEnd = lineHeight / 2 + SCREEN_HEIGHT / 2;
if(drawEnd >= SCREEN_HEIGHT)
drawEnd = SCREEN_HEIGHT - 1;
// draw vertical line
int y = 0;
while (y < drawStart)
{
@ -134,7 +132,24 @@ int render(u_int32_t *img_data)
img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR;
y++;
}
}
int render(u_int32_t *img_data)
{
int x; // px
// clear image data
ft_bzero(img_data, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(int)); // TODO why
// 1
double dirX, dirY, planeX, planeY;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
vector_from_rotation(&planeX, &planeY, g_ray.rot + PI/2, FOV);
x = 0;
while (x < SCREEN_WIDTH)
{
draw_vertical_line(x, img_data);
x++;
}