42_cub3d/move.c
mcolonna fc2b243daa s
2024-10-15 12:56:03 +02:00

73 lines
2.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */
/* Updated: 2024/10/15 12:52:05 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
int keypress(int keycode)
{
double dirX, dirY, planeX, planeY;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
vector_from_rotation(&planeX, &planeY, g_ray.rot + PI/2, FOV);
//move forward if no wall in front of you
if (keycode == XK_Up || keycode == XK_z || keycode == XK_w)
{
if (worldMap[(int)((g_ray.posX + dirX * MOVE_SPEED))][(int)(g_ray.posY)] != 1)
g_ray.posX += dirX * MOVE_SPEED;
if (worldMap[(int)(g_ray.posX)][(int)(g_ray.posY + dirY * MOVE_SPEED)] != 1)
g_ray.posY += dirY * MOVE_SPEED;
}
//move backwards if no wall behind you
if (keycode == XK_Down || keycode == XK_s)
{
if (worldMap[(int)(g_ray.posX - dirX * MOVE_SPEED)][(int)(g_ray.posY)] != 1)
g_ray.posX -= dirX * MOVE_SPEED;
if (worldMap[(int)(g_ray.posX)][(int)(g_ray.posY - dirY * MOVE_SPEED)] != 1)
g_ray.posY -= dirY * MOVE_SPEED;
}
//rotate to the right
if (keycode == XK_Right || keycode == XK_d)
{
g_ray.rot += ROT_SPEED;
// double oldDirX;
// double oldPlaneX;
// //both camera direction and camera plane must be rotated
// oldDirX = dirX;
// dirX = dirX * cos(-ROT_SPEED) - dirY * sin(-ROT_SPEED);
// dirY = oldDirX * sin(-ROT_SPEED) + dirY * cos(-ROT_SPEED);
// oldPlaneX = planeX;
// planeX = planeX * cos(-ROT_SPEED) - planeY * sin(-ROT_SPEED);
// planeY = oldPlaneX * sin(-ROT_SPEED) + planeY * cos(-ROT_SPEED);
}
//rotate to the left
if (keycode == XK_Left || keycode == XK_q || keycode == XK_a)
{
g_ray.rot -= ROT_SPEED;
// double oldDirX;
// double oldPlaneX;
// //both camera direction and camera plane must be rotated
// oldDirX = dirX;
// dirX = dirX * cos(ROT_SPEED) - dirY * sin(ROT_SPEED);
// dirY = oldDirX * sin(ROT_SPEED) + dirY * cos(ROT_SPEED);
// oldPlaneX = planeX;
// planeX = planeX * cos(ROT_SPEED) - planeY * sin(ROT_SPEED);
// planeY = oldPlaneX * sin(ROT_SPEED) + planeY * cos(ROT_SPEED);
}
if (keycode == XK_Escape)
exit(0);
printf("x:%f y:%f\n", g_ray.posX, g_ray.posY);
// render the updated frame after key press
draw_screen();
return (0);
}