Merge branch 'hugo'

This commit is contained in:
hugogogo
2022-04-28 20:30:17 +02:00
9 changed files with 123 additions and 66 deletions

View File

@@ -1,3 +1,8 @@
# todo
- resolve view buble
- add textures
# ressources
- [tuto mlx](https://harm-smits.github.io/42docs/libs/minilibx/getting_started.html)

View File

@@ -21,8 +21,8 @@
# define SCREEN_FOCAL 90
/* size of a cell on the map */
# define CELL 20
/* height of a wall without perpectiv */
# define WALL_HEIGHT 200
/* horizon fr perspectiv */
# define HORIZON 700
/*
* keys macro

View File

@@ -72,8 +72,8 @@ void rotate_double(t_plr *plr, t_d_coord *coord);
void plr_turn_right(t_plr *plr);
void plr_turn_left(t_plr *plr);
// player_limits.c
int plr_out_limits(t_game *game, int x, int y);
int is_wall(t_game *game, int cell_x, int cell_y);
int plr_out_limits(t_game *game, int x, int y);
// -------------------------------
// SRC/DRAW

View File

@@ -32,13 +32,13 @@ typedef struct s_vec
typedef struct s_rcast
{
t_vec screen_dist;
t_vec screen_size;
t_vec ray;
t_vec wall;
int wall_height;
int hor;
int screen_width;
int screen_height;
int screen_dist;
int cell;
int cell_x;
int cell_y;
@@ -56,6 +56,7 @@ typedef struct s_rcast
int ray_step_y;
int is_x;
int ray_nb;
int ray_len;
double ratio;
} t_rcast;

View File

@@ -102,6 +102,7 @@ void draw_line(t_img *img, t_vec *vec, int color)
draw_square(game, pos, 0x00999900, 0x00330033, cell, 1);
}
/*
static void draw_screen(t_game *game, t_rcast *rcast)
{
t_vec screen;
@@ -122,6 +123,7 @@ void draw_line(t_img *img, t_vec *vec, int color)
rotate(&(game->plr), &(screen.end));
draw_line(&game->map_img, &screen, 0x00FFFFFF);
}
*/
// tmp end
void draw(t_game *game)
@@ -133,7 +135,7 @@ void draw(t_game *game)
raycast(game, &(game->rcast));
// tmp, to draw map
draw_screen(game, &(game->rcast));
// draw_screen(game, &(game->rcast));
mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, SCREEN_HEIGHT);
// tmp end

View File

