diff --git a/camera.cpp b/camera.cpp index 1bfc6fb..436bffe 100644 --- a/camera.cpp +++ b/camera.cpp @@ -1,11 +1,9 @@ #include "camera.h" -#include +#include "maths.h" #include "level.h" -#define PI 3.14159265 -#define DEG_RAD PI/180.f -#define ROTATION_SPEED 180 +#define ROTATION_SPEED PI #define TRANSLATIONAL_SPEED 100.f static void draw(Camera* camera, sf::RenderWindow* window); @@ -36,14 +34,24 @@ static void draw(Camera* camera, sf::RenderWindow* window) static void drawRays(Camera* camera, sf::RenderWindow* window) { - float halfFOV = camera->fov/2.f; - float rayDirection = camera->direction - halfFOV; + float rayDirection = 0; float rayDirectionStep = camera->fov / (float)camera->resolution; + bool isOddResolution = (camera->resolution % 2); + float rayDirectionOffset = isOddResolution? 0 : rayDirectionStep / 2.f; for (unsigned int i = 0; i < camera->resolution; i++) { + if (isOddResolution && i == 0) + rayDirection = camera->direction; + else if (i % 2) + rayDirection = camera->direction - rayDirectionOffset; + else + rayDirection = camera->direction + rayDirectionOffset; + float distance = level_rayCastDistance(camera->pos, rayDirection); + drawLine(window, camera->pos, rayDirection, distance, sf::Color::Blue); - rayDirection += rayDirectionStep; + + if ((i + isOddResolution) % 2) rayDirectionOffset += rayDirectionStep; } } @@ -51,7 +59,7 @@ static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, fl { if (!window) return; - sf::Vector2f endOffset(length * cos(angle * DEG_RAD), length * sin(angle * DEG_RAD)); + sf::Vector2f endOffset(length * cos(angle), length * sin(angle)); sf::Vertex start(pos); sf::Vertex end(pos + endOffset); @@ -77,7 +85,7 @@ static void move(Camera* camera, float t) rotation -=1; float magnitude = forward * t * TRANSLATIONAL_SPEED; - sf::Vector2f offset(magnitude * cos(camera->direction * DEG_RAD), magnitude *sin(camera->direction * DEG_RAD)); + sf::Vector2f offset(magnitude * cos(camera->direction), magnitude *sin(camera->direction)); camera->pos += offset; camera->direction += rotation * t * ROTATION_SPEED; diff --git a/level.cpp b/level.cpp index 3b5254b..199f38b 100644 --- a/level.cpp +++ b/level.cpp @@ -1,6 +1,8 @@ -#include "level.h" +#include "level.h" #include +#include "maths.h" + #define WIDTH 5 #define HEIGHT 5 @@ -8,6 +10,9 @@ static void drawGrid(); static void drawGridLine(unsigned int step, bool isHorizontal); static sf::Vertex getGridLineVertex(unsigned int n, unsigned int maxDimension, bool isStart, bool isHorizontal); +static void castRay(sf::Vector2f point, float direction); +static void getGridIndex(sf::Vector2f point, int* x, int* y); + static sf::RenderWindow* window = nullptr; static unsigned int level[WIDTH * HEIGHT] = { @@ -40,14 +45,15 @@ void level_end() float level_rayCastDistance(sf::Vector2f point, float direction) { - return 100.f; + castRay(point, direction); + return 1000.f; } static void drawGrid() { const sf::Vector2u windowSize = window->getSize(); - unsigned int stepX = windowSize.x/WIDTH; - unsigned int stepY = windowSize.y/HEIGHT; + const unsigned int stepX = windowSize.x/WIDTH; + const unsigned int stepY = windowSize.y/HEIGHT; for (unsigned int x = 0; x < WIDTH; x++) { for (unsigned int y = 0; y < HEIGHT; y++) { @@ -100,3 +106,83 @@ static sf::Vertex getGridLineVertex(unsigned int offset, unsigned int maxDimensi end.color = sf::Color(100, 100, 100); return isStart? start : end; } + +static void castRay(sf::Vector2f point, float direction) +{ + const sf::Vector2u windowSize = window->getSize(); + const unsigned int tileWidth = windowSize.x/WIDTH; + const unsigned int tileHeight = windowSize.y/HEIGHT; + + // dx and dy are the delta x and delta y of closest grid intersection + int indexX, indexY; + float dx, dy; + + getGridIndex(point, &indexX, &indexY); + + direction = maths_modulo(direction, 2.0f*PI); + bool goingDown = direction < PI; + int signDown = goingDown? 1 : -1; + + dy = (float)((indexY + goingDown) * tileHeight) - point.y; + dx = dy/tan(direction); + + float horizontalStepX = (float)(signDown * (tileWidth/tan(direction))); + float horizontalStepY = (float)(signDown * (int)tileHeight); + float horizontalProjectedX = point.x + dx; + float horizontalProjectedY = (indexY + goingDown) * tileHeight; + + direction = maths_modulo(direction + 0.5f*PI, 2.0f*PI); + bool goingRight = direction < PI; + int signRight = goingRight? 1 : -1; + + dx = (float)((indexX + goingRight) * tileWidth) - point.x; + dy = -dx/tan(direction); + + float verticalStepY = -(float)(signRight * (tileHeight/tan(direction))); + float verticalStepX = (float)(signRight * (int)tileHeight); + float verticalProjectedY = point.y + dy; + float verticalProjectedX = (indexX + goingRight) * tileWidth; + + bool inLevel; + do { + const float circleRadius = 3.f; + sf::CircleShape circle(circleRadius); + + circle.setFillColor(sf::Color::Red); + + circle.setPosition(sf::Vector2f(horizontalProjectedX, horizontalProjectedY)); + circle.setOrigin(circleRadius, circleRadius); + window->draw(circle); + + circle.setPosition(sf::Vector2f(verticalProjectedX, verticalProjectedY)); + circle.setOrigin(circleRadius, circleRadius); + window->draw(circle); + + horizontalProjectedX += horizontalStepX; + horizontalProjectedY += horizontalStepY; + + verticalProjectedX += verticalStepX; + verticalProjectedY += verticalStepY; + + int indexX0, indexY0; + int indexX1, indexY1; + getGridIndex(sf::Vector2f(horizontalProjectedX, horizontalProjectedY), &indexX0, &indexY0); + getGridIndex(sf::Vector2f(verticalProjectedX, verticalProjectedY), &indexX1, &indexY1); + + bool inLevel0 = ((indexX0 >= 0 && indexX0 < WIDTH) && (indexY0 >= 0 && indexY0 < HEIGHT)); + bool inLevel1 = ((indexX1 >= 0 && indexX1 < WIDTH) && (indexY1 >= 0 && indexY1 < HEIGHT)); + + inLevel = inLevel0 || inLevel1; + } while (inLevel); +} + +static void getGridIndex(sf::Vector2f point, int* x, int* y) +{ + const sf::Vector2u windowSize = window->getSize(); + + *x = point.x / (int)(windowSize.x / WIDTH); + *y = point.y / (int)(windowSize.y / HEIGHT); + + if (*x < 0 || WIDTH <= *x) *x = -1; + if (*y < 0 || HEIGHT <= *y) *y = -1; +} diff --git a/maths.cpp b/maths.cpp new file mode 100644 index 0000000..e89fb94 --- /dev/null +++ b/maths.cpp @@ -0,0 +1,7 @@ +#include "maths.h" + +float maths_modulo(float a, float b) +{ + float r = fmod(a, b); + return r < 0? r + b : r; +} diff --git a/maths.h b/maths.h new file mode 100644 index 0000000..56e500c --- /dev/null +++ b/maths.h @@ -0,0 +1,11 @@ +#ifndef MATHS_H +#define MATHS_H + +#include + +#define PI 3.14159265 +#define DEG_RAD PI/180.f + +float maths_modulo(float a, float b); + +#endif diff --git a/view.cpp b/view.cpp index 7d22b8e..3a40a0d 100644 --- a/view.cpp +++ b/view.cpp @@ -4,6 +4,7 @@ #include "level.h" #include "camera.h" +#include "maths.h" static int handleKeyCode(sf::Keyboard::Key key); @@ -11,7 +12,7 @@ static sf::Uint32 style = sf::Style::Titlebar; static sf::RenderWindow window(sf::VideoMode(500, 500), "Raycasting", style); static sf::Clock timer; -static Camera camera = { sf::Vector2f(300.f, 250.f), 0.f, 20, 360.f }; +static Camera camera = { sf::Vector2f(300.f, 250.f), 0.f, 50, 2.0f*PI }; int view_init() {