Browse Source

fix: rot_vec reducing vector progressivly, add: some more rework

master
narnaud 3 years ago
parent
commit
edf44d1499
  1. 2
      Makefile
  2. 10
      README.md
  3. 6
      includes/cub3d.h
  4. 4
      includes/mlx_keycode.h
  5. 2
      libft/Makefile
  6. 12
      sources/hooks.c
  7. 155
      sources/render.c
  8. 10
      sources/utils.c

2
Makefile

@ -16,7 +16,7 @@ endif
ifeq ($(UNAME_S), Darwin) ifeq ($(UNAME_S), Darwin)
LFLAGS = -lmlx -framework OpenGL -framework AppKit -L. -lft LFLAGS = -lmlx -framework OpenGL -framework AppKit -L. -lft
endif endif
CFLAGS = -Werror -Wall -Wextra #-O3 -ffast-math -funsafe-math-optimizations CFLAGS = -Werror -Wall -Wextra -O3 -ffast-math -funsafe-math-optimizations
%.o:%.c %.o:%.c
${CC} ${CFLAGS} -c $< -o ${<:.c=.o} ${CC} ${CFLAGS} -c $< -o ${<:.c=.o}

10
README.md

@ -9,6 +9,9 @@ CUBE3D
- [Debug maps (Outdated?)](https://github.com/nsterk/cub3D/tree/main/maps) - [Debug maps (Outdated?)](https://github.com/nsterk/cub3D/tree/main/maps)
- [Tester & maps](https://github.com/MichelleJiam/cub3D_tester) - [Tester & maps](https://github.com/MichelleJiam/cub3D_tester)
## Issues:
- Distances increase while time pass.
## Program life cycle : ## Program life cycle :
### Parsing ### Parsing
- [x] read file, - [x] read file,
@ -29,11 +32,12 @@ CUBE3D
- [ ] display wall's texture - [ ] display wall's texture
### Control ### Control
- [x] enable commands - [x] enable commands
- [ ] wasd to move - [x] wasd to move
- [x] arrows to move
- [ ] react to commands : - [ ] react to commands :
- [x] calculate new pos, - [x] calculate new pos,
- [x] display new view, - [x] display new view,
- [-] avoid colisions - [x] avoid colisions
- [x] M to show minimap
- [x] SHIFT to run
### Quit ### Quit
- [ ] clean and exit - [ ] clean and exit

6
includes/cub3d.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */ /* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */
/* Updated: 2022/06/01 14:23:18 by narnaud ### ########.fr */ /* Updated: 2022/06/01 14:59:38 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -79,7 +79,7 @@ typedef struct s_env
int endian; int endian;
int *buffer; int *buffer;
int minimap; int minimap;
int controls[256]; int controls[KEYS_LIMIT];
char *wallTexture[4]; char *wallTexture[4];
int floorColor; int floorColor;
int ceilColor; int ceilColor;
@ -91,7 +91,7 @@ typedef struct s_env
t_vec_d camPlan; t_vec_d camPlan;
} t_env; } t_env;
void render_minimap(t_env *env); void render_minimap(t_env *env, t_vec size);
void render_view(t_env *env); void render_view(t_env *env);
void render(t_env *env); void render(t_env *env);

4
includes/mlx_keycode.h

@ -6,7 +6,7 @@
/* By: bledda <bledda@student.42nice.fr> +#+ +:+ +#+ */ /* By: bledda <bledda@student.42nice.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/10 20:43:21 by bledda #+# #+# */ /* Created: 2021/08/10 20:43:21 by bledda #+# #+# */
/* Updated: 2022/05/23 09:42:56 by narnaud ### ########.fr */ /* Updated: 2022/06/01 14:58:39 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,7 @@
# define MLX_KEYCODE_H # define MLX_KEYCODE_H
# ifdef __linux__ # ifdef __linux__
# define KEYS_LIMIT 66000
# define KEY_Q 113 # define KEY_Q 113
# define KEY_W 119 # define KEY_W 119
# define KEY_E 101 # define KEY_E 101
@ -63,6 +64,7 @@
# define KEY_CTRL 65507 # define KEY_CTRL 65507
# define KEY_SPACE 32 # define KEY_SPACE 32
# elif __APPLE__ # elif __APPLE__
# define KEYS_LIMIT 300
# define KEY_Q 12 # define KEY_Q 12
# define KEY_W 13 # define KEY_W 13
# define KEY_E 14 # define KEY_E 14

2
libft/Makefile

@ -41,6 +41,8 @@ CFLAGS = -Wall -Wextra -Werror
all : $(NAME) all : $(NAME)
$(VERBOSE).SILENT:
$(NAME): $(OBJS) $(NAME): $(OBJS)
echo Making libft... echo Making libft...
${AR} ${NAME} ${OBJS} ${AR} ${NAME} ${OBJS}

12
sources/hooks.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 13:47:30 by narnaud #+# #+# */ /* Created: 2022/06/01 13:47:30 by narnaud #+# #+# */
/* Updated: 2022/06/01 14:26:26 by narnaud ### ########.fr */ /* Updated: 2022/06/01 17:10:10 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,17 +32,19 @@ int update_hook(t_env *env)
double dX; double dX;
double dY; double dY;
dX = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x / 10; dX = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x
dY = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.y / 10; / (10 - env->controls[KEY_SHFT] * 5);
dY = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.y
/ (10 - env->controls[KEY_SHFT] * 5);
if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + dX)] == '0') if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + dX)] == '0')
set_vec(&env->playerPos, env->playerPos.x + dX, env->playerPos.y); set_vec(&env->playerPos, env->playerPos.x + dX, env->playerPos.y);
if (env->map[(int)(env->playerPos.y + dY)][(int)env->playerPos.x] == '0') if (env->map[(int)(env->playerPos.y + dY)][(int)env->playerPos.x] == '0')
set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y + dY); set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y + dY);
rot_vec(&env->playerDir, rot_vec(&env->playerDir,
(double)(env->controls[KEY_D] - env->controls[KEY_A]) / 36); (double)(env->controls[KEY_D] - env->controls[KEY_A]) * M_PI / 36);
rot_vec(&env->camPlan, rot_vec(&env->camPlan,
(double)(env->controls[KEY_D] - env->controls[KEY_A]) / 36); (double)(env->controls[KEY_D] - env->controls[KEY_A]) * M_PI / 36);
if (DEBUG) if (DEBUG)
{ {
printf("playerPos: %f, %f. playerDir: %f, %f\n", env->playerPos.x, env->playerPos.y, env->playerDir.x, env->playerDir.y); printf("playerPos: %f, %f. playerDir: %f, %f\n", env->playerPos.x, env->playerPos.y, env->playerDir.x, env->playerDir.y);

155
sources/render.c

@ -6,63 +6,51 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/31 09:33:02 by narnaud #+# #+# */ /* Created: 2022/05/31 09:33:02 by narnaud #+# #+# */
/* Updated: 2022/06/01 14:06:23 by narnaud ### ########.fr */ /* Updated: 2022/06/01 17:00:21 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../includes/cub3d.h" #include "../includes/cub3d.h"
void draw_vert(t_env *env, int x, int *startEnd, int color) void draw_rectangle(t_env *env, t_rectangle rect, int borders)
{
int y;
y= 0;
while (y < startEnd[0])
{
env->buffer[env->line_bytes * y + x] = env->ceilColor;
y++;
}
while (startEnd[0] < startEnd[1])
{
env->buffer[env->line_bytes * startEnd[0] + x] = color;
startEnd[0]++;
}
y = startEnd[0];
while (y < WIN_Y_SZ)
{
env->buffer[env->line_bytes * y + x] = env->floorColor;
y++;
}
}
void draw_rectangle(t_env *env, t_rectangle rect)
{ {
int step_x; int stepX;
int step_y; int stepY;
step_y = 0; stepY = 0;
while (step_y < rect.sizeY) while (stepY < rect.sizeY)
{ {
step_x = 0; stepX = 0;
while (step_x < rect.sizeX) while (stepX < rect.sizeX)
{ {
if (step_x == rect.sizeX - 1 || step_y == rect.sizeY - 1) if (borders && (stepX == rect.sizeX - 1 || stepY == rect.sizeY - 1))
env->buffer[(env->line_bytes * (int)(rect.startY + step_y)) env->buffer[(env->line_bytes * (int)(rect.startY + stepY))
+ (int)(rect.startX + step_x)] = 0; + (int)(rect.startX + stepX)] = 0;
else else
env->buffer[(env->line_bytes * (int)(rect.startY + step_y)) env->buffer[(env->line_bytes * (int)(rect.startY + stepY))
+ (int)(rect.startX+ step_x)] = rect.color; + (int)(rect.startX + stepX)] = rect.color;
step_x++; stepX++;
} }
step_y++; stepY++;
} }
} }
void render_minimap(t_env *env) void draw_vert(t_env *env, int x, int *startEnd, int color)
{
draw_rectangle(env, (t_rectangle){x, 0, 1, startEnd[0] - 1, env->ceilColor}, 0);
draw_rectangle(env, (t_rectangle){x, startEnd[0], 1, startEnd[1] - startEnd[0] - 1, color}, 0);
draw_rectangle(env, (t_rectangle){x, startEnd[1], 1, WIN_Y_SZ - startEnd[1], env->floorColor}, 0);
}
void render_minimap(t_env *env, t_vec size)
{ {
char **map; char **map;
t_vec vec; t_vec vec;
int dX;
int dY;
dX = size.x / env->wide;
dY = size.y / env->deep;
vec.y = 0; vec.y = 0;
map = env->map; map = env->map;
while (map[vec.y]) while (map[vec.y])
@ -71,87 +59,62 @@ void render_minimap(t_env *env)
while (map[vec.y][vec.x]) while (map[vec.y][vec.x])
{ {
if (map[vec.y][vec.x] == '0') if (map[vec.y][vec.x] == '0')
draw_rectangle(env, (t_rectangle){vec.x * 6, vec.y * 6, 6, 6, 39424}); draw_rectangle(env, (t_rectangle){vec.x * dX, vec.y * dY, dX, dY, 39424}, 1);
else if (map[vec.y][vec.x] == '1') else if (map[vec.y][vec.x] == '1')
draw_rectangle(env, (t_rectangle){vec.x * 6, vec.y * 6, 6, 6, 11885067}); draw_rectangle(env, (t_rectangle){vec.x * dX, vec.y * dY, dX, dY, 11885067}, 1);
vec.x++; vec.x++;
} }
vec.y++; vec.y++;
} }
draw_rectangle(env, (t_rectangle){env->playerPos.x * 6 - 2,env->playerPos.y * 6 - 2, 4, 4, 255}); draw_rectangle(env, (t_rectangle){
env->playerPos.x * dX - dX / 4, env->playerPos.y * dY - dY / 4,
dX / 2, dY / 2, 255}, 0);
} }
double get_wall_dist(t_env *env, int x) double get_wall_dist(t_env *env, int x)
{ {
double camX; double camX;
t_vec_d ray; t_vec_d ray;
t_vec_d dDist; double dDist[2];
t_vec_d sDist; double sDist[2];
t_vec cell; int cell[2];
t_vec step; int *step;
int hit; int hit;
int side; int side;
if (env->map[(int)env->playerPos.y][(int)env->playerPos.x] > '0')
return (1e-30);
camX = 2 * x / (double)WIN_X_SZ - 1; camX = 2 * x / (double)WIN_X_SZ - 1;
set_vec(&ray, env->playerDir.x + env->camPlan.x * camX, env->playerDir.y + env->camPlan.y * camX); set_vec(&ray, env->playerDir.x + env->camPlan.x * camX, env->playerDir.y + env->camPlan.y * camX);
cell.x = (int)(env->playerPos.x); cell[0] = (int)(env->playerPos.x);
cell.y = (int)(env->playerPos.y); cell[1] = (int)(env->playerPos.y);
if (ray.x == 0) if (ray.x == 0)
dDist.x = 1e30; dDist[0] = 1e30;
else else
dDist.x = fabs(1 / ray.x); dDist[0] = fabs(1 / ray.x);
if (ray.y == 0) if (ray.y == 0)
dDist.y = 1e30; dDist[1] = 1e30;
else else
dDist.y = fabs(1 / ray.y); dDist[1] = fabs(1 / ray.y);
hit = 0; hit = 0;
if (ray.x < 0) step = (int[2]){0, 0};
{ if (ray.x < 0 && --step[0])
step.x = -1; sDist[0] = (env->playerPos.x - cell[0]) * dDist[0];
sDist.x = (env->playerPos.x - cell.x) * dDist.x; else if (++step[0])
} sDist[0] = (cell[0] + 1.0 - env->playerPos.x) * dDist[0];
else if (ray.y < 0 && --step[1])
{ sDist[1] = (env->playerPos.y - cell[1]) * dDist[1];
step.x = 1; else if (++step[1])
sDist.x = (cell.x + 1.0 - env->playerPos.x) * dDist.x; sDist[1] = (cell[1] + 1.0 - env->playerPos.y) * dDist[1];
}
if (ray.y < 0)
{
step.y = -1;
sDist.y = (env->playerPos.y - cell.y) * dDist.y;
}
else
{
step.y = 1;
sDist.y = (cell.y + 1.0 - env->playerPos.y) * dDist.y;
}
while (hit == 0) while (hit == 0)
{ {
if (sDist.x < sDist.y) side = 0;
{ if (sDist[0] > sDist[1])
sDist.x += dDist.x; side++;
cell.x += step.x; sDist[side] += dDist[side];
side = 0; cell[side] += step[side];
} if (env->map[cell[1]][cell[0]] > '0')
else
{
sDist.y += dDist.y;
cell.y += step.y;
side = 1;
}
if (env->map[cell.y][cell.x] > '0')
hit = 1; hit = 1;
} }
if (DEBUG) return (sDist[side] - dDist[side]);
{
//printf("X:%d - sideDist: %f, %f. deltaDist: %f, %f.\n", x, sDist.x, sDist.y, dDist.x, dDist.y);
}
if (side == 0)
return (sDist.x - dDist.x);
else
return (sDist.y - dDist.y);
} }
void render_view(t_env *env) void render_view(t_env *env)
@ -187,7 +150,7 @@ void render(t_env *env)
env->line_bytes /= 4; env->line_bytes /= 4;
render_view(env); render_view(env);
if (env->minimap) if (env->minimap)
render_minimap(env); render_minimap(env, (t_vec){env->wide * 8, env->deep * 8});
mlx_put_image_to_window(env->mlx, env->win, env->img, 0, 0); mlx_put_image_to_window(env->mlx, env->win, env->img, 0, 0);
mlx_destroy_image(env->mlx, env->img); mlx_destroy_image(env->mlx, env->img);
} }

10
sources/utils.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/31 08:59:01 by narnaud #+# #+# */ /* Created: 2022/05/31 08:59:01 by narnaud #+# #+# */
/* Updated: 2022/06/01 14:21:41 by narnaud ### ########.fr */ /* Updated: 2022/06/01 17:16:10 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,8 +25,12 @@ void set_vec(t_vec_d *vec, double x, double y)
void rot_vec(t_vec_d *vec, double rad) void rot_vec(t_vec_d *vec, double rad)
{ {
vec->x = cos(M_PI * rad) * vec->x - sin(M_PI * rad) * vec->y; double len;
vec->y = sin(M_PI * rad) * vec->x + cos(M_PI * rad) * vec->y;
len = vec_len(*vec);
vec->x = (cos(rad) * vec->x - sin(rad) * vec->y) * 0.66 / len;
vec->y = (sin(rad) * vec->x + cos(rad) * vec->y) * 0.66 / len;
} }
int rgb_to_int(char **rgb) int rgb_to_int(char **rgb)

Loading…
Cancel
Save