refactor: floats to doubles

This commit is contained in:
Quinten Mennen 2025-05-06 15:23:53 +02:00
parent 15c47e36ac
commit 06156e5436
15 changed files with 276 additions and 285 deletions

View File

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

View File

@ -6,17 +6,17 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 18:53:27 by qmennen #+# #+# */ /* Created: 2025/04/15 18:53:27 by qmennen #+# #+# */
/* Updated: 2025/04/22 15:23:45 by qmennen ### ########.fr */ /* Updated: 2025/05/06 15:20:37 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef PLAYER_H #ifndef PLAYER_H
# define PLAYER_H #define PLAYER_H
# include "cub3d.h" #include "cub3d.h"
int player_create(t_game **game); int player_create(t_game **game);
void player_render(t_screen *screen, t_player *player); void player_render(t_screen *screen, t_player *player);
void player_update(t_game *game, float delta_time); void player_update(t_game *game, double delta_time);
#endif #endif

View File

@ -1,19 +1,19 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* types.h :+: :+: */ /* types.h :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ /* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
/* Updated: 2025/05/06 13:58:16 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:37 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef TYPES_H #ifndef TYPES_H
# define TYPES_H #define TYPES_H
# include "cub3d.h" #include "cub3d.h"
typedef enum TILE typedef enum TILE
{ {
@ -22,63 +22,63 @@ typedef enum TILE
TILE_EMPTY = 0, TILE_EMPTY = 0,
TILE_WALL = 1, TILE_WALL = 1,
TILE_PLAYER = 2, TILE_PLAYER = 2,
} t_tile; } t_tile;
typedef struct s_vec2 typedef struct s_vec2
{ {
float x; double x;
float y; double y;
} t_vec2; } t_vec2;
typedef struct s_vec2_int typedef struct s_vec2_int
{ {
int x; int x;
int y; int y;
} t_vec2_int; } t_vec2_int;
typedef struct s_vec2_line typedef struct s_vec2_line
{ {
t_vec2 support; t_vec2 support;
t_vec2 dir; t_vec2 dir;
} t_vec2_line; } t_vec2_line;
typedef struct s_player typedef struct s_player
{ {
t_vec2 pos; t_vec2 pos;
t_vec2 dir; t_vec2 dir;
t_vec2 camera; t_vec2 camera;
float speed; double speed;
float fov; double fov;
} t_player; } t_player;
typedef struct s_map typedef struct s_map
{ {
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
t_tile **grid; t_tile **grid;
char *NO_texture; char *NO_texture;
char *SO_texture; char *SO_texture;
char *WE_texture; char *WE_texture;
char *EA_texture; char *EA_texture;
unsigned int floor_color; unsigned int floor_color;
unsigned int ceiling_color; unsigned int ceiling_color;
} t_map; } t_map;
typedef struct s_keyboard typedef struct s_keyboard
{ {
int keys[NUM_KEYS]; int keys[NUM_KEYS];
int last_keys[NUM_KEYS]; int last_keys[NUM_KEYS];
} t_keyboard; } t_keyboard;
typedef struct s_screen typedef struct s_screen
{ {
mlx_t *mlx; mlx_t *mlx;
mlx_image_t *img; mlx_image_t *img;
mlx_image_t *minimap; mlx_image_t *minimap;
mlx_image_t *background; mlx_image_t *background;
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
} t_screen; } t_screen;
typedef enum e_side typedef enum e_side
{ {
@ -86,21 +86,21 @@ typedef enum e_side
SIDE_SOUTH, SIDE_SOUTH,
SIDE_WEST, SIDE_WEST,
SIDE_EAST, SIDE_EAST,
} t_side; } t_side;
typedef struct s_render typedef struct s_render
{ {
float perp_dist; double perp_dist;
t_side side; t_side side;
float wall_x; double wall_x;
} t_render; } t_render;
typedef struct s_game typedef struct s_game
{ {
t_map *map; t_map *map;
t_player *player; t_player *player;
t_screen *screen; t_screen *screen;
t_keyboard *keyboard; t_keyboard *keyboard;
} t_game; } t_game;
#endif #endif

View File

@ -1,54 +1,53 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* vec_math.h :+: :+: */ /* vec_math.h :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/25 10:11:44 by whaffman #+# #+# */ /* Created: 2025/04/25 10:11:44 by whaffman #+# #+# */
/* Updated: 2025/04/25 10:44:33 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:37 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef VEC_MATH_H #ifndef VEC_MATH_H
# define VEC_MATH_H #define VEC_MATH_H
# include "types.h" #include "types.h"
/** /**
* @brief Calculates the distance between two 2D vectors. * @brief Calculates the distance between two 2D vectors.
* *
* @param a The first 2D vector. * @param a The first 2D vector.
* @param b The second 2D vector. * @param b The second 2D vector.
* @return The distance between vector a and vector b as a float. * @return The distance between vector a and vector b as a double.
*/ */
float dist(t_vec2 a, t_vec2 b); double dist(t_vec2 a, t_vec2 b);
/** /**
* @brief Calculates the norm (length) of a 2D vector. * @brief Calculates the norm (length) of a 2D vector.
* *
* @param a The 2D vector. * @param a The 2D vector.
* @return The norm of vector a as a float. * @return The norm of vector a as a double.
*/ */
float norm(t_vec2 a); double norm(t_vec2 a);
/** /**
* @brief Calculates the dot product of two 2D vectors. * @brief Calculates the dot product of two 2D vectors.
* *
* @param a The first 2D vector. * @param a The first 2D vector.
* @param b The second 2D vector. * @param b The second 2D vector.
* @return The dot product of vector a and vector b as a float. * @return The dot product of vector a and vector b as a double.
*/ */
float dot(t_vec2 a, t_vec2 b); double dot(t_vec2 a, t_vec2 b);
/** /**
* @brief Calculates the distance from a point to a line. * @brief Calculates the distance from a point to a line.
* *
* @param point The point to calculate the distance from. * @param point The point to calculate the distance from.
* @param line The line defined by a (support)point and a direction. * @param line The line defined by a (support)point and a direction.
* @return The distance from the point to the line as a float. * @return The distance from the point to the line as a double.
*/ */
float dist_point_line(t_vec2 point, t_vec2_line line); double dist_point_line(t_vec2 point, t_vec2_line line);
/** /**
* @brief Adds two 2D vectors. * @brief Adds two 2D vectors.
@ -57,7 +56,7 @@ float dist_point_line(t_vec2 point, t_vec2_line line);
* @param b The second 2D vector. * @param b The second 2D vector.
* @return The sum of vector a and vector b as a new 2D vector. * @return The sum of vector a and vector b as a new 2D vector.
*/ */
t_vec2 add(t_vec2 a, t_vec2 b); t_vec2 add(t_vec2 a, t_vec2 b);
/** /**
* @brief Subtracts one 2D vector from another. * @brief Subtracts one 2D vector from another.
@ -66,7 +65,7 @@ t_vec2 add(t_vec2 a, t_vec2 b);
* @param b The second 2D vector to subtract from the first. * @param b The second 2D vector to subtract from the first.
* @return The result of subtracting vector b from vector a as a new 2D vector. * @return The result of subtracting vector b from vector a as a new 2D vector.
*/ */
t_vec2 sub(t_vec2 a, t_vec2 b); t_vec2 sub(t_vec2 a, t_vec2 b);
/** /**
* @brief Multiplies a 2D vector by a scalar. * @brief Multiplies a 2D vector by a scalar.
@ -75,7 +74,7 @@ t_vec2 sub(t_vec2 a, t_vec2 b);
* @param b The scalar to multiply the vector by. * @param b The scalar to multiply the vector by.
* @return The result of multiplying vector a by scalar b as a new 2D vector. * @return The result of multiplying vector a by scalar b as a new 2D vector.
*/ */
t_vec2 mul(t_vec2 a, float b); t_vec2 mul(t_vec2 a, double b);
/** /**
* @brief Rotates a 2D vector by a given angle. * @brief Rotates a 2D vector by a given angle.
@ -84,7 +83,7 @@ t_vec2 mul(t_vec2 a, float b);
* @param angle The angle in radians to rotate the vector by. * @param angle The angle in radians to rotate the vector by.
* @return The rotated vector as a new 2D vector. * @return The rotated vector as a new 2D vector.
*/ */
t_vec2 rot(t_vec2 a, float angle); t_vec2 rot(t_vec2 a, double angle);
/** /**
* @brief Calculates the perpendicular vector of a 2D vector. * @brief Calculates the perpendicular vector of a 2D vector.
@ -92,6 +91,6 @@ t_vec2 rot(t_vec2 a, float angle);
* @param a The 2D vector to calculate the perpendicular of. * @param a The 2D vector to calculate the perpendicular of.
* @return The perpendicular vector as a new 2D vector. * @return The perpendicular vector as a new 2D vector.
*/ */
t_vec2 perp(t_vec2 a); t_vec2 perp(t_vec2 a);
#endif // VEC_MATH_H #endif // VEC_MATH_H

