86 lines
3 KiB
C
86 lines
3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* move.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/10/14 15:03:10 by greg #+# #+# */
|
|
/* Updated: 2024/10/14 16:53:56 by greg ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "algo.h"
|
|
|
|
static int move_forward(t_ray *ray)
|
|
{
|
|
if (worldMap[(int)((ray->posX + ray->dirX * ray->movespeed))][(int)(ray->posY)] != 1)
|
|
ray->posX += ray->dirX * ray->movespeed;
|
|
if (worldMap[(int)(ray->posX)][(int)(ray->posY + ray->dirY * ray->movespeed)] != 1)
|
|
ray->posY += ray->dirY * ray->movespeed;
|
|
return(0);
|
|
}
|
|
|
|
static int move_backward(t_ray *ray)
|
|
{
|
|
if (worldMap[(int)(ray->posX - ray->dirX * ray->movespeed)][(int)(ray->posY)] != 1)
|
|
ray->posX -= ray->dirX * ray->movespeed;
|
|
if (worldMap[(int)(ray->posX)][(int)(ray->posY - ray->dirY * ray->movespeed)] != 1)
|
|
ray->posY -= ray->dirY * ray->movespeed;
|
|
return(0);
|
|
}
|
|
|
|
static int move_right(t_ray *ray)
|
|
{
|
|
//both camera direction and camera plane must be rotated
|
|
ray->oldDirX = ray->dirX;
|
|
ray->dirX = ray->dirX * cos(ray->rotspeed) - ray->dirY * sin(ray->rotspeed);
|
|
ray->dirY = ray->oldDirX * sin(ray->rotspeed) + ray->dirY * cos(ray->rotspeed);
|
|
ray->oldPlaneX = ray->planeX;
|
|
ray->planeX = ray->planeX * cos(ray->rotspeed) - ray->planeY * sin(ray->rotspeed);
|
|
ray->planeY = ray->oldPlaneX * sin(ray->rotspeed) + ray->planeY * cos(ray->rotspeed);
|
|
return(0);
|
|
}
|
|
|
|
static int move_left(t_ray *ray)
|
|
{
|
|
//both camera direction and camera plane must be rotated
|
|
ray->oldDirX = ray->dirX;
|
|
ray->dirX = ray->dirX * cos(-ray->rotspeed) - ray->dirY * sin(-ray->rotspeed);
|
|
ray->dirY = ray->oldDirX * sin(-ray->rotspeed) + ray->dirY * cos(-ray->rotspeed);
|
|
ray->oldPlaneX = ray->planeX;
|
|
ray->planeX = ray->planeX * cos(-ray->rotspeed) - ray->planeY * sin(-ray->rotspeed);
|
|
ray->planeY = ray->oldPlaneX * sin(-ray->rotspeed) + ray->planeY * cos(-ray->rotspeed);
|
|
return(0);
|
|
}
|
|
|
|
|
|
|
|
|
|
int keypress(int keycode, t_ray *ray)
|
|
{
|
|
if (keycode == 119) //move forward if no wall in front of you
|
|
{
|
|
move_forward(ray);
|
|
}
|
|
if (keycode == 115) //move backwards if no wall behind you
|
|
{
|
|
move_backward(ray);
|
|
}
|
|
if (keycode == 100) //rotate to the right
|
|
{
|
|
move_left(ray);
|
|
}
|
|
if (keycode == 97) //rotate to the left
|
|
{
|
|
move_right(ray);
|
|
}
|
|
if (keycode == 65307)
|
|
{
|
|
mlx_destroy_window(ray->mlx_ptr, ray->win_ptr);
|
|
exit(0);
|
|
}
|
|
// render the updated frame after key pressd
|
|
render(ray);
|
|
return (0);
|
|
}
|