player rotates and moves, rays for raycasting on map

This commit is contained in:
Hugo LAMY
2022-04-18 18:27:16 +02:00
parent 00b3612c16
commit 01eb2255d2
21 changed files with 689 additions and 138 deletions

View File

@@ -0,0 +1,30 @@
#include "cube3d.h"
int is_wall(t_game *game, int cell_x, int cell_y)
{
if (cell_x < 0 || cell_y < 0)
return (1);
if (cell_x > game->map_size_x || cell_y > game->map_size_y)
return (1);
if (game->map[cell_y][cell_x] != '0')
return (1);
return (0);
}
int plr_out_limits(t_game *game, int x, int y)
{
int xmax;
int ymax;
int cell_x;
int cell_y;
xmax = game->sizel / (game->bpp / 8);
ymax = game->win_size_y;
cell_x = x / game->cell;
cell_y = y / game->cell;
if (is_wall(game, cell_x, cell_y))
return (1);
if (x < 0 || y < 0 || x > xmax || y > ymax)
return (1);
return (0);
}

View File

@@ -0,0 +1,65 @@
#include "cube3d.h"
void plr_posx_decrement(t_game *game)
{
double pos_x;
double pos_y;
pos_x = game->plr_exact_x - 5;
pos_y = game->plr_exact_y;
rotate_double(game, &pos_x, &pos_y);
if (plr_out_limits(game, pos_x, pos_y))
return ;
game->plr_exact_x = pos_x;
game->plr_exact_y = pos_y;
(game->plr_x) = (int)pos_x;
(game->plr_y) = (int)pos_y;
}
void plr_posy_decrement(t_game *game)
{
double pos_x;
double pos_y;
pos_x = game->plr_exact_x;
pos_y = game->plr_exact_y - 5;
rotate_double(game, &pos_x, &pos_y);
if (plr_out_limits(game, pos_x, pos_y))
return ;
game->plr_exact_x = pos_x;
game->plr_exact_y = pos_y;
(game->plr_x) = (int)pos_x;
(game->plr_y) = (int)pos_y;
}
void plr_posx_increment(t_game *game)
{
double pos_x;
double pos_y;
pos_x = game->plr_exact_x + 5;
pos_y = game->plr_exact_y;
rotate_double(game, &pos_x, &pos_y);
if (plr_out_limits(game, pos_x, pos_y))
return ;
game->plr_exact_x = pos_x;
game->plr_exact_y = pos_y;
(game->plr_x) = (int)pos_x;
(game->plr_y) = (int)pos_y;
}
void plr_posy_increment(t_game *game)
{
double pos_x;
double pos_y;
pos_x = game->plr_exact_x;
pos_y = game->plr_exact_y + 5;
rotate_double(game, &pos_x, &pos_y);
if (plr_out_limits(game, pos_x, pos_y))
return ;
game->plr_exact_x = pos_x;
game->plr_exact_y = pos_y;
(game->plr_x) = (int)pos_x;
(game->plr_y) = (int)pos_y;
}

View File

@@ -0,0 +1,35 @@
#include "cube3d.h"
void plr_turn_left(t_game *game)
{
double radi;
double radj;
if (game->rot == -180)
(game->rot) *= -1;
(game->rot)--;
// calculate trigo for rotations
radi = game->rot * M_PI / 180;
radj = radi + (M_PI / 2);
game->cosi = cos(radi);
game->sini = sin(radi);
game->cosj = cos(radj);
game->sinj = sin(radj);
}
void plr_turn_right(t_game *game)
{
double radi;
double radj;
if (game->rot == 180)
(game->rot) *= -1;
(game->rot)++;
// calculate trigo for rotations
radi = game->rot * M_PI / 180;
radj = radi + (M_PI / 2);
game->cosi = cos(radi);
game->sini = sin(radi);
game->cosj = cos(radj);
game->sinj = sin(radj);
}