2023-04-01 07:16:14 +08:00
|
|
|
#include "camera.h"
|
2023-04-02 08:21:02 +08:00
|
|
|
|
2023-04-05 03:02:49 +08:00
|
|
|
#include "maths.h"
|
2023-04-02 08:21:02 +08:00
|
|
|
#include "level.h"
|
2023-04-01 07:16:14 +08:00
|
|
|
|
2023-04-05 03:02:49 +08:00
|
|
|
#define ROTATION_SPEED PI
|
2023-04-02 07:41:32 +08:00
|
|
|
#define TRANSLATIONAL_SPEED 100.f
|
2023-04-01 07:16:14 +08:00
|
|
|
|
2023-04-02 07:41:32 +08:00
|
|
|
static void draw(Camera* camera, sf::RenderWindow* window);
|
2023-04-02 08:21:02 +08:00
|
|
|
static void drawRays(Camera* camera, sf::RenderWindow* window);
|
|
|
|
static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color);
|
2023-04-02 07:41:32 +08:00
|
|
|
static void move(Camera* camera, float t);
|
2023-04-01 07:16:14 +08:00
|
|
|
|
2023-04-02 07:41:32 +08:00
|
|
|
void camera_update(Camera* camera, sf::RenderWindow* window, float t)
|
2023-04-01 07:16:14 +08:00
|
|
|
{
|
|
|
|
if (!camera || !window) return;
|
|
|
|
|
2023-04-02 07:41:32 +08:00
|
|
|
draw(camera, window);
|
|
|
|
move(camera, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw(Camera* camera, sf::RenderWindow* window)
|
|
|
|
{
|
|
|
|
const float circleRadius = 5.f;
|
|
|
|
sf::CircleShape circle(circleRadius);
|
|
|
|
circle.setPosition(camera->pos);
|
|
|
|
circle.setOrigin(circleRadius, circleRadius);
|
|
|
|
circle.setFillColor(sf::Color::Green);
|
|
|
|
|
2023-04-02 08:21:02 +08:00
|
|
|
drawRays(camera, window);
|
2023-04-01 07:16:14 +08:00
|
|
|
drawLine(window, camera->pos, camera->direction, 100, sf::Color::Red);
|
2023-04-02 07:41:32 +08:00
|
|
|
window->draw(circle);
|
2023-04-01 07:16:14 +08:00
|
|
|
}
|
|
|
|
|
2023-04-02 08:21:02 +08:00
|
|
|
static void drawRays(Camera* camera, sf::RenderWindow* window)
|
|
|
|
{
|
2023-04-05 03:02:49 +08:00
|
|
|
float rayDirection = 0;
|
2023-04-02 08:21:02 +08:00
|
|
|
float rayDirectionStep = camera->fov / (float)camera->resolution;
|
2023-04-05 03:02:49 +08:00
|
|
|
bool isOddResolution = (camera->resolution % 2);
|
|
|
|
float rayDirectionOffset = isOddResolution? 0 : rayDirectionStep / 2.f;
|
2023-04-02 08:21:02 +08:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < camera->resolution; i++) {
|
2023-04-05 03:02:49 +08:00
|
|
|
if (isOddResolution && i == 0)
|
|
|
|
rayDirection = camera->direction;
|
|
|
|
else if (i % 2)
|
|
|
|
rayDirection = camera->direction - rayDirectionOffset;
|
|
|
|
else
|
|
|
|
rayDirection = camera->direction + rayDirectionOffset;
|
|
|
|
|
2023-04-02 08:21:02 +08:00
|
|
|
float distance = level_rayCastDistance(camera->pos, rayDirection);
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-02 08:21:02 +08:00
|
|
|
drawLine(window, camera->pos, rayDirection, distance, sf::Color::Blue);
|
2023-04-05 03:02:49 +08:00
|
|
|
|
|
|
|
if ((i + isOddResolution) % 2) rayDirectionOffset += rayDirectionStep;
|
2023-04-02 08:21:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 07:16:14 +08:00
|
|
|
static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color)
|
|
|
|
{
|
|
|
|
if (!window) return;
|
|
|
|
|
2023-04-05 03:02:49 +08:00
|
|
|
sf::Vector2f endOffset(length * cos(angle), length * sin(angle));
|
2023-04-01 07:16:14 +08:00
|
|
|
sf::Vertex start(pos);
|
|
|
|
sf::Vertex end(pos + endOffset);
|
|
|
|
|
|
|
|
start.color = color;
|
|
|
|
end.color = color;
|
|
|
|
|
|
|
|
sf::Vertex line[] = { start, end };
|
|
|
|
|
|
|
|
window->draw(line, 2, sf::Lines);
|
|
|
|
}
|
2023-04-02 07:41:32 +08:00
|
|
|
|
|
|
|
static void move(Camera* camera, float t)
|
|
|
|
{
|
|
|
|
int forward = 0;
|
|
|
|
int rotation = 0;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::F))
|
|
|
|
forward += 1;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
|
|
|
|
forward -= 1;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::T))
|
|
|
|
rotation +=1;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R))
|
|
|
|
rotation -=1;
|
|
|
|
|
|
|
|
float magnitude = forward * t * TRANSLATIONAL_SPEED;
|
2023-04-05 03:02:49 +08:00
|
|
|
sf::Vector2f offset(magnitude * cos(camera->direction), magnitude *sin(camera->direction));
|
2023-04-02 07:41:32 +08:00
|
|
|
|
|
|
|
camera->pos += offset;
|
|
|
|
camera->direction += rotation * t * ROTATION_SPEED;
|
|
|
|
}
|