diff --git a/camera.cpp b/camera.cpp index dd73977..1bfc6fb 100644 --- a/camera.cpp +++ b/camera.cpp @@ -1,13 +1,16 @@ #include "camera.h" + #include +#include "level.h" #define PI 3.14159265 #define DEG_RAD PI/180.f #define ROTATION_SPEED 180 #define TRANSLATIONAL_SPEED 100.f -static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color); static void draw(Camera* camera, sf::RenderWindow* window); +static void drawRays(Camera* camera, sf::RenderWindow* window); +static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color); static void move(Camera* camera, float t); void camera_update(Camera* camera, sf::RenderWindow* window, float t) @@ -26,10 +29,24 @@ static void draw(Camera* camera, sf::RenderWindow* window) circle.setOrigin(circleRadius, circleRadius); circle.setFillColor(sf::Color::Green); + drawRays(camera, window); drawLine(window, camera->pos, camera->direction, 100, sf::Color::Red); window->draw(circle); } +static void drawRays(Camera* camera, sf::RenderWindow* window) +{ + float halfFOV = camera->fov/2.f; + float rayDirection = camera->direction - halfFOV; + float rayDirectionStep = camera->fov / (float)camera->resolution; + + for (unsigned int i = 0; i < camera->resolution; i++) { + float distance = level_rayCastDistance(camera->pos, rayDirection); + drawLine(window, camera->pos, rayDirection, distance, sf::Color::Blue); + rayDirection += rayDirectionStep; + } +} + static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color) { if (!window) return; diff --git a/camera.h b/camera.h index 697f78c..5ae43ea 100644 --- a/camera.h +++ b/camera.h @@ -7,7 +7,7 @@ typedef struct { sf::Vector2f pos; float direction; - float resolution; + unsigned int resolution; float fov; } Camera; diff --git a/level.cpp b/level.cpp index 9ab3d2e..3b5254b 100644 --- a/level.cpp +++ b/level.cpp @@ -12,9 +12,9 @@ static sf::RenderWindow* window = nullptr; static unsigned int level[WIDTH * HEIGHT] = { 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, - 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, }; @@ -38,6 +38,11 @@ void level_end() return; } +float level_rayCastDistance(sf::Vector2f point, float direction) +{ + return 100.f; +} + static void drawGrid() { const sf::Vector2u windowSize = window->getSize(); diff --git a/level.h b/level.h index cf6c38a..c4c5b9d 100644 --- a/level.h +++ b/level.h @@ -6,5 +6,6 @@ int level_init(sf::RenderWindow* renderWindow); int level_update(); void level_end(); +float level_rayCastDistance(sf::Vector2f point, float direction); #endif diff --git a/view.cpp b/view.cpp index 63d4de9..7d22b8e 100644 --- a/view.cpp +++ b/view.cpp @@ -11,7 +11,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, 1.f, 70.f }; +static Camera camera = { sf::Vector2f(300.f, 250.f), 0.f, 20, 360.f }; int view_init() {