Compare commits

16 Commits

Author SHA1 Message Date
hugogogo
6640c9e6d3 wip several test to avoid buble effects, one wihtout doubles 2022-04-28 20:27:46 +02:00
hugogogo
285f6f3c55 add struct wall 2022-04-28 09:35:05 +02:00
hugogogo
5e8afcb245 clean of debug rays vertical and horizontal 2022-04-28 09:03:23 +02:00
hugogogo
4f0e5fcd00 rays verticals and horizontals are working 2022-04-28 08:52:21 +02:00
hugogogo
3501a20abb wip debug ray length 2022-04-28 00:44:02 +02:00
hugogogo
a0f32dee98 plr moves through walls 2022-04-27 00:11:55 +02:00
hugogogo
f3ced10b82 game view in bubble 2022-04-26 22:13:57 +02:00
Hugo LAMY
be650f085e bubble game view with vertical ray pbm 2022-04-26 12:57:22 +02:00
hugogogo
791110c88c merge test 2022-04-25 13:35:25 +02:00
hugogogo
ef4cc1a996 visual 3d with buble effect and rays for slope null 2022-04-25 13:17:55 +02:00
hugogogo
0d9e5879c0 wall grows proportionally 2022-04-25 09:13:10 +02:00
hugogogo
03f61d989b game image created but all wrong 2022-04-24 21:18:36 +02:00
hugogogo
a11ea35ac1 changed functions draw line and pixel to receive an img ptr instead of the whole structure 2022-04-24 19:41:44 +02:00
hugogogo
bb3a9db930 add place for game window 2022-04-24 11:12:39 +02:00
hugogogo
9359e87f6f temp test other way of writting ray end calcul 2022-04-23 12:55:10 +02:00
hugogogo
e9b0dd7ec9 possibility to choose angle of view 2022-04-23 11:58:11 +02:00
9 changed files with 139 additions and 60 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

@@ -17,6 +17,12 @@
# define SCREEN_FOCAL 90
/* size of a cell on the map */
# define CELL 20
/* horizon fr perspectiv */
# define HORIZON 700
/*
* keys macro
*/
/*
* keys macro

View File

@@ -55,8 +55,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,12 +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 hor;
int screen_width;
int screen_height;
int screen_dist;
int cell;
int cell_x;
int cell_y;
@@ -55,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,44 +30,70 @@
}
// tmp end
static void calcul_wall(t_rcast *rcast)
static void wall_length(t_rcast *rcast)
{
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)
height = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y);
if (rcast->is_x == 0 && rcast->slope_x != 0)
height = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x);
// len = sqrt(pow(ray->end.x - ray->start.x, 2) + pow(ray->end.y - ray->start.y, 2));
if (rcast->is_x == 1)
{
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);
}
else if (rcast->is_x == 0)
{
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);
}
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;
rcast->wall.end.y = rcast->screen_height / 2 - height / 2;
}
void draw_column(t_game *game, t_rcast *rcast)
{
t_vec floor;
t_vec ceilling;
t_vec plan;
int color;
plan.start.x = rcast->ray_nb;
plan.end.x = rcast->ray_nb;
if (rcast->wall.start.y > 0)
{
floor.start.x = rcast->ray_nb;
floor.end.x = rcast->ray_nb;
floor.start.y = rcast->screen_height;
floor.end.y = rcast->wall.start.y;
draw_line(&game->img, &floor, 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)
{
ceilling.start.x = rcast->ray_nb;
ceilling.end.x = rcast->ray_nb;
ceilling.start.y = rcast->wall.end.y;
ceilling.end.y = 0;
draw_line(&game->img, &ceilling, 0x00FF0000);
plan.start.y = rcast->wall.end.y;
plan.end.y = 0;
draw_line(&game->img, &plan, 0x000000FF);
}
draw_line(&game->img, &rcast->wall, 0x00FF00FF);
color = 0x00FF00FF;
if (rcast->is_x)
color = 0x00EE00EE;
draw_line(&game->img, &rcast->wall, color);
}
void raycast(t_game *game, t_rcast *rcast)
@@ -84,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

@@ -68,13 +68,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;
// 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);