player: sliding collisions
This commit is contained in:
parent
74b7eb17ee
commit
8cd885f6f8
@ -18,5 +18,6 @@
|
||||
int map_create(t_game **game, int argc, char **argv);
|
||||
void map_free(t_map *map);
|
||||
void print_map(t_map *map);
|
||||
t_tile get_tile(t_map * map, int x, int y);
|
||||
|
||||
#endif
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include <unistd.h>
|
||||
#include "cub3d.h"
|
||||
#include "libft.h"
|
||||
#include "types.h"
|
||||
|
||||
#define FAILURE 0
|
||||
#define SUCCESS 1
|
||||
@ -232,3 +233,11 @@ void map_free(t_map *map)
|
||||
free(map->grid);
|
||||
free(map);
|
||||
}
|
||||
|
||||
t_tile get_tile(t_map * map, int x, int y)
|
||||
{
|
||||
if (x < 0 || y < 0 || x >= map->width || y >= map->height)
|
||||
return (TILE_WALL);
|
||||
printf("tile at %i %i: %i\n", x, y, map->grid[y][x]);
|
||||
return (map->grid[y][x]);
|
||||
}
|
||||
|
||||
40
src/player.c
40
src/player.c
@ -21,8 +21,8 @@ int player_create(t_game **game)
|
||||
player = malloc(sizeof(t_player));
|
||||
if (!player)
|
||||
return (FAILURE);
|
||||
player->pos.x = 20.f;
|
||||
player->pos.y = 20.f;
|
||||
player->pos.x = 2 * TILE_SIZE;
|
||||
player->pos.y = 2 * TILE_SIZE;
|
||||
player->angle = 0.f;
|
||||
player->speed = 80.f;
|
||||
player->fov = 90.f;
|
||||
@ -30,27 +30,45 @@ int player_create(t_game **game)
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
static void move(t_player *player, int dir, float delta)
|
||||
static int check_horizontal(t_map *map, t_player *player, float xa)
|
||||
{
|
||||
player->pos.x += dir * (sin(player->angle) * player->speed * delta);
|
||||
player->pos.y += dir * -1 * (cos(player->angle) * player->speed * delta);
|
||||
t_tile tile;
|
||||
|
||||
tile = get_tile(map, (int) ((player->pos.x + xa) / TILE_SIZE), (int) ((player->pos.y) / TILE_SIZE));
|
||||
return (tile != TILE_WALL && tile != TILE_VOID);
|
||||
}
|
||||
|
||||
static void rotate(t_player *player, int dir)
|
||||
static int check_vertical(t_map *map, t_player *player, float ya)
|
||||
{
|
||||
player->angle += dir * .1f;
|
||||
t_tile tile;
|
||||
|
||||
tile = get_tile(map, (int) ((player->pos.x) / TILE_SIZE), (int) ((player->pos.y + ya) / TILE_SIZE));
|
||||
return (tile != TILE_WALL && tile != TILE_VOID);
|
||||
}
|
||||
|
||||
static void move(t_map *map, t_player *player, int dir, float delta)
|
||||
{
|
||||
float xa;
|
||||
float ya;
|
||||
|
||||
xa = dir * (sin(player->angle) * player->speed * delta);
|
||||
ya = dir * -1 * (cos(player->angle) * player->speed * delta);
|
||||
if ( xa != 0 && check_horizontal(map, player, xa))
|
||||
player->pos.x += xa;
|
||||
if ( ya != 0 && check_vertical(map, player, ya))
|
||||
player->pos.y += ya;
|
||||
}
|
||||
|
||||
void player_update(t_game *game)
|
||||
{
|
||||
if (get_key(game, MLX_KEY_W))
|
||||
move(game->player, 1, game->screen->mlx->delta_time);
|
||||
move(game->map, game->player, 1, game->screen->mlx->delta_time);
|
||||
else if (get_key(game, MLX_KEY_S))
|
||||
move(game->player, -1, game->screen->mlx->delta_time);
|
||||
move(game->map, game->player, -1, game->screen->mlx->delta_time);
|
||||
if (get_key(game, MLX_KEY_LEFT))
|
||||
rotate(game->player, -1);
|
||||
game->player->angle -= .1f;
|
||||
else if (get_key(game, MLX_KEY_RIGHT))
|
||||
rotate(game->player, 1);
|
||||
game->player->angle += .1f;
|
||||
}
|
||||
|
||||
void player_render(t_screen *screen, t_player *player)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user