Skip to content

Commit

Permalink
Fixed texture moving bug when getting closer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joppe Boeve committed Jan 18, 2024
1 parent 6bb09aa commit 1d7509e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
4 changes: 2 additions & 2 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jboeve <[email protected]>+#+ */
/* +#+ */
/* Created: 2023/11/01 20:07:37 by jboeve#+##+# */
/* Updated: 2024/01/18 11:13:46 by jboeve ######## odam.nl */
/* Updated: 2024/01/18 11:56:06 by jboeve ######## odam.nl */
/**/
/* ************************************************************************** */

Expand Down Expand Up @@ -135,7 +135,7 @@ typedef enum e_side {
typedef struct s_ray {
t_vec2d direction;
t_vec2d end;
t_vec2d map_pos;
t_vec2i map_pos;
t_vec2i texture_point;
t_vec2i line_point;
t_side hit_side;
Expand Down
18 changes: 7 additions & 11 deletions src/game/raycaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:27:33 by yzaim #+# #+# */
/* Updated: 2024/01/18 11:13:21 by jboeve ######## odam.nl */
/* Updated: 2024/01/18 12:05:53 by jboeve ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,7 +34,7 @@ inline static t_vec2d calculate_delta_dist(t_vec2d ray_direction)
}

inline static t_vec2d calculate_side_dist(t_vec2d ray_direction, \
t_vec2d player_pos, t_vec2d map_pos, t_vec2d delta_dist)
t_vec2d player_pos, t_vec2i map_pos, t_vec2d delta_dist)
{
t_vec2d side_dist;

Expand All @@ -49,14 +49,14 @@ inline static t_vec2d calculate_side_dist(t_vec2d ray_direction, \
return (side_dist);
}

inline static t_vec2d calculate_step_size(t_vec2d ray_direction)
inline static t_vec2i calculate_step_size(t_vec2d ray_direction)
{
const bool comp_x = (ray_direction.x < 0);
const bool comp_y = (ray_direction.y < 0);
const int8_t dir_x = (comp_x * -1) + (!comp_x * 1);
const int8_t dir_y = (comp_y * -1) + (!comp_y * 1);

return ((t_vec2d){dir_x, dir_y});
return ((t_vec2i){dir_x, dir_y});
}

inline static double calculate_ray_length(t_side hit_side, \
Expand All @@ -69,7 +69,7 @@ inline static double calculate_ray_length(t_side hit_side, \
}

inline static t_side ray_move(t_vec2d *side_dist, t_vec2d *delta_dist, \
t_vec2d step_size, t_vec2d *map_pos)
t_vec2i step_size, t_vec2i *map_pos)
{
if (side_dist->x < side_dist->y)
{
Expand All @@ -95,7 +95,7 @@ t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *par
{
t_ray r;
t_vec2d side_dist;
t_vec2d step_size;
t_vec2i step_size;
t_vec2d delta_dist;

r.map_pos.x = (int)pp.x;
Expand All @@ -115,17 +115,13 @@ t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *par
WARNING("Raycaster limit reached!");
r.length = calculate_ray_length(r.hit_side, side_dist, delta_dist);
r.direction = dir;
r.end = r.map_pos;
// r.end = r.map_pos;

r.line_height = (int)(WINDOW_HEIGHT / r.length);
if (r.line_height > WINDOW_HEIGHT)
r.line_height = WINDOW_HEIGHT;

// draw start and draw end
r.line_point.x = -r.line_height / 2 + ((double)WINDOW_HEIGHT) / 2;
r.line_point.y = r.line_height / 2 + ((double)WINDOW_HEIGHT) / 2;
if (r.line_point.y >= WINDOW_HEIGHT)
r.line_point.y = WINDOW_HEIGHT - 1;

if (r.hit_side == SIDE_N || r.hit_side == SIDE_S)
r.wall_x = pp.y + r.length * r.direction.y;
Expand Down
20 changes: 13 additions & 7 deletions src/game/render_viewport.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */
/* Updated: 2024/01/18 11:14:19 by jboeve ######## odam.nl */
/* Updated: 2024/01/18 12:27:01 by jboeve ######## odam.nl */
/* */
/* ************************************************************************** */

#include "MLX42/MLX42.h"
#include "meta.h"
#include "vector.h"
#include <stdint.h>
#include <stdio.h>
#include <math.h>

mlx_texture_t *get_texture(t_side side, t_attr attributes);

static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, uint32_t h)
static void draw_column(t_meta *meta, t_ray *ray, uint32_t col, uint32_t h)
{
int32_t y;
int32_t color;
Expand All @@ -32,15 +33,20 @@ static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, ui
if ((ray->hit_side == SIDE_E || ray->hit_side == SIDE_W) && ray->direction.y < 0)
ray->texture_point.x = texture->width - ray->texture_point.x - 1;

ray->step = 1.0 * texture->height / ray->line_height;
ray->texture_position = (ray->line_point.x + (ray->line_height - h) / 2) * ray->step;

double offset = 0;
if (ray->line_height > h)
offset = (ray->line_height - h) / 2;

ray->step = texture->height / ray->line_height;
ray->texture_position = ((ray->line_point.x + offset) + (ray->line_height - h) / 2) * ray->step;

y = 0;
while (y < (int32_t) h)
{
if (y < line.x)
if (y < ray->line_point.x)
color = find_color(meta->attributes.ceiling_c);
else if (y > line.y)
else if (y >= ray->line_point.y)
color = find_color(meta->attributes.floor_c);
else
{
Expand All @@ -58,7 +64,7 @@ void render_viewport(mlx_image_t *image, t_player *p)
uint32_t col = 0;
while(col < image->width)
{
draw_column(p->meta, p->rays[col].line_point, &p->rays[col], col, image->height);
draw_column(p->meta, &p->rays[col], col, image->height);
col++;
}
}

0 comments on commit 1d7509e

Please sign in to comment.