ray casting start working
This commit is contained in:
parent
be27eab42c
commit
4aa227ae33
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/02/13 16:30:59 by apommier #+# #+# */
|
/* Created: 2022/02/13 16:30:59 by apommier #+# #+# */
|
||||||
/* Updated: 2022/05/04 17:34:25 by apommier ### ########.fr */
|
/* Updated: 2022/05/04 18:46:58 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
@ -6,12 +6,23 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/05/04 18:08:14 by apommier #+# #+# */
|
/* Created: 2022/05/04 18:08:14 by apommier #+# #+# */
|
||||||
/* Updated: 2022/05/04 18:35:18 by apommier ### ########.fr */
|
/* Updated: 2022/05/04 20:55:52 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/Cub3D.h"
|
#include "../includes/Cub3D.h"
|
||||||
|
|
||||||
|
void print_ray2(t_data *img, float vx, float vy, float dist)
|
||||||
|
{
|
||||||
|
int i = -1;
|
||||||
|
while (++i < dist)
|
||||||
|
{
|
||||||
|
mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (vx) * i) , (img->player.y + (vy) * i) , 500);
|
||||||
|
mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (vx) * i) + 1, (img->player.y + (vy) * i) , 500);
|
||||||
|
mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (vx) * i) , (img->player.y + (vy) * i) + 1, 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void draw_ray(t_data *img)
|
void draw_ray(t_data *img)
|
||||||
{
|
{
|
||||||
float ray_angle = 0; //ray angle
|
float ray_angle = 0; //ray angle
|
||||||
@ -19,8 +30,11 @@ void draw_ray(t_data *img)
|
|||||||
float ray_x = 0; //where ray touch y
|
float ray_x = 0; //where ray touch y
|
||||||
float next_x = 0;
|
float next_x = 0;
|
||||||
float next_y = 0;
|
float next_y = 0;
|
||||||
float dist_v = 0;
|
float dist_v = 1000000000;
|
||||||
float dist_h = 0;
|
float dist_h = 1000000000;
|
||||||
|
float dist_f = 0;
|
||||||
|
float vx = 0;
|
||||||
|
float vy = 0;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
float aTan = 0;
|
float aTan = 0;
|
||||||
int nb_ray = -1;
|
int nb_ray = -1;
|
||||||
@ -29,22 +43,33 @@ void draw_ray(t_data *img)
|
|||||||
int mp = 0;
|
int mp = 0;
|
||||||
|
|
||||||
|
|
||||||
printf("\nENTER DRAW RAY\n");
|
//printf("\nENTER DRAW RAY\n");
|
||||||
|
int k = -1;
|
||||||
|
//while (++k < ft_strlen(img->map.simple_map))
|
||||||
|
// printf("%d--- %c\n", k, img->map.simple_map[k]);
|
||||||
|
//printf("map = -%s-\n", img->map.simple_map);
|
||||||
count = 0;
|
count = 0;
|
||||||
ray_angle = img->player.angle;
|
ray_angle = reset_angle(img->player.angle + 30);
|
||||||
while (++nb_ray < 1)
|
//ray_angle = reset_angle(img->player.angle);
|
||||||
|
while (++nb_ray < 60)
|
||||||
{
|
{
|
||||||
printf("player_angle= %f\n", img->player.angle);
|
//if (nb_ray)
|
||||||
//start vertical ray
|
// ray_angle -= 30;
|
||||||
|
count = 0;
|
||||||
|
dist_v = -1;
|
||||||
|
dist_h = -1;
|
||||||
|
//printf("------RAY N0 %d-------\n", nb_ray);
|
||||||
|
//printf("player_angle= %f ray_angle= %f\n", img->player.angle, ray_angle);
|
||||||
|
//----------start vertical ray----------
|
||||||
aTan = tan(deg_to_rad(ray_angle));
|
aTan = tan(deg_to_rad(ray_angle));
|
||||||
if (cos(deg_to_rad(ray_angle))> 0.001)//looking left
|
if (cos(deg_to_rad(ray_angle)) > 0.001)//looking left
|
||||||
{
|
{
|
||||||
ray_x = (((int)img->player.x>>6)<<6) + 64;
|
ray_x = (((int)img->player.x>>6)<<6) + 64;
|
||||||
ray_y = (img->player.x - ray_x) * aTan + img->player.y;
|
ray_y = (img->player.x - ray_x) * aTan + img->player.y;
|
||||||
next_x = 64;
|
next_x = 64;
|
||||||
next_y = -next_x * aTan;
|
next_y = -next_x * aTan;
|
||||||
}
|
}
|
||||||
else if (cos(deg_to_rad(ray_angle))<-0.001)//looking right
|
else if (cos(deg_to_rad(ray_angle)) < -0.001)//looking right
|
||||||
{
|
{
|
||||||
ray_x = (((int)img->player.x>>6)<<6) - 0.0001;
|
ray_x = (((int)img->player.x>>6)<<6) - 0.0001;
|
||||||
ray_y = (img->player.x - ray_x) * aTan + img->player.y;
|
ray_y = (img->player.x - ray_x) * aTan + img->player.y;
|
||||||
@ -57,9 +82,13 @@ void draw_ray(t_data *img)
|
|||||||
ray_y = img->player.y;
|
ray_y = img->player.y;
|
||||||
count = 8;
|
count = 8;
|
||||||
}
|
}
|
||||||
printf("ray_y= %f ray_x= %f\n", ray_y, ray_x);
|
//if (next_x > 0)
|
||||||
printf("next_y= %f next_x= %f\n", next_y, next_x);
|
// printf("for hroizon looking left\n");
|
||||||
printf("BASE p_y= %f p_x= %f\n", img->player.y, img->player.x);
|
//else
|
||||||
|
// printf("for hroizon looking right\n");
|
||||||
|
//printf("\nray_y= %f ray_x= %f\n", ray_y, ray_x);
|
||||||
|
//printf("next_y= %f next_x= %f\n", next_y, next_x);
|
||||||
|
//printf("BASE p_y= %f p_x= %f\n", img->player.y, img->player.x);
|
||||||
while (count < 8)
|
while (count < 8)
|
||||||
{
|
{
|
||||||
//printf("count = %d\n", count);
|
//printf("count = %d\n", count);
|
||||||
@ -70,7 +99,7 @@ void draw_ray(t_data *img)
|
|||||||
if (mp > 0 && mp < img->map.x * img->map.y && img->map.simple_map[mp] == '1')//hit wall
|
if (mp > 0 && mp < img->map.x * img->map.y && img->map.simple_map[mp] == '1')//hit wall
|
||||||
{
|
{
|
||||||
count = 8;
|
count = 8;
|
||||||
printf("vertical wall\n");
|
//printf("vertical wall\n");
|
||||||
dist_v = cos(deg_to_rad(ray_angle)) * (ray_x-img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y-img->player.y);
|
dist_v = cos(deg_to_rad(ray_angle)) * (ray_x-img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y-img->player.y);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -80,7 +109,10 @@ void draw_ray(t_data *img)
|
|||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//end vertical ray && start horizontal ray
|
//print_ray2(img, img->player.vx, img->player.vy, dist_v);
|
||||||
|
vx = ray_x;
|
||||||
|
vy = ray_y;
|
||||||
|
//-------start horizontal ray---------
|
||||||
count = 0;
|
count = 0;
|
||||||
aTan = 1.0 / aTan;
|
aTan = 1.0 / aTan;
|
||||||
if (sin(deg_to_rad(ray_angle)) > 0.001)//looking up
|
if (sin(deg_to_rad(ray_angle)) > 0.001)//looking up
|
||||||
@ -88,7 +120,7 @@ void draw_ray(t_data *img)
|
|||||||
ray_y = (((int)img->player.y>>6)<<6) - 0.0001;
|
ray_y = (((int)img->player.y>>6)<<6) - 0.0001;
|
||||||
ray_x = (img->player.y - ray_y) * aTan + img->player.x;
|
ray_x = (img->player.y - ray_y) * aTan + img->player.x;
|
||||||
next_y = -64;
|
next_y = -64;
|
||||||
next_x = next_y * aTan;
|
next_x = -next_y * aTan;
|
||||||
}
|
}
|
||||||
else if (sin(deg_to_rad(ray_angle))<-0.001)//looking down
|
else if (sin(deg_to_rad(ray_angle))<-0.001)//looking down
|
||||||
{
|
{
|
||||||
@ -102,25 +134,43 @@ void draw_ray(t_data *img)
|
|||||||
ray_x = img->player.x;
|
ray_x = img->player.x;
|
||||||
ray_y = img->player.y;
|
ray_y = img->player.y;
|
||||||
count = 8;
|
count = 8;
|
||||||
} //looking straight left or right
|
}//looking straight left or right
|
||||||
|
|
||||||
while (count<8)
|
while (count<8)
|
||||||
{
|
{
|
||||||
|
//printf("ray_y= %f ray_x= %f\n", ray_y, ray_x);
|
||||||
mx = (int)(ray_x)>>6;
|
mx = (int)(ray_x)>>6;
|
||||||
my = (int)(ray_y)>>6;
|
my = (int)(ray_y)>>6;
|
||||||
mp = my * img->map.x + mx;
|
mp = my * img->map.x + mx;
|
||||||
|
//printf("mx=%d my=%d mp= %d\n", mx, my, mp);
|
||||||
if (mp > 0 && mp < img->map.x * img->map.y && img->map.simple_map[mp] == '1')
|
if (mp > 0 && mp < img->map.x * img->map.y && img->map.simple_map[mp] == '1')
|
||||||
{
|
{
|
||||||
count = 8;
|
count = 8;
|
||||||
printf ("horizontal wall\n");
|
//printf ("horizontal wall\n");
|
||||||
|
//printf("case: x= %d, y= %d mp= %d\n", mx, my, mp);
|
||||||
dist_h = cos(deg_to_rad(ray_angle)) * (ray_x - img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y - img->player.y);
|
dist_h = cos(deg_to_rad(ray_angle)) * (ray_x - img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y - img->player.y);
|
||||||
}//hit
|
}//hit
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ray_x += next_x;
|
ray_x += next_x;
|
||||||
ray_y += next_y;
|
ray_y += next_y;
|
||||||
count += 1;
|
count += 1;
|
||||||
} //check next horizontal
|
} //check next horizontal
|
||||||
}
|
}
|
||||||
printf("dist_h= %f dist_v= %f\n", dist_h, dist_v);
|
//printf("dist_h= %f dist_v= %f\n", dist_h, dist_v);
|
||||||
|
vx = cos(deg_to_rad(ray_angle));
|
||||||
|
vy = -sin(deg_to_rad(ray_angle));
|
||||||
|
//printf("player.vx= %f vx= %f player.vy= %f vy= %f\n", img->player.vx, vx, img->player.vy, vy);
|
||||||
|
if ((dist_h != -1 || dist_v != -1) && dist_h < dist_v && dist_h > 0)
|
||||||
|
{
|
||||||
|
print_ray2(img, vx, vy, fabs(dist_h));
|
||||||
|
dist_f = dist_h;
|
||||||
|
}
|
||||||
|
else if (dist_h != -1 || dist_v != -1)
|
||||||
|
{
|
||||||
|
dist_f = dist_v;
|
||||||
|
print_ray2(img, vx, vy, fabs(dist_v));
|
||||||
|
}
|
||||||
|
ray_angle = reset_angle(ray_angle - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user