View File

@ -1,28 +1,28 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* collision.c :+: :+: */ /* collision.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */ /* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
/* Updated: 2025/05/06 14:18:07 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "collision.h" #include "collision.h"
int collision_horizontal(t_map *map, t_player *player, float xa) int collision_horizontal(t_map *map, t_player *player, double xa)
{ {
t_tile tile; t_tile tile;
tile = get_tile(map, (int)((player->pos.x + xa)), (int)((player->pos.y))); tile = get_tile(map, (int)((player->pos.x + xa)), (int)((player->pos.y)));
return (tile != TILE_WALL && tile != TILE_VOID); return (tile != TILE_WALL && tile != TILE_VOID);
} }
int collision_vertical(t_map *map, t_player *player, float ya) int collision_vertical(t_map *map, t_player *player, double ya)
{ {
t_tile tile; t_tile tile;
tile = get_tile(map, (int)((player->pos.x)), (int)((player->pos.y + ya))); tile = get_tile(map, (int)((player->pos.x)), (int)((player->pos.y + ya)));
return (tile != TILE_WALL && tile != TILE_VOID); return (tile != TILE_WALL && tile != TILE_VOID);

View File

@ -1,18 +1,18 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* game.c :+: :+: */ /* game.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ /* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/05/06 14:18:50 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d.h" #include "cub3d.h"
int game_create(t_game **game) int game_create(t_game **game)
{ {
*game = malloc(sizeof(t_game)); *game = malloc(sizeof(t_game));
if (!game) if (!game)
@ -24,7 +24,7 @@ int game_create(t_game **game)
return (SUCCESS); return (SUCCESS);
} }
void free_game(t_game **game) void free_game(t_game **game)
{ {
if (game && *game) if (game && *game)
{ {
@ -38,12 +38,12 @@ void free_game(t_game **game)
} }
} }
void game_loop(void *param) void game_loop(void *param)
{ {
t_game *game; t_game *game;
float delta_time; double delta_time;
static int framecount = 0; static int framecount = 0;
static int fps = 0; static int fps = 0;
framecount++; framecount++;
game = (t_game *)param; game = (t_game *)param;
@ -62,7 +62,7 @@ void game_loop(void *param)
keyboard_update(game); keyboard_update(game);
} }
void game_free(t_game *game) void game_free(t_game *game)
{ {
if (game->screen) if (game->screen)
{ {
@ -80,7 +80,7 @@ void game_free(t_game *game)
free(game); free(game);
} }
void game_terminate(t_game *game) void game_terminate(t_game *game)
{ {
game_free(game); game_free(game);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* dist.c :+: :+: */ /* dist.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/25 10:07:59 by whaffman #+# #+# */ /* Created: 2025/04/25 10:07:59 by whaffman #+# #+# */
/* Updated: 2025/04/25 10:39:59 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
#include "math.h" #include "math.h"
#include "vec_math.h" #include "vec_math.h"
float dist(t_vec2 a, t_vec2 b) double dist(t_vec2 a, t_vec2 b)
{ {
return (sqrtf((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))); return (sqrtf((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
} }

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* dist_point_line.c :+: :+: */ /* dist_point_line.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/25 10:09:45 by whaffman #+# #+# */ /* Created: 2025/04/25 10:09:45 by whaffman #+# #+# */
/* Updated: 2025/04/25 10:46:06 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,10 +14,10 @@
#include "math.h" #include "math.h"
#include "vec_math.h" #include "vec_math.h"
float dist_point_line(t_vec2 point, t_vec2_line line) double dist_point_line(t_vec2 point, t_vec2_line line)
{ {
float d; double d;
t_vec2 ps; t_vec2 ps;
ps = sub(line.support, point); ps = sub(line.support, point);
d = norm(sub(ps, mul(line.dir, dot(ps, line.dir)))); d = norm(sub(ps, mul(line.dir, dot(ps, line.dir))));

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* dot.c :+: :+: */ /* dot.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/25 09:58:29 by whaffman #+# #+# */ /* Created: 2025/04/25 09:58:29 by whaffman #+# #+# */
/* Updated: 2025/04/25 10:40:02 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
#include "math.h" #include "math.h"
#include "vec_math.h" #include "vec_math.h"
float dot(t_vec2 a, t_vec2 b) double dot(t_vec2 a, t_vec2 b)
{ {
return (a.x * b.x + a.y * b.y); return (a.x * b.x + a.y * b.y);
} }

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* mul.c :+: :+: */ /* mul.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/25 09:56:39 by whaffman #+# #+# */ /* Created: 2025/04/25 09:56:39 by whaffman #+# #+# */
/* Updated: 2025/04/25 10:40:05 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,9 +14,9 @@
#include "math.h" #include "math.h"
#include "vec_math.h" #include "vec_math.h"
t_vec2 mul(t_vec2 a, float b) t_vec2 mul(t_vec2 a, double b)
{ {
t_vec2 result; t_vec2 result;
result.x = a.x * b; result.x = a.x * b;
result.y = a.y * b; result.y = a.y * b;

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* norm.c :+: :+: */ /* norm.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/25 09:59:13 by whaffman #+# #+# */ /* Created: 2025/04/25 09:59:13 by whaffman #+# #+# */
/* Updated: 2025/04/25 10:40:13 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
#include "math.h" #include "math.h"
#include "vec_math.h" #include "vec_math.h"
float norm(t_vec2 a) double norm(t_vec2 a)
{ {
return (sqrtf(a.x * a.x + a.y * a.y)); return (sqrtf(a.x * a.x + a.y * a.y));
} }

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* rot.c :+: :+: */ /* rot.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/25 10:35:58 by whaffman #+# #+# */ /* Created: 2025/04/25 10:35:58 by whaffman #+# #+# */
/* Updated: 2025/04/25 10:40:23 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,11 +14,11 @@
#include "math.h" #include "math.h"
#include "vec_math.h" #include "vec_math.h"
t_vec2 rot(t_vec2 a, float angle) t_vec2 rot(t_vec2 a, double angle)
{ {
t_vec2 result; t_vec2 result;
float cos_angle; double cos_angle;
float sin_angle; double sin_angle;
cos_angle = cosf(angle); cos_angle = cosf(angle);
sin_angle = sinf(angle); sin_angle = sinf(angle);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* parse_map.c :+: :+: */ /* parse_map.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 13:12:04 by whaffman #+# #+# */ /* Created: 2025/04/22 13:12:04 by whaffman #+# #+# */
/* Updated: 2025/05/04 15:07:44 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,20 +15,20 @@
t_vec2 parse_dir(int x, int y) t_vec2 parse_dir(int x, int y)
{ {
t_vec2 dir; t_vec2 dir;
dir.x = (float)x; dir.x = (double)x;
dir.y = (float)y; dir.y = (double)y;
return (dir); return (dir);
} }
int parse_player(t_game *game, int y, int x, char c) int parse_player(t_game *game, int y, int x, char c)
{ {
t_player *player; t_player *player;
player = game->player; player = game->player;
player->pos.x = ((float)x + 0.5f); player->pos.x = ((double)x + 0.5f);
player->pos.y = ((float)y + 0.5f); player->pos.y = ((double)y + 0.5f);
if (c == 'N') if (c == 'N')
player->dir = parse_dir(0, -1); player->dir = parse_dir(0, -1);
@ -43,10 +43,10 @@ int parse_player(t_game *game, int y, int x, char c)
return (SUCCESS); return (SUCCESS);
} }
int parse_map_line(char **lines, t_game *game, int y) int parse_map_line(char **lines, t_game *game, int y)
{ {
int x; int x;
t_map *map; t_map *map;
map = game->map; map = game->map;
map->grid[y] = malloc(sizeof(t_tile) * (map->width + 1)); map->grid[y] = malloc(sizeof(t_tile) * (map->width + 1));
@ -70,10 +70,10 @@ int parse_map_line(char **lines, t_game *game, int y)
return (SUCCESS); return (SUCCESS);
} }
int parse_map(char **lines, t_game *game) int parse_map(char **lines, t_game *game)
{ {
int y; int y;
t_map *map; t_map *map;
y = 0; y = 0;
while (lines[y]) while (lines[y])

View File

@ -1,20 +1,20 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* player.c :+: :+: */ /* player.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */ /* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
/* Updated: 2025/05/06 14:20:37 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d.h" #include "cub3d.h"
int player_create(t_game **game) int player_create(t_game **game)
{ {
t_player *player; t_player *player;
player = malloc(sizeof(t_player)); player = malloc(sizeof(t_player));
if (!player) if (!player)
@ -31,10 +31,10 @@ int player_create(t_game **game)
return (SUCCESS); return (SUCCESS);
} }
static void move(t_map *map, t_player *player, int dir, float delta) static void move(t_map *map, t_player *player, int dir, double delta)
{ {
float xa; double xa;
float ya; double ya;
xa = dir * player->dir.x * player->speed * delta; xa = dir * player->dir.x * player->speed * delta;
ya = dir * player->dir.y * player->speed * delta; ya = dir * player->dir.y * player->speed * delta;
@ -44,10 +44,10 @@ static void move(t_map *map, t_player *player, int dir, float delta)
player->pos.y += ya; player->pos.y += ya;
} }
static void strave(t_map *map, t_player *player, int dir, float delta) static void strave(t_map *map, t_player *player, int dir, double delta)
{ {
float xa; double xa;
float ya; double ya;
xa = dir * perp(player->dir).x * player->speed * delta; xa = dir * perp(player->dir).x * player->speed * delta;
ya = dir * perp(player->dir).y * player->speed * delta; ya = dir * perp(player->dir).y * player->speed * delta;
@ -57,13 +57,13 @@ static void strave(t_map *map, t_player *player, int dir, float delta)
player->pos.y += ya; player->pos.y += ya;
} }
static void rotate(t_player *player, float rot_speed) static void rotate(t_player *player, double rot_speed)
{ {
player->dir = rot(player->dir, rot_speed); player->dir = rot(player->dir, rot_speed);
player->camera = rot(player->camera, rot_speed); player->camera = rot(player->camera, rot_speed);
} }
void player_update(t_game *game, float delta_time) void player_update(t_game *game, double delta_time)
{ {
if (get_key(game, MLX_KEY_W)) if (get_key(game, MLX_KEY_W))
move(game->map, game->player, 1, delta_time); move(game->map, game->player, 1, delta_time);
@ -79,15 +79,12 @@ void player_update(t_game *game, float delta_time)
rotate(game->player, .05f); rotate(game->player, .05f);
} }
void player_render(t_screen *screen, t_player *player) void player_render(t_screen *screen, t_player *player)
{ {
t_vec2 direction; t_vec2 direction;
if (player->pos.x < 0 if (player->pos.x < 0 || player->pos.x >= screen->width || player->pos.y < 0 || player->pos.y >= screen->height)
|| player->pos.x >= screen->width return;
|| player->pos.y < 0
|| player->pos.y >= screen->height)
return ;
render_circle(screen, mul(player->pos, TILE_SIZE), 4, 0x111111ff); render_circle(screen, mul(player->pos, TILE_SIZE), 4, 0x111111ff);
direction = add(mul(player->pos, TILE_SIZE), mul(player->dir, TILE_SIZE)); direction = add(mul(player->pos, TILE_SIZE), mul(player->dir, TILE_SIZE));
render_line(screen, mul(player->pos, TILE_SIZE), direction, 0xa83232ff); render_line(screen, mul(player->pos, TILE_SIZE), direction, 0xa83232ff);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* DDAscratch.c :+: :+: */ /* DDAscratch.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */ /* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
/* Updated: 2025/05/06 14:25:00 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:21:22 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,46 +14,49 @@
#include "vec_math.h" #include "vec_math.h"
#include <math.h> #include <math.h>
t_vec2 get_delta_dist(t_vec2 ray_dir) t_vec2 get_delta_dist(t_vec2 ray_dir)
{ {
t_vec2 delta_dist; t_vec2 delta_dist;
delta_dist.x = (ray_dir.x == 0) * 1e30 if (ray_dir.x == 0)
+ (ray_dir.x != 0) * fabs(1 / ray_dir.x); delta_dist.x = 1e30;
delta_dist.y = (ray_dir.y == 0) * 1e30 else
+ (ray_dir.y != 0) * fabs(1 / ray_dir.y); delta_dist.x = fabs(1 / ray_dir.x);
if (ray_dir.y == 0)
delta_dist.y = 1e30;
else
delta_dist.y = fabs(1 / ray_dir.y);
return (delta_dist); return (delta_dist);
} }
t_vec2 get_step(t_vec2 ray_dir) t_vec2 get_step(t_vec2 ray_dir)
{ {
t_vec2 step; t_vec2 step;
step.x = 2 * (ray_dir.x >= 0) - 1; step.x = 2 * (ray_dir.x >= 0) - 1;
step.y = 2 * (ray_dir.y >= 0) - 1; step.y = 2 * (ray_dir.y >= 0) - 1;
return (step); return (step);
} }
t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_dist) t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_dist)
{ {
const t_vec2 frac_pos = (t_vec2){pos.x - (int)pos.x, pos.y - (int)pos.y}; const t_vec2 frac_pos = (t_vec2){pos.x - (int)pos.x, pos.y - (int)pos.y};
const int raydir_x_pos = (ray_dir.x >= 0); const int raydir_x_pos = (ray_dir.x >= 0);
const int raydir_y_pos = (ray_dir.y >= 0); const int raydir_y_pos = (ray_dir.y >= 0);
t_vec2 side_dist; t_vec2 side_dist;
side_dist.x = ((1 - raydir_x_pos) * frac_pos.x side_dist.x = ((1 - raydir_x_pos) * frac_pos.x + raydir_x_pos * (1 - frac_pos.x)) * delta_dist.x;
+ raydir_x_pos * (1 - frac_pos.x)) * delta_dist.x; side_dist.y = ((1 - raydir_y_pos) * frac_pos.y + raydir_y_pos * (1 - frac_pos.y)) * delta_dist.y;
side_dist.y = ((1 - raydir_y_pos) * frac_pos.y
+ raydir_y_pos * (1 - frac_pos.y)) * delta_dist.y;
return (side_dist); return (side_dist);
} }
int dda_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map) int dda_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
{ {
const t_vec2 delta_dist = get_delta_dist(ray_dir); const t_vec2 delta_dist = get_delta_dist(ray_dir);
const t_vec2 step = get_step(ray_dir); const t_vec2 step = get_step(ray_dir);
int side; int side;
int hit; int hit;
hit = 0; hit = 0;
while (hit == 0) while (hit == 0)
@ -63,8 +66,7 @@ int dda_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
side_dist->y += delta_dist.y * side; side_dist->y += delta_dist.y * side;
map_pos.x += step.x * (1 - side); map_pos.x += step.x * (1 - side);
map_pos.y += step.y * side; map_pos.y += step.y * side;
if (map_pos.x < 0 || map_pos.x >= map->width if (map_pos.x < 0 || map_pos.x >= map->width || map_pos.y < 0 || map_pos.y >= map->height)
|| map_pos.y < 0 || map_pos.y >= map->height)
printf("Out of bounds: %d %d\n", map_pos.x, map_pos.y); printf("Out of bounds: %d %d\n", map_pos.x, map_pos.y);
hit = (map->grid[map_pos.y][map_pos.x] == TILE_WALL); hit = (map->grid[map_pos.y][map_pos.x] == TILE_WALL);
} }
@ -74,22 +76,21 @@ int dda_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
// Returns the distance to the wall hit if the wall is hit in y // Returns the distance to the wall hit if the wall is hit in y
// direction the result is negative, if the wall is hit // direction the result is negative, if the wall is hit
// in x direction the result is positive // in x direction the result is positive
float dda(t_vec2 ray_dir, t_vec2 pos, t_map *map) double dda(t_vec2 ray_dir, t_vec2 pos, t_map *map)
{ {
const t_vec2 delta_dist = get_delta_dist(ray_dir); const t_vec2 delta_dist = get_delta_dist(ray_dir);
t_vec2 side_dist; t_vec2 side_dist;
int side; int side;
side_dist = get_side_dist(ray_dir, pos, delta_dist); side_dist = get_side_dist(ray_dir, pos, delta_dist);
side = dda_main(ray_dir, side = dda_main(ray_dir,
(t_vec2_int){(int)pos.x, (int)pos.y}, (t_vec2_int){(int)pos.x, (int)pos.y},
&side_dist, &side_dist,
map); map);
return ((1 - side) * (side_dist.x - delta_dist.x) return ((1 - side) * (side_dist.x - delta_dist.x) - side * (side_dist.y - delta_dist.y));
- side * (side_dist.y - delta_dist.y));
} }
t_side get_side(t_vec2 ray_dir, float perp_dist) t_side get_side(t_vec2 ray_dir, double perp_dist)
{ {
if (perp_dist > 0) if (perp_dist > 0)
{ {
@ -107,49 +108,45 @@ t_side get_side(t_vec2 ray_dir, float perp_dist)
} }
} }
t_render cast_ray(t_game *game, int x) t_render cast_ray(t_game *game, int x)
{ {
t_vec2 ray_dir; t_vec2 ray_dir;
t_vec2 pos; t_vec2 pos;
t_render render; t_render render;
float perp_dist; double perp_dist;
ray_dir = add(game->player->dir, ray_dir = add(game->player->dir,
mul(game->player->camera, mul(game->player->camera,
(2.0f * x / (float)game->screen->width - 1))); (2.0f * x / (double)game->screen->width - 1)));
pos = game->player->pos; pos = game->player->pos;
perp_dist = dda(ray_dir, pos, game->map); perp_dist = dda(ray_dir, pos, game->map);
render.perp_dist = fabs(perp_dist); render.perp_dist = fabs(perp_dist);
render.side = get_side(ray_dir, perp_dist); render.side = get_side(ray_dir, perp_dist);
render.wall_x = (perp_dist > 0) * (pos.x + ray_dir.x * perp_dist) render.wall_x = (perp_dist > 0) * (pos.x + ray_dir.x * perp_dist) + (perp_dist <= 0) * (pos.y + ray_dir.y * perp_dist);
+ (perp_dist <= 0) * (pos.y + ray_dir.y * perp_dist);
render.wall_x -= floor(render.wall_x); render.wall_x -= floor(render.wall_x);
return (render); return (render);
} }
unsigned int get_color(t_render render) unsigned int get_color(t_render render)
{ {
float dist; double dist;
const unsigned int color[4] = { const unsigned int color[4] = {
0x488B49, 0x488B49,
0x4AAD52, 0x4AAD52,
0x6EB257, 0x6EB257,
0xC5E063 0xC5E063};
};
dist = (render.perp_dist == 0) * 1e30 dist = (render.perp_dist == 0) * 1e30 + (render.perp_dist > 1) * render.perp_dist + (render.perp_dist <= 1) * 1;
+ (render.perp_dist > 1) * render.perp_dist return (color[render.side] << 8 | (int)(1.0 / dist * 255));
+ (render.perp_dist <= 1) * 1;
return (color[render.side] << 8 |(int)(1.0 / dist * 255));
} }
void draw_line(t_game *game, t_render render, int x) void draw_line(t_game *game, t_render render, int x)
{ {
int y; int y;
int color; int color;
int lineHeight; int lineHeight;
int drawStart; int drawStart;
int drawEnd; int drawEnd;
y = 0; y = 0;
lineHeight = (int)(game->screen->height / render.perp_dist); lineHeight = (int)(game->screen->height / render.perp_dist);
@ -162,11 +159,9 @@ void draw_line(t_game *game, t_render render, int x)
while (y < game->screen->height) while (y < game->screen->height)
{ {
if (y < drawStart) if (y < drawStart)
color = game->map->ceiling_color << 8 color = game->map->ceiling_color << 8 | (int)fabs(2 * y * 0xFF / (double)game->screen->height - 0xFF);
| (int)fabs(2 * y * 0xFF / (float)game->screen->height - 0xFF);
else if (y > drawEnd) else if (y > drawEnd)
color = game->map->floor_color << 8 color = game->map->floor_color << 8 | (int)fabs(2 * y * 0xFF / (double)game->screen->height - 0xFF);
| (int)fabs(2 * y * 0xFF / (float)game->screen->height - 0xFF);
else else
color = get_color(render); color = get_color(render);
mlx_put_pixel(game->screen->img, x, y, color); mlx_put_pixel(game->screen->img, x, y, color);
@ -174,10 +169,10 @@ void draw_line(t_game *game, t_render render, int x)
} }
} }
void cast_rays(t_game *game) void cast_rays(t_game *game)
{ {
int x; int x;
t_render render; t_render render;
x = 0; x = 0;
while (x < game->screen->width) while (x < game->screen->width)