@@ -1,48 +1,62 @@
#include "cube3d.h"
static void init_raycast(t_rcast *rcast, t_vec *ray)
static void init_ray_params(t_rcast *rcast, t_vec *ray)
{
rcast->is_x = 0;
// ray sign
rcast->ray_sign_x = 1;
if (ray->start.x < ray->end.x)
rcast->ray_sign_x = -1;
rcast->ray_sign_y = 1;
if (ray->start.y < ray->end.y)
rcast->ray_sign_y = -1;
// cell position of ray
rcast->cell_x = ray->start.x / rcast->cell;
rcast->cell_y = ray->start.y / rcast->cell;
// slope of ray
rcast->slope_x = ray->end.x - ray->start.x;
rcast->slope_y = ray->end.y - ray->start.y;
// direction of next cell
rcast->next_cell_x = -rcast->ray_sign_x;
rcast->next_cell_y = -rcast->ray_sign_y;
// ray steps in both axis
rcast->ray_step_x = ft_abs(rcast->cell * rcast->slope_y);
rcast->ray_step_y = ft_abs(rcast->cell * rcast->slope_x);
}
static void init_first_step(t_rcast *rcast, t_vec *ray)
{
rcast->first_next_x = ray->start.x % rcast->cell;
if (rcast->first_next_x && rcast->ray_sign_x < 0)
rcast->first_next_x = rcast->cell - rcast->first_next_x;
if (!rcast->first_next_x && rcast->ray_sign_x < 0)
rcast->first_next_x = rcast->cell;
rcast->first_next_y = ray->start.y % rcast->cell;
if (rcast->first_next_y && rcast->ray_sign_y < 0)
rcast->first_next_y = rcast->cell - rcast->first_next_y;
if (!rcast->first_next_y && rcast->ray_sign_y < 0)
rcast->first_next_y = rcast->cell;
rcast->next_x = ft_abs(rcast->first_next_x * rcast->slope_y);
rcast->next_y = ft_abs(rcast->first_next_y * rcast->slope_x);
rcast->first_next_x = 0;
if (rcast->slope_x)
rcast->first_next_x = ray->start.x % rcast->cell;
if (rcast->ray_sign_x < 0)
rcast->first_next_x -= rcast->cell;
rcast->first_next_y = 0;
if (rcast->slope_y)
rcast->first_next_y = ray->start.y % rcast->cell;
if (rcast->ray_sign_y < 0)
rcast->first_next_y -= rcast->cell;
rcast->next_x = ft_abs(rcast->first_next_x);
if (rcast->slope_y != 0)
rcast->next_x *= ft_abs(rcast->slope_y);
rcast->next_y = ft_abs(rcast->first_next_y);
if (rcast->slope_x != 0)
rcast->next_y *= ft_abs(rcast->slope_x);
}
static void init_steps(t_rcast *rcast)
{
rcast->ray_step_x = 0;
if (rcast->slope_x)
{
rcast->ray_step_x = ft_abs(rcast->cell);
if (rcast->slope_y)
rcast->ray_step_x *= ft_abs(rcast->slope_y);
}
rcast->ray_step_y = 0;
if (rcast->slope_y)
{
rcast->ray_step_y = ft_abs(rcast->cell);
if (rcast->slope_x)
rcast->ray_step_y *= ft_abs(rcast->slope_x);
}
}
static void next_cell(t_rcast *rcast)
{
if (rcast->next_x > rcast->next_y)
if (!rcast->slope_x || (rcast->next_x > rcast->next_y && rcast->slope_y))
{
rcast->cell_y += rcast->next_cell_y;
rcast->next_y += rcast->ray_step_y;
@@ -64,8 +78,9 @@ void ray_intersect(t_game *game, t_rcast *rcast, t_vec *ray)
ray->end.x += rcast->ray_nb;
ray->end.y = rcast->ray.end.y + game->plr.pos.y;
rotate(&(game->plr), &(ray->end));
init_raycast(rcast, ray);
init_ray_params(rcast, ray);
init_first_step(rcast, ray);
init_steps(rcast);
while (!is_wall(game, rcast->cell_x, rcast->cell_y))
next_cell(rcast);
}

View File

@@ -30,53 +30,65 @@
}
// tmp end
static void calcul_wall(t_rcast *rcast)
static void wall_length(t_rcast *rcast)
{
int length;
int height;
int len;
rcast->wall.start.x = rcast->ray_nb;
rcast->wall.end.x = rcast->ray_nb;
if (rcast->is_x == 1 && rcast->slope_y != 0)
// len = sqrt(pow(ray->end.x - ray->start.x, 2) + pow(ray->end.y - ray->start.y, 2));
if (rcast->is_x == 1)
{
length = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y);
length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_x;
// length = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y);
len = rcast->next_x - rcast->ray_step_x;
if (rcast->slope_y)
len /= ft_abs(rcast->slope_y);
// len = (double)len * (double)rcast->screen_dist.end.y / (double)rcast->slope_x;
// len = rcast->screen_dist * len / (rcast->slope_x);
}
if (rcast->is_x == 0 && rcast->slope_x != 0)
else if (rcast->is_x == 0)
{
length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x);
length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_y;
// length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x);
len = rcast->next_y - rcast->ray_step_y;
if (rcast->slope_x)
len /= ft_abs(rcast->slope_x);
// len = (double)len * (double)rcast->screen_dist.end.y / (double)rcast->slope_y;
// len = rcast->screen_dist * len / (rcast->slope_y);
}
length = ft_abs(length);
height = rcast->wall_height - length;
rcast->ray_len = ft_abs(len);
}
static void wall_height(t_rcast *rcast)
{
int height;
height = rcast->screen_height * (rcast->hor - rcast->ray_len) / rcast->hor;
if (height < 0)
height = 0;
if (height > rcast->screen_height)
height = rcast->screen_height;
rcast->wall.start.y = rcast->screen_height / 2 + height / 2;
rcast->wall.end.y = rcast->screen_height / 2 - height / 2;
}
void draw_column(t_game *game, t_rcast *rcast)
{
t_vec plane;
t_vec plan;
int color;
plane.start.x = rcast->ray_nb;
plane.end.x = rcast->ray_nb;
plan.start.x = rcast->ray_nb;
plan.end.x = rcast->ray_nb;
if (rcast->wall.start.y > 0)
{
plane.start.y = rcast->screen_height;
plane.end.y = rcast->wall.start.y;
draw_line(&game->img, &plane, 0x00FF0000);
plan.start.y = rcast->screen_height;
plan.end.y = rcast->wall.start.y;
draw_line(&game->img, &plan, 0x00FF0000);
}
if (rcast->wall.start.y < rcast->screen_height)
{
plane.start.y = rcast->wall.end.y;
plane.end.y = 0;
draw_line(&game->img, &plane, 0x000000FF);
plan.start.y = rcast->wall.end.y;
plan.end.y = 0;
draw_line(&game->img, &plan, 0x000000FF);
}
color = 0x00FF00FF;
if (rcast->is_x)
@@ -98,7 +110,9 @@ void raycast(t_game *game, t_rcast *rcast)
draw_line(&game->map_img, &ray, 0x00FF00FF);
// tmp end
calcul_wall(rcast);
wall_length(rcast);
wall_height(rcast);
draw_column(game, rcast);
(rcast->ray_nb)++;
}

