diff --git a/algo.c b/algo.c index 2dc1cbb..b67db6a 100644 --- a/algo.c +++ b/algo.c @@ -6,7 +6,7 @@ /* By: greg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */ -/* Updated: 2024/10/03 17:42:13 by greg ### ########.fr */ +/* Updated: 2024/10/03 18:59:06 by greg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ static int initalgo(t_ray *ray) ray->planeY = 0.66; // FOV de 66 degres ray->currtime = 0; ray->oldtime = 0; + ray->movespeed = 0.1; + ray->rotspeed = 0.1; return (0); } @@ -47,78 +49,135 @@ int algo() x = 0; width = 640; height = 480; - while (x < w) + while (x < width) { - // position et direction du rayon - ray.cameraX = 2 * x / width - 1; - ray.raydirX = ray.dirX + ray.planeX * ray.cameraX; - ray.raydirY = ray.dirY + ray.planeY * ray.cameraX; + // position et direction du rayon + ray.cameraX = 2 * x / width - 1; + ray.raydirX = ray.dirX + ray.planeX * ray.cameraX; + ray.raydirY = ray.dirY + ray.planeY * ray.cameraX; - mapX = (int)ray.posX; - mapY = (int)ray.posY; + mapX = (int)ray.posX; + mapY = (int)ray.posY; - ray.deltadistX = (ray.raydirX == 0) ? 1e30 : fabs(1 / ray.raydirX); - ray.deltadistY = (ray.raydirY == 0) ? 1e30 : fabs(1 / ray.raydirY); + ray.deltadistX = (ray.raydirX == 0) ? 1e30 : fabs(1 / ray.raydirX); + ray.deltadistY = (ray.raydirY == 0) ? 1e30 : fabs(1 / ray.raydirY); - hit = 0; + hit = 0; - //calculate step and initial sideDist - if (ray.raydirX < 0) - { - ray.stepX = -1; - ray.sidedistX = (ray.posX - mapX) * ray.deltadistX; - } - else - { - ray.stepX = 1; - ray.sidedistX = (mapX + 1.0 - ray.posX) * ray.deltadistX; - } - if (ray.raydirY < 0) - { - ray.stepY = -1; - ray.sidedistY = (ray.posY - mapY) * ray.deltadistY; - } - else - { - ray.stepY = 1; - ray.sidedistY = (mapY + 1.0 - ray.posY) * ray.deltadistY; - } - while(hit == 0) - { - //jump to next map square, either in x-direction, or in y-direction - if(ray.sidedistX < ray.sidedistY) + //calculate step and initial sideDist + if (ray.raydirX < 0) { - ray.sidedistX += ray.deltadistX; - mapX += ray.stepX; - side = 0; + ray.stepX = -1; + ray.sidedistX = (ray.posX - mapX) * ray.deltadistX; } else { - ray.sidedistY += ray.deltadistY; - mapY += ray.stepY; - side = 1; + ray.stepX = 1; + ray.sidedistX = (mapX + 1.0 - ray.posX) * ray.deltadistX; } - //Check if ray has hit a wall - if(worldMap[mapX][mapY] == 1) - hit = 1; - } - if(side == 0) - ray.perpwalldist = (ray.sidedistX - ray.deltadistX); - else - ray.perpwalldist = (ray.sidedistY - ray.deltadistY); - //Calculate height of line to draw on screen - int lineHeight = (int)(height / ray.perpwalldist); - lineHeight = (int)(height / ray.perpwalldist); + if (ray.raydirY < 0) + { + ray.stepY = -1; + ray.sidedistY = (ray.posY - mapY) * ray.deltadistY; + } + else + { + ray.stepY = 1; + ray.sidedistY = (mapY + 1.0 - ray.posY) * ray.deltadistY; + } + while(hit == 0) + { + //jump to next map square, either in x-direction, or in y-direction + if(ray.sidedistX < ray.sidedistY) + { + ray.sidedistX += ray.deltadistX; + mapX += ray.stepX; + side = 0; + } + else + { + ray.sidedistY += ray.deltadistY; + mapY += ray.stepY; + side = 1; + } + //Check if ray has hit a wall + if(worldMap[mapX][mapY] == 1) + hit = 1; + } + if(side == 0) + ray.perpwalldist = (ray.sidedistX - ray.deltadistX); + else + ray.perpwalldist = (ray.sidedistY - ray.deltadistY); + //Calculate height of line to draw on screen + int lineHeight = (int)(height / ray.perpwalldist); + lineHeight = (int)(height / ray.perpwalldist); - //calculate lowest and highest pixel to fill in current stripe - int drawStart; - drawStart = -lineHeight / 2 + height / 2; - if(drawStart < 0) - drawStart = 0; - int drawEnd; - if(drawEnd >= height) - drawEnd = height - 1; + //calculate lowest and highest pixel to fill in current stripe + int drawStart; + drawStart = -lineHeight / 2 + height / 2; + if(drawStart < 0) + drawStart = 0; + int drawEnd; + if(drawEnd >= height) + drawEnd = height - 1; + /*mlx fontction color + + give x and y sides different brightness + if (side == 1) {color = color / 2;} + + //draw the pixels of the stripe as a vertical line + verLine(x, drawStart, drawEnd, color); */ + + + //timing for input to dissociate speed from speed processor (same speed on all pc) + ray.oldtime = ray.currtime; + ray.currtime = getTicks(); + // put image to window + // destroy old image + + + // hook event + + readKeys(); + //move forward if no wall in front of you + if (keyDown(move_up)) + { + if(worldMap[(ray.posX + ray.dirX * ray.movespeed)][(int)(ray.posY)] == 0) + ray.posX += ray.dirX * ray.movespeed; + if(worldMap[(ray.posX)][((int)ray.posY + (int)ray.dirY * (int)ray.movespeed)] == 0) + ray.posY += ray.dirY * ray.movespeed; + } + //move backwards if no wall behind you + if (keyDown(moove_down)) + { + if(worldMap[(ray.posX - ray.dirX * ray.movespeed)][(int)(ray.posY)] == 0) + ray.posX -= ray.dirX * ray.movespeed; + if(worldMap[(ray.posX)][((int)ray.posY - (int)ray.dirY * (int)ray.movespeed)] == 0) + ray.posY -= ray.dirY * ray.movespeed; + } + //rotate to the right + if (keyDown(moove_right)) + { + //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); + } + //rotate to the left + if (keyDown(moove_left)) + { + //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); diff --git a/algo.h b/algo.h index 8f89eeb..acb137f 100644 --- a/algo.h +++ b/algo.h @@ -6,7 +6,7 @@ /* By: greg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */ -/* Updated: 2024/10/03 17:26:27 by greg ### ########.fr */ +/* Updated: 2024/10/03 18:59:43 by greg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,10 +27,6 @@ typedef struct s_ray // orientation cam on map double planeX; double planeY; - // current frame - double currtime; - // last frame - double oldtime; // camera pos double cameraX; // direction rayon @@ -47,7 +43,10 @@ typedef struct s_ray // step direction (can be -1 or +1) int stepX; int stepY; - //wall hit? + double movespeed; + double rotspeed; + double oldDirX; + double oldPlaneX; } t_ray; #endif