raycasting/camera.cpp

37 lines
827 B
C++
Raw Normal View History

2023-04-01 07:16:14 +08:00
#include "camera.h"
#include "maths.h"
#include "level.h"
2023-04-01 07:16:14 +08:00
#define ROTATION_SPEED PI
#define TRANSLATIONAL_SPEED 1.f
2023-04-01 07:16:14 +08:00
2023-04-02 07:41:32 +08:00
static void move(Camera* camera, float t);
2023-04-01 07:16:14 +08:00
void camera_update(Camera* camera, float t)
2023-04-01 07:16:14 +08:00
{
if (!camera) return;
2023-04-01 07:16:14 +08:00
2023-04-02 07:41:32 +08:00
move(camera, t);
}
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;
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;
}