View File

@@ -83,14 +83,10 @@ static void init_raycast(t_rcast *rcast)
dist = (SCREEN_FOCAL / 2) * M_PI / 180;
dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist));
rcast->screen_dist = dist;
rcast->screen_width = SCREEN_WIDTH;
rcast->screen_height = SCREEN_HEIGHT;
rcast->wall_height = WALL_HEIGHT;
// screen dist
rcast->screen_dist.start.x = 0;
rcast->screen_dist.start.y = 0;
rcast->screen_dist.end.x = 0;
rcast->screen_dist.end.y = -dist;
rcast->hor = HORIZON;
// screen size
rcast->screen_size.start.x = -SCREEN_WIDTH / 2;
rcast->screen_size.start.y = -dist;

View File

@@ -3,12 +3,18 @@
void plr_posx_decrement(t_game *game, t_plr *plr)
{
t_d_coord pos;
int limit_x;
int limit_y;
pos.x = plr->exact.x - PLR_MV;
pos.y = plr->exact.y;
rotate_double(plr, &pos);
if (plr_out_limits(game, pos.x, pos.y))
return ;
limit_x = plr_out_limits(game, pos.x, plr->pos.y);
limit_y = plr_out_limits(game, plr->pos.x, pos.y);
if (limit_x)
pos.x = plr->exact.x;
if (limit_y)
pos.y = plr->exact.y;
plr->exact.x = pos.x;
plr->exact.y = pos.y;
(plr->pos.x) = (int)(pos.x);
@@ -18,12 +24,18 @@ void plr_posx_decrement(t_game *game, t_plr *plr)
void plr_posy_decrement(t_game *game, t_plr *plr)
{
t_d_coord pos;
int limit_x;
int limit_y;
pos.x = plr->exact.x;
pos.y = plr->exact.y - PLR_MV;
rotate_double(plr, &(pos));
if (plr_out_limits(game, pos.x, pos.y))
return ;
limit_x = plr_out_limits(game, pos.x, plr->pos.y);
limit_y = plr_out_limits(game, plr->pos.x, pos.y);
if (limit_x)
pos.x = plr->exact.x;
if (limit_y)
pos.y = plr->exact.y;
plr->exact.x = pos.x;
plr->exact.y = pos.y;
(plr->pos.x) = (int)(pos.x);
@@ -33,12 +45,18 @@ void plr_posy_decrement(t_game *game, t_plr *plr)
void plr_posx_increment(t_game *game, t_plr *plr)
{
t_d_coord pos;
int limit_x;
int limit_y;
pos.x = plr->exact.x + PLR_MV;
pos.y = plr->exact.y;
rotate_double(plr, &(pos));
if (plr_out_limits(game, pos.x, pos.y))
return ;
limit_x = plr_out_limits(game, pos.x, plr->pos.y);
limit_y = plr_out_limits(game, plr->pos.x, pos.y);
if (limit_x)
pos.x = plr->exact.x;
if (limit_y)
pos.y = plr->exact.y;
plr->exact.x = pos.x;
plr->exact.y = pos.y;
(plr->pos.x) = (int)(pos.x);
@@ -48,12 +66,18 @@ void plr_posx_increment(t_game *game, t_plr *plr)
void plr_posy_increment(t_game *game, t_plr *plr)
{
t_d_coord pos;
int limit_x;
int limit_y;
pos.x = plr->exact.x;
pos.y = plr->exact.y + PLR_MV;
rotate_double(plr, &(pos));
if (plr_out_limits(game, pos.x, pos.y))
return ;
limit_x = plr_out_limits(game, pos.x, plr->pos.y);
limit_y = plr_out_limits(game, plr->pos.x, pos.y);
if (limit_x)
pos.x = plr->exact.x;
if (limit_y)
pos.y = plr->exact.y;
plr->exact.x = pos.x;
plr->exact.y = pos.y;
(plr->pos.x) = (int)(pos.x);