player: direction vector
This commit is contained in:
parent
1252edf661
commit
89e0ef21c9
@ -33,8 +33,8 @@ typedef struct s_vec2
|
|||||||
typedef struct s_player
|
typedef struct s_player
|
||||||
{
|
{
|
||||||
t_vec2 pos;
|
t_vec2 pos;
|
||||||
|
t_vec2 dir;
|
||||||
float speed;
|
float speed;
|
||||||
float angle;
|
|
||||||
float fov;
|
float fov;
|
||||||
} t_player;
|
} t_player;
|
||||||
|
|
||||||
|
|||||||
24
src/player.c
24
src/player.c
@ -21,7 +21,8 @@ int player_create(t_game **game)
|
|||||||
return (FAILURE);
|
return (FAILURE);
|
||||||
player->pos.x = 2 * TILE_SIZE;
|
player->pos.x = 2 * TILE_SIZE;
|
||||||
player->pos.y = 2 * TILE_SIZE;
|
player->pos.y = 2 * TILE_SIZE;
|
||||||
player->angle = 0.f;
|
player->dir.x = 1;
|
||||||
|
player->dir.y = 0;
|
||||||
player->speed = 80.f;
|
player->speed = 80.f;
|
||||||
player->fov = 90.f;
|
player->fov = 90.f;
|
||||||
(*game)->player = player;
|
(*game)->player = player;
|
||||||
@ -34,14 +35,23 @@ static void move(t_map *map, t_player *player, int dir, float delta)
|
|||||||
float xa;
|
float xa;
|
||||||
float ya;
|
float ya;
|
||||||
|
|
||||||
xa = dir * (sin(player->angle) * player->speed * delta);
|
xa = dir * player->dir.x * player->speed * delta;
|
||||||
ya = dir * -1 * (cos(player->angle) * player->speed * delta);
|
ya = dir * -1 * player->dir.y * player->speed * delta;
|
||||||
if ( xa != 0 && collision_horizontal(map, player, xa))
|
if ( xa != 0 && collision_horizontal(map, player, xa))
|
||||||
player->pos.x += xa;
|
player->pos.x += xa;
|
||||||
if ( ya != 0 && collision_vertical(map, player, ya))
|
if ( ya != 0 && collision_vertical(map, player, ya))
|
||||||
player->pos.y += ya;
|
player->pos.y += ya;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void rotate(t_player *player, float rot_speed)
|
||||||
|
{
|
||||||
|
double old_x;
|
||||||
|
|
||||||
|
old_x = player->dir.x;
|
||||||
|
player->dir.x = player->dir.x * cos(rot_speed) - player->dir.y * sin(rot_speed);
|
||||||
|
player->dir.y = old_x * sin(rot_speed) + player->dir.y * cos(rot_speed);
|
||||||
|
}
|
||||||
|
|
||||||
void player_update(t_game *game)
|
void player_update(t_game *game)
|
||||||
{
|
{
|
||||||
if (get_key(game, MLX_KEY_W))
|
if (get_key(game, MLX_KEY_W))
|
||||||
@ -49,9 +59,9 @@ void player_update(t_game *game)
|
|||||||
else if (get_key(game, MLX_KEY_S))
|
else if (get_key(game, MLX_KEY_S))
|
||||||
move(game->map, 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))
|
if (get_key(game, MLX_KEY_LEFT))
|
||||||
game->player->angle -= .1f;
|
rotate(game->player, .1f);
|
||||||
else if (get_key(game, MLX_KEY_RIGHT))
|
else if (get_key(game, MLX_KEY_RIGHT))
|
||||||
game->player->angle += .1f;
|
rotate(game->player, -.1f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void player_render(t_screen *screen, t_player *player)
|
void player_render(t_screen *screen, t_player *player)
|
||||||
@ -61,7 +71,7 @@ void player_render(t_screen *screen, t_player *player)
|
|||||||
if (player->pos.x < 0 || player->pos.x >= screen->width || player->pos.y < 0 || player->pos.y >= screen->height)
|
if (player->pos.x < 0 || player->pos.x >= screen->width || player->pos.y < 0 || player->pos.y >= screen->height)
|
||||||
return ;
|
return ;
|
||||||
render_circle(screen, player->pos, 4, 0x3294a8ff);
|
render_circle(screen, player->pos, 4, 0x3294a8ff);
|
||||||
direction.x = player->pos.x + sin(player->angle) * 30;
|
direction.x = player->pos.x + player->dir.x * 30;
|
||||||
direction.y = player->pos.y - cos(player->angle) * 30;
|
direction.y = player->pos.y - player->dir.y * 30;
|
||||||
render_line(screen, player->pos, direction, 0xa83232ff);
|
render_line(screen, player->pos, direction, 0xa83232ff);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/17 19:29:29 by qmennen #+# #+# */
|
/* Created: 2025/04/17 19:29:29 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/04/17 19:59:53 by qmennen ### ########.fr */
|
/* Updated: 2025/04/22 14:46:18 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ int keyboard_create(t_game **game)
|
|||||||
return (FAILURE);
|
return (FAILURE);
|
||||||
(*game)->keyboard = keyboard;
|
(*game)->keyboard = keyboard;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < NUM_KEYS)
|
while (i < NUM_KEYS) // TODO: Better
|
||||||
{
|
{
|
||||||
(*game)->keyboard->keys[i] = 0;
|
(*game)->keyboard->keys[i] = 0;
|
||||||
i++;
|
i++;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user