Merge remote-tracking branch 'origin/quinten' into willem

This commit is contained in:
whaffman 2025-04-22 15:43:38 +02:00
commit 93216e6f76
10 changed files with 103 additions and 26 deletions

21
inc/collision.h Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* collision.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 14:40:47 by qmennen #+# #+# */
/* Updated: 2025/04/22 14:42:35 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef COLLISION_H
# define COLLISION_H
# include "cub3d.h"
int collision_horizontal(t_map *map, t_player *player, float xa);
int collision_vertical(t_map *map, t_player *player, float ya);
#endif

View File

@ -45,6 +45,7 @@
# include "hooks.h"
# include "render.h"
# include "player.h"
# include "collision.h"
# include "parser.h"
#endif

View File

@ -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

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 18:53:27 by qmennen #+# #+# */
/* Updated: 2025/04/17 19:49:36 by qmennen ### ########.fr */
/* Updated: 2025/04/22 15:23:45 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,6 @@
int player_create(t_game **game);
void player_render(t_screen *screen, t_player *player);
void player_update(t_game *game);
void player_update(t_game *game, float delta_time);
#endif

View File

@ -33,8 +33,9 @@ typedef struct s_vec2
typedef struct s_player
{
t_vec2 pos;
t_vec2 dir;
t_vec2 camera;
float speed;
float angle;
float fov;
} t_player;

30
src/collision.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* collision.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
/* Updated: 2025/04/22 14:42:09 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "collision.h"
int collision_horizontal(t_map *map, t_player *player, float xa)
{
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);
}
int collision_vertical(t_map *map, t_player *player, float ya)
{
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);
}

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/04/22 12:58:50 by whaffman ######## odam.nl */
/* Updated: 2025/04/22 15:42:09 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -45,10 +45,12 @@ void free_game(t_game **game)
void game_loop(void *param)
{
t_game *game;
float delta_time;
game = (t_game *)param;
delta_time = game->screen->mlx->delta_time;
render_clear(game->screen);
player_update(game);
player_update(game, delta_time);
render_entities(game);
render_map(game->screen, game->map);
keyboard_update(game); // Goes last

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:20:51 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:20:52 by whaffman ######## odam.nl */
/* Updated: 2025/04/22 15:43:35 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
#include <unistd.h>
#include "cub3d.h"
#include "libft.h"
#include "types.h"
#define FAILURE 0
#define SUCCESS 1
@ -247,3 +248,10 @@ void map_free(t_map *map)
free(map->EA_texture);
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);
return (map->grid[y][x]);
}

View File

@ -10,9 +10,7 @@
/* */
/* ************************************************************************** */
#include "player.h"
#include "render.h"
#include "types.h"
#include "cub3d.h"
int player_create(t_game **game)
{
@ -21,36 +19,51 @@ 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->angle = 0.f;
player->pos.x = 2 * TILE_SIZE;
player->pos.y = 2 * TILE_SIZE;
player->dir.x = 1;
player->dir.y = 0;
player->camera.x = 0;
player->camera.y = 0.66f;
player->speed = 80.f;
player->fov = 90.f;
(*game)->player = player;
return (SUCCESS);
}
static void move(t_player *player, int dir, float delta)
static void move(t_map *map, t_player *player, int dir, float delta)
{
player->pos.x += dir * (sin(player->angle) * player->speed * delta);
player->pos.y += dir * -1 * (cos(player->angle) * player->speed * delta);
float xa;
float ya;
xa = dir * player->dir.x * player->speed * delta;
ya = dir * -1 * player->dir.y * player->speed * delta;
if ( xa != 0 && collision_horizontal(map, player, xa))
player->pos.x += xa;
if ( ya != 0 && collision_vertical(map, player, ya))
player->pos.y += ya;
}
static void rotate(t_player *player, int dir)
static void rotate(t_player *player, float rot_speed)
{
player->angle += dir * .1f;
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, float delta_time)
{
if (get_key(game, MLX_KEY_W))
move(game->player, 1, game->screen->mlx->delta_time);
move(game->map, game->player, 1, 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, delta_time);
if (get_key(game, MLX_KEY_LEFT))
rotate(game->player, -1);
rotate(game->player, .1f);
else if (get_key(game, MLX_KEY_RIGHT))
rotate(game->player, 1);
rotate(game->player, -.1f);
}
void player_render(t_screen *screen, t_player *player)
@ -60,7 +73,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)
return ;
render_circle(screen, player->pos, 4, 0x3294a8ff);
direction.x = player->pos.x + sin(player->angle) * 30;
direction.y = player->pos.y - cos(player->angle) * 30;
direction.x = player->pos.x + player->dir.x * 30;
direction.y = player->pos.y - player->dir.y * 30;
render_line(screen, player->pos, direction, 0xa83232ff);
}

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
(*game)->keyboard = keyboard;
i = 0;
while (i < NUM_KEYS)
while (i < NUM_KEYS) // TODO: Better
{
(*game)->keyboard->keys[i] = 0;
i++;