Add camera movement

This commit is contained in:
Sheldon Lee 2023-04-02 00:41:32 +01:00
parent 15c3fe7604
commit 0cd0d410a1
3 changed files with 46 additions and 8 deletions

View File

@ -1,24 +1,40 @@
#include "camera.h"
#include <cmath>
#include <SFML/Graphics/Color.hpp>
#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 move(Camera* camera, float t);
void camera_update(Camera* camera, sf::RenderWindow* window)
void camera_update(Camera* camera, sf::RenderWindow* window, float t)
{
if (!camera || !window) return;
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);
drawLine(window, camera->pos, camera->direction, 100, sf::Color::Red);
window->draw(circle);
}
static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color)
{
if (!window) return;
sf::Vector2f endOffset(length * cos(angle * PI/180.f), length * sin(angle * PI/180.3));
sf::Vector2f endOffset(length * cos(angle * DEG_RAD), length * sin(angle * DEG_RAD));
sf::Vertex start(pos);
sf::Vertex end(pos + endOffset);
@ -29,3 +45,23 @@ static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, fl
window->draw(line, 2, sf::Lines);
}
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 * DEG_RAD), magnitude *sin(camera->direction * DEG_RAD));
camera->pos += offset;
camera->direction += rotation * t * ROTATION_SPEED;
}

View File

@ -1,8 +1,7 @@
#ifndef CAMERA_H
#define CAMERA_H
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics.hpp>
typedef struct
{
@ -12,6 +11,6 @@ typedef struct
float fov;
} Camera;
void camera_update(Camera* camera, sf::RenderWindow* window);
void camera_update(Camera* camera, sf::RenderWindow* window, float t);
#endif

View File

@ -9,6 +9,7 @@ static int handleKeyCode(sf::Keyboard::Key key);
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 };
@ -37,8 +38,10 @@ int view_update()
}
window.clear();
sf::Time t = timer.restart();
if (!level_update()) return 0;
camera_update(&camera, &window);
camera_update(&camera, &window, t.asSeconds());
window.display();
return 1;