Add dda and raycasting start
This commit is contained in:
parent
da452769c2
commit
65a622bf10
BIN
game/.game.h.swp
Normal file
BIN
game/.game.h.swp
Normal file
Binary file not shown.
121
game/dda.c
Normal file
121
game/dda.c
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* dda.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/05/02 15:49:00 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/05/02 16:58:31 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "game.h"
|
||||||
|
|
||||||
|
void init_dda(t_game *game, t_dda *dda)
|
||||||
|
{
|
||||||
|
t_ray *ray
|
||||||
|
|
||||||
|
ray = game->ray;
|
||||||
|
dda->map_x = (int)game->ply->pos_x;
|
||||||
|
dda->map_y = (int)game->ply->pos_y;
|
||||||
|
if (ray->dir_x == 0)
|
||||||
|
dda->delta_dist_x = 1e30;
|
||||||
|
else
|
||||||
|
dda->delta_dist_x = abs(1 / ray->dir_x);
|
||||||
|
if (ray->dir_y == 0)
|
||||||
|
dda->delta_dist_y = 1e30;
|
||||||
|
else
|
||||||
|
dda->delta_dist_y = abs(1 / ray->dir_y);
|
||||||
|
dda->hit = 0;
|
||||||
|
if (ray->dir_x < 0)
|
||||||
|
{
|
||||||
|
dda->step_x = -1;
|
||||||
|
dda->side_dist_x = (game->ply->pos_x - dda->map_x) * dda->delta_dist_x;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dda->step_x = 1;
|
||||||
|
dda->side_dist_x = (dda->map_x + 1.0 - game->ply->pos_y) * dda->delta_dist_x;
|
||||||
|
}
|
||||||
|
if (ray->dir_y < 0)
|
||||||
|
{
|
||||||
|
dda->step_y = -1;
|
||||||
|
dda->side_dist_y = (game->ply->pos_y - dda->map_y) * dda->delta_dist_y;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dda->step_y = 1;
|
||||||
|
dda->side_dist_y = (dda->map_y + 1.0 - game->ply->pos_y) * dda->delta_dist_y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop_dda(t_game *game, t_dda *dda)
|
||||||
|
{
|
||||||
|
while (dda->hit == 0)
|
||||||
|
{
|
||||||
|
if (dda->side_dist_x < dda->side_dist_y)
|
||||||
|
{
|
||||||
|
dda->side_dist_x += dda->delta_dist_x;
|
||||||
|
dda->map_x += dda->step_x;
|
||||||
|
dda->side = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dda->side_dist_y += dda->delta_dist_y;
|
||||||
|
dda->map_y += dda->step_y;
|
||||||
|
dda->side = 1;
|
||||||
|
}
|
||||||
|
if (game->map->map[dda->map_x][dda->map_y] > 0)
|
||||||
|
dda->hit = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ray_dda(t_game *game, t_dda *dda)
|
||||||
|
{
|
||||||
|
if(dda->side == 0)
|
||||||
|
dda->perp_wall_dist = (dda->side_dist_x - dda->delta_dist_x);
|
||||||
|
else
|
||||||
|
dda->perp_wall_dist = (dda->side_dist_y - dda->delta_dist_y);
|
||||||
|
dda->line_height = (int)(HEIGHT / dda->perp_wall_dist);
|
||||||
|
dda->draw_start = -dda->line_height / 2 + HEIGHT / 2;
|
||||||
|
if(dda->draw_start < 0)
|
||||||
|
dda->draw_start = 0;
|
||||||
|
dda->draw_end = dda->line_height / 2 + HEIGHT / 2;
|
||||||
|
if(draw_end >= HEIGHT)
|
||||||
|
draw_end = HEIGHT - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int create_trgb(int t, int r, int g, int b)
|
||||||
|
{
|
||||||
|
return (t << 24 | r << 16 | g << 8 | b);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void draw_dda(t_game *game, t_dda *dda)
|
||||||
|
{
|
||||||
|
int color;
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
int r = rand() % 4;
|
||||||
|
if (r == 0)
|
||||||
|
color = create_trgv(255, 150, 10, 200);
|
||||||
|
else if (r == 1)
|
||||||
|
color = create_trgv(255, 10, 150, 200);
|
||||||
|
else if (r == 2)
|
||||||
|
color = create_trgv(255, 200, 150, 10);
|
||||||
|
else if (r == 3)
|
||||||
|
color = create_trgv(255, 10, 200, 150);
|
||||||
|
|
||||||
|
mlx_put_pixel(game->mlx->window, abs(x), y,
|
||||||
|
}
|
||||||
|
|
||||||
|
void dda(t_game *game, t_dda *dda)
|
||||||
|
{
|
||||||
|
init_dda(game, dda);
|
||||||
|
loop_dda(game, dda);
|
||||||
|
ray_dda(game, dda);
|
||||||
|
draw_dda(game, dda);
|
||||||
|
}
|
21
game/game.h
21
game/game.h
@ -6,7 +6,7 @@
|
|||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */
|
/* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */
|
||||||
/* Updated: 2023/05/02 15:32:43 by erey-bet ### ########.fr */
|
/* Updated: 2023/05/02 15:56:17 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -31,10 +31,25 @@ typedef struct s_player
|
|||||||
|
|
||||||
typedef struct s_raycast
|
typedef struct s_raycast
|
||||||
{
|
{
|
||||||
double x;
|
double dir_x;
|
||||||
double y;
|
double dir_y;
|
||||||
} t_ray;
|
} t_ray;
|
||||||
|
|
||||||
|
typedef struct s_dda
|
||||||
|
{
|
||||||
|
int map_x;
|
||||||
|
int map_y;
|
||||||
|
double side_dist_x;
|
||||||
|
double side_dist_y;
|
||||||
|
double delta_dist_x;
|
||||||
|
double delta_dist_y;
|
||||||
|
double perp_wall_dist;
|
||||||
|
int step_x;
|
||||||
|
int step_y;
|
||||||
|
int hit;
|
||||||
|
int side;
|
||||||
|
} t_dda;
|
||||||
|
|
||||||
typedef struct s_game
|
typedef struct s_game
|
||||||
{
|
{
|
||||||
mlx_t *mlx;
|
mlx_t *mlx;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/27 14:30:29 by erey-bet #+# #+# */
|
/* Created: 2023/04/27 14:30:29 by erey-bet #+# #+# */
|
||||||
/* Updated: 2023/05/02 13:12:15 by erey-bet ### ########.fr */
|
/* Updated: 2023/05/02 15:56:09 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -25,8 +25,8 @@ int ray(t_game *game)
|
|||||||
while (i <= WIDTH)
|
while (i <= WIDTH)
|
||||||
{
|
{
|
||||||
camera = 2 * i / (double)WIDTH - 1; //x-coordinate in camera space
|
camera = 2 * i / (double)WIDTH - 1; //x-coordinate in camera space
|
||||||
ray->x = p->dir_x + p->pla_x * camera;
|
ray->dir_x = p->dir_x + p->pla_x * camera;
|
||||||
ray->y = p->dir_y + p->pla_y * camera;
|
ray->dir_y = p->dir_y + p->pla_y * camera;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user