-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ys-zm/raycaster
Raycaster
- Loading branch information
Showing
10 changed files
with
159 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: jboeve <[email protected]> +#+ */ | ||
/* +#+ */ | ||
/* Created: 2023/11/01 20:07:37 by jboeve #+# #+# */ | ||
/* Updated: 2023/11/13 21:19:38 by joppe ######## odam.nl */ | ||
/* Updated: 2023/11/18 20:45:26 by jboeve ######## odam.nl */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -45,10 +45,14 @@ | |
#define PI 3.1415926535 | ||
|
||
// Window settings | ||
#define WINDOW_WIDTH 1024 | ||
#define WINDOW_HEIGHT 720 | ||
#define WINDOW_WIDTH 1920 | ||
#define WINDOW_HEIGHT 1080 | ||
#define WINDOW_TITLE "Gammoe" | ||
|
||
#define PLAYER_VIEWPORT_X 720 | ||
#define PLAYER_VIEWPORT_Y 512 | ||
#define PLAYER_VIEWPORT_WALL_WIDTH 8 | ||
|
||
#define TICK_RATE (1.0 / 60.0) | ||
|
||
// TODO Move all this stuff to some kind of game.h | ||
|
@@ -57,7 +61,7 @@ | |
|
||
#define PLAYER_WIDTH 16 | ||
#define PLAYER_HEIGHT 16 | ||
#define PLAYER_RAY_COUNT 360 | ||
#define PLAYER_RAY_COUNT 90 | ||
#define PLAYER_WALK_SPEED 15 | ||
#define PLAYER_ROTATE_SPEED 5 | ||
#define PLAYER_RUN_MODIFIER 2.5 | ||
|
@@ -99,10 +103,15 @@ typedef struct s_meta t_meta; | |
|
||
// NOTE: Maybe switch to double instead of float? | ||
typedef struct s_player { | ||
uint32_t x; | ||
uint32_t y; | ||
uint32_t start_x; | ||
uint32_t start_y; | ||
t_meta *meta; | ||
// TODO Have a map_position which will be the position relative to the leftmost square. | ||
// Based on that position we can just `position / CELL_WIDTH` to find the cell position. | ||
t_vec2i map_cell; | ||
t_vec2f position; | ||
t_vec2f direction; | ||
t_vec2f beam; | ||
t_ray rays[PLAYER_RAY_COUNT]; | ||
float angle_rad; | ||
} t_player; | ||
|
||
typedef struct s_map { | ||
|
@@ -136,6 +145,40 @@ typedef struct s_meta { | |
int cub3d(int argc, char *argv[]); | ||
|
||
// game.c | ||
void game_init(t_meta *meta); | ||
void game_loop(void* param); | ||
|
||
// player.c | ||
void player_move(t_player *p, t_vec2f transform); | ||
void player_look(t_player *p, double angle); | ||
void player_raycast(t_player *p); | ||
|
||
// input.c | ||
void key_hook(mlx_key_data_t keydata, void* param); | ||
void cursor_hook(double xpos, double ypos, void* param); | ||
|
||
// render.c | ||
t_vec2i render_get_draw_offset(); | ||
void render_player_view(mlx_image_t *image, t_player *p); | ||
void render_player(mlx_image_t *image, t_player *p); | ||
void render_clear_bg(mlx_image_t *image); | ||
void render_map_grid(mlx_image_t *image, t_map *m); | ||
|
||
// map.c | ||
t_cell_type map_get_cell_type(t_map *m, t_vec2f pos); | ||
|
||
// draw.c | ||
void draw_rect(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color); | ||
void draw_line(mlx_image_t *image, t_vec2i start, t_vec2i end, t_rgba c); | ||
void draw_put_pixel(mlx_image_t* image, uint32_t x, uint32_t y, uint32_t color); | ||
|
||
// utils.c | ||
float deg_to_rad(float deg); | ||
|
||
// test_utils.c | ||
void print_vec2f(const char *s, t_vec2f vec); | ||
void print_vec2i(const char *s, t_vec2i vec); | ||
void print_cell(t_cell_type cell); | ||
void game_init(t_meta *meta); | ||
void game_loop(void* param); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,15 @@ | |
/* By: joppe <[email protected]> +#+ */ | ||
/* +#+ */ | ||
/* Created: 2023/11/11 03:51:14 by joppe #+# #+# */ | ||
/* Updated: 2023/11/11 03:58:12 by joppe ######## odam.nl */ | ||
/* Updated: 2023/11/18 13:21:55 by jboeve ######## odam.nl */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef VECTOR_H | ||
#define VECTOR_H | ||
|
||
#include <inttypes.h> | ||
#include <sys/cdefs.h> | ||
|
||
|
||
#define VEC_X 0 | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: joppe <[email protected]> +#+ */ | ||
/* +#+ */ | ||
/* Created: 2023/11/08 22:35:05 by joppe #+# #+# */ | ||
/* Updated: 2023/11/09 17:56:57 by yzaim ### ########.fr */ | ||
/* Updated: 2023/11/18 20:39:45 by jboeve ######## odam.nl */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -62,9 +62,6 @@ void game_init(t_meta *meta) | |
p->position[VEC_X] = (float) meta->map.width * CELL_WIDTH / 2; | ||
p->position[VEC_Y] = (float) meta->map.height * CELL_WIDTH/ 2; | ||
|
||
// p->position[VEC_X] = 0; | ||
// p->position[VEC_Y] = 0; | ||
|
||
|
||
player_look(p, deg_to_rad(180.0f)); | ||
} | ||
|
@@ -84,9 +81,9 @@ static void game_update(t_meta *meta, double time_delta) | |
player_move(p, -move * p->direction); | ||
|
||
if (mlx_is_key_down(meta->mlx, MLX_KEY_A)) | ||
player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle + (3 * PI / 2))) * move); | ||
player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle_rad + (3 * PI / 2))) * move); | ||
if (mlx_is_key_down(meta->mlx, MLX_KEY_D)) | ||
player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle + (PI / 2))) * move); | ||
player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle_rad + (PI / 2))) * move); | ||
|
||
if (mlx_is_key_down(meta->mlx, MLX_KEY_Q)) | ||
player_look(p, -rotate); | ||
|
@@ -118,5 +115,6 @@ void game_loop(void* param) | |
|
||
render_clear_bg(meta->image); | ||
render_map_grid(meta->image, &meta->map); | ||
render_player_view(meta->image, &meta->player); | ||
render_player(meta->image, &meta->player); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,13 @@ | |
/* By: jboeve <[email protected]> +#+ */ | ||
/* +#+ */ | ||
/* Created: 2023/11/07 15:28:54 by jboeve #+# #+# */ | ||
/* Updated: 2023/11/09 17:57:13 by yzaim ### ########.fr */ | ||
/* Updated: 2023/11/18 20:55:05 by jboeve ######## odam.nl */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "meta.h" | ||
#include "vector.h" | ||
#include <stdio.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: joppe <[email protected]> +#+ */ | ||
/* +#+ */ | ||
/* Created: 2023/11/10 02:25:34 by joppe #+# #+# */ | ||
/* Updated: 2023/11/13 21:31:05 by joppe ######## odam.nl */ | ||
/* Updated: 2023/11/18 20:45:45 by jboeve ######## odam.nl */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -30,11 +30,11 @@ void player_look(t_player *p, double angle) | |
{ | ||
const uint32_t len = 50; | ||
|
||
p->angle = fmod(p->angle + angle, 2 * PI); | ||
if (p->angle < 0) | ||
p->angle += 2 * PI; | ||
p->angle_rad = fmod(p->angle_rad + angle, 2 * PI); | ||
if (p->angle_rad < 0) | ||
p->angle_rad += 2 * PI; | ||
|
||
p->direction = vec2f_normalize(vec2f_rotate2d(p->angle)); | ||
p->direction = vec2f_normalize(vec2f_rotate2d(p->angle_rad)); | ||
p->beam = p->position + p->direction * (t_vec2f) {len, len}; | ||
|
||
player_raycast(p); | ||
|
@@ -72,8 +72,8 @@ void player_raycast(t_player *p) | |
i = 0; | ||
while (i < PLAYER_RAY_COUNT) | ||
{ | ||
dir = vec2f_normalize(vec2f_rotate2d(p->angle + deg_to_rad(i))); | ||
p->rays[i] = raycast(&p->meta->map, p->position, p->direction * dir, depth); | ||
dir = vec2f_normalize(vec2f_rotate2d(p->angle_rad + deg_to_rad(i) - (deg_to_rad(PLAYER_RAY_COUNT) / 2))); | ||
p->rays[i] = raycast(&p->meta->map, p->position, dir, depth); | ||
i++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "enemy.h" | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
|
||
typedef void enemy; | ||
|
||
enemy_t *enemy_init(const char *name); | ||
void enemy_die(void *ptr); | ||
|
||
|
||
//int main() | ||
//{ | ||
// enemy *e = enemy_init("sjon"); | ||
// enemy_die(e); | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* raycaster.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: yzaim <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/11/09 17:58:20 by yzaim #+# #+# */ | ||
/* Updated: 2023/11/09 18:05:01 by yzaim ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "meta.h" | ||
|
||
void raycaster(t_meta *meta) | ||
{ | ||
int x; | ||
|
||
x = -1; | ||
while (++x < MAP_WIDTH) | ||
{ | ||
//search the intersections with x-lines | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: joppe <[email protected]> +#+ */ | ||
/* +#+ */ | ||
/* Created: 2023/11/08 23:14:20 by joppe #+# #+# */ | ||
/* Updated: 2023/11/09 17:55:01 by yzaim ### ########.fr */ | ||
/* Updated: 2023/11/18 20:56:34 by jboeve ######## odam.nl */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -35,20 +35,36 @@ const t_rgba CELL_COLORS[] = { | |
t_vec2i render_get_draw_offset() | ||
{ | ||
return (DRAW_OFFSET); | ||
} | ||
} :::::::: */ | ||
4 | ||
/* render.c :+: :+: :+: */ | ||
5 | ||
/* +:+ */ | ||
6 | ||
/* By: joppe <[email protected]> +#+ */ | ||
7 | ||
/* +#+ */ | ||
8 | ||
/* Created: 2023/11/08 23:14:20 by joppe #+# #+# */ | ||
9 | ||
/* Updated: 2023/11/18 20:56:34 by jboeve ######## odam.nl */ | ||
10 | ||
/* */ | ||
11 | ||
/* ************************************************************************** */ | ||
|
||
|
||
void draw_cell(mlx_image_t *image, t_map *m, uint32_t cell_x, uint32_t cell_y) | ||
{ | ||
const size_t x_offset = (cell_x * CELL_WIDTH) + cell_x + DRAW_OFFSET[VEC_X]; | ||
const size_t y_offset = (cell_y * CELL_HEIGHT) + cell_y + DRAW_OFFSET[VEC_Y]; | ||
// const size_t x_offset = (cell_x * CELL_WIDTH) + cell_x + DRAW_OFFSET[VEC_X]; | ||
// const size_t y_offset = (cell_y * CELL_HEIGHT) + cell_y + DRAW_OFFSET[VEC_Y]; | ||
|
||
// const size_t x_offset = (cell_x * CELL_WIDTH) + DRAW_OFFSET[VEC_X]; | ||
// const size_t y_offset = (cell_y * CELL_HEIGHT) + DRAW_OFFSET[VEC_Y]; | ||
const size_t x_offset = (cell_x * CELL_WIDTH) + DRAW_OFFSET[VEC_X]; | ||
const size_t y_offset = (cell_y * CELL_HEIGHT) + DRAW_OFFSET[VEC_Y]; | ||
|
||
const t_cell_type cell = (m->level[(cell_y * m->width) + cell_x]); | ||
|
||
draw_square(image, x_offset, y_offset, CELL_WIDTH, CELL_HEIGHT, CELL_COLORS[cell].value); | ||
draw_rect(image, x_offset, y_offset, CELL_WIDTH, CELL_HEIGHT, CELL_COLORS[cell].value); | ||
} | ||
|
||
// The player is essentially just a single point/pixel, around which we draw a square with the "player point" in its center. | ||
|
@@ -59,20 +75,42 @@ void render_player(mlx_image_t *image, t_player *p) | |
draw_pos[VEC_Y] -= ((float) PLAYER_HEIGHT / 2); | ||
|
||
// Draw the player square. | ||
draw_square(image, draw_pos[VEC_X], draw_pos[VEC_Y], | ||
draw_rect(image, draw_pos[VEC_X], draw_pos[VEC_Y], | ||
PLAYER_WIDTH, PLAYER_HEIGHT, COLOR_PLAYER); | ||
|
||
size_t i = 0; | ||
while (i < PLAYER_RAY_COUNT) | ||
{ | ||
draw_line(image, vec2f_to_vec2i(p->rays[i].start) + DRAW_OFFSET, | ||
vec2f_to_vec2i(p->rays[i].end) + DRAW_OFFSET, | ||
(t_rgba) {0xFF1500FF}); | ||
i++; | ||
} | ||
|
||
// Draw the player look direction. | ||
draw_line(image, vec2f_to_vec2i(p->position) + DRAW_OFFSET, | ||
vec2f_to_vec2i(p->beam) + DRAW_OFFSET, | ||
(t_rgba) {0x00FF00FF}); | ||
} | ||
|
||
float ray_distance(t_ray r) | ||
{ | ||
t_vec2f diff = r.end - r.start; | ||
return sqrtf((r.end[VEC_X] - r.start[VEC_X]) * (r.end[VEC_X] - r.start[VEC_X]) + (r.end[VEC_Y] - r.start[VEC_Y]) * (r.end[VEC_Y] - r.start[VEC_Y])); | ||
} | ||
|
||
void render_player_view(mlx_image_t *image, t_player *p) | ||
{ | ||
size_t i = 0; | ||
t_ray tmp; | ||
int height = 1; | ||
const int x_offset = PLAYER_VIEWPORT_X + 1; | ||
|
||
while (i < PLAYER_RAY_COUNT) | ||
{ | ||
draw_line(image, vec2f_to_vec2i(p->rays[i].start) + DRAW_OFFSET, | ||
vec2f_to_vec2i(p->rays[i].end) + DRAW_OFFSET, | ||
(t_rgba) {0xFF1500FF}); | ||
tmp = p->rays[i]; | ||
height = 500 - ray_distance(tmp); | ||
draw_rect(image, x_offset + (PLAYER_VIEWPORT_WALL_WIDTH * i), PLAYER_VIEWPORT_Y, PLAYER_VIEWPORT_WALL_WIDTH, height, 0xfc7b25ff); | ||
i++; | ||
} | ||
} | ||
|