Draw rays from camera and call ray casting stub function.

This commit is contained in:
Sheldon Lee 2023-04-02 01:21:02 +01:00
parent f1cb03bc22
commit ca50b6f4b7
5 changed files with 28 additions and 5 deletions

View File

@ -1,13 +1,16 @@
#include "camera.h" #include "camera.h"
#include <cmath> #include <cmath>
#include "level.h"
#define PI 3.14159265 #define PI 3.14159265
#define DEG_RAD PI/180.f #define DEG_RAD PI/180.f
#define ROTATION_SPEED 180 #define ROTATION_SPEED 180
#define TRANSLATIONAL_SPEED 100.f #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 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); static void move(Camera* camera, float t);
void camera_update(Camera* camera, sf::RenderWindow* window, 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.setOrigin(circleRadius, circleRadius);
circle.setFillColor(sf::Color::Green); circle.setFillColor(sf::Color::Green);
drawRays(camera, window);
drawLine(window, camera->pos, camera->direction, 100, sf::Color::Red); drawLine(window, camera->pos, camera->direction, 100, sf::Color::Red);
window->draw(circle); 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) static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color)
{ {
if (!window) return; if (!window) return;

View File

@ -7,7 +7,7 @@ typedef struct
{ {
sf::Vector2f pos; sf::Vector2f pos;
float direction; float direction;
float resolution; unsigned int resolution;
float fov; float fov;
} Camera; } Camera;

View File

@ -12,9 +12,9 @@ static sf::RenderWindow* window = nullptr;
static unsigned int level[WIDTH * HEIGHT] = { static unsigned int level[WIDTH * HEIGHT] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 1, 0, 1, 1, 0, 1, 0, 1,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 1, 0, 1, 1, 0, 1, 0, 1,
}; };
@ -38,6 +38,11 @@ void level_end()
return; return;
} }
float level_rayCastDistance(sf::Vector2f point, float direction)
{
return 100.f;
}
static void drawGrid() static void drawGrid()
{ {
const sf::Vector2u windowSize = window->getSize(); const sf::Vector2u windowSize = window->getSize();

View File

@ -6,5 +6,6 @@
int level_init(sf::RenderWindow* renderWindow); int level_init(sf::RenderWindow* renderWindow);
int level_update(); int level_update();
void level_end(); void level_end();
float level_rayCastDistance(sf::Vector2f point, float direction);
#endif #endif

View File

@ -11,7 +11,7 @@ static sf::Uint32 style = sf::Style::Titlebar;
static sf::RenderWindow window(sf::VideoMode(500, 500), "Raycasting", style); static sf::RenderWindow window(sf::VideoMode(500, 500), "Raycasting", style);
static sf::Clock timer; 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() int view_init()
{ {