feat: raycasting part 1

This commit is contained in:
Floriansylvain
2025-12-28 05:35:35 +01:00
parent 62655038f3
commit 4402ec9159
4 changed files with 101 additions and 13 deletions
+59 -12
View File
@@ -1,6 +1,12 @@
#include "Game.hpp"
#include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window/Mouse.hpp>
#include <cmath>
Game::Game() : gridLines(WIDTH, HEIGHT, 20.f), player(15.f, sf::Color::Green) {
Game::Game()
: gridLines(WIDTH, HEIGHT, 20.f), player(15.f, sf::Color::Green),
rayVertices(sf::PrimitiveType::Lines) {
window.create(sf::VideoMode({WIDTH, HEIGHT}), "SFML Game");
window.setVerticalSyncEnabled(VSYNC);
@@ -10,17 +16,6 @@ Game::Game() : gridLines(WIDTH, HEIGHT, 20.f), player(15.f, sf::Color::Green) {
worldView.setSize({(float)WIDTH, (float)HEIGHT});
worldView.setCenter({(float)WIDTH / 2.f, (float)HEIGHT / 2.f});
walls.add({1.f, 1.f}, {WIDTH - 1.f, 1.f});
walls.add({1.f, 1.f}, {1.f, HEIGHT - 1.f});
walls.add({WIDTH - 1.f, 1.f}, {WIDTH - 1.f, HEIGHT - 1.f});
walls.add({1.f, HEIGHT - 1.f}, {WIDTH - 1.f, HEIGHT - 1.f});
walls.add({1000.f, 500.f}, {1500.f, 500.f});
walls.add({500.f, 240.f}, {500.f, 760.f});
walls.add({420.f, 240.f}, {420.f, 760.f});
walls.add({420.f, 240.f}, {500.f, 240.f});
walls.add({420.f, 760.f}, {500.f, 760.f});
}
void Game::run() {
@@ -63,9 +58,57 @@ void Game::processEvents() {
};
};
float Game::intersects(Ray ray, Wall wall) {
auto wallVector = wall.end - wall.start;
auto determinator =
(ray.direction.x * wallVector.y) - (ray.direction.y * wallVector.x);
if (determinator == 0)
return false;
auto rayDist = ((wall.start.x - ray.origin.x) * wallVector.y -
(wall.start.y - ray.origin.y) * wallVector.x);
rayDist /= determinator;
auto wallDist = (wall.start.x - ray.origin.x) * ray.direction.y -
(wall.start.y - ray.origin.y) * ray.direction.x;
wallDist /= determinator;
if (rayDist > 0 && wallDist >= 0 && wallDist <= 1) {
return rayDist;
}
return INFINITY;
};
void Game::update(double dt) {
debug.update(dt);
player.update(dt, window, worldView);
// INIT/UPDATE RAYS -------
auto mousePos = sf::Mouse::getPosition(window);
auto mousePosWorld = window.mapPixelToCoords(mousePos, worldView);
rayVertices.clear();
for (sf::Vector2 targetPoint : walls.getPoints()) {
auto ray = Ray(mousePosWorld, targetPoint);
auto closestDist = 1.0f;
for (Wall wall : walls.get()) {
auto rayDist = intersects(ray, wall);
if (rayDist < closestDist) {
closestDist = rayDist;
};
}
sf::Vector2f hitPoint = ray.origin + (ray.direction * closestDist);
rayVertices.append({ray.origin, sf::Color::Blue});
rayVertices.append({hitPoint, sf::Color::Blue});
}
// TODO: implement 3-rays trick to have softer angles
// -----------------
};
void Game::render() {
@@ -77,5 +120,9 @@ void Game::render() {
player.render(window);
walls.render(window);
// RENDER RAYS -------
window.draw(rayVertices);
// -----------------
window.display();
};
+22
View File
@@ -2,6 +2,10 @@
#define GAME_HPP
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/PrimitiveType.hpp>
#include <SFML/Graphics/VertexArray.hpp>
#include <SFML/Graphics/VertexBuffer.hpp>
#include <SFML/System/Vector2.hpp>
#include "DebugUI.hpp"
#include "GridLines.hpp"
@@ -14,6 +18,20 @@ constexpr int HEIGHT = 1080;
constexpr int DEBUG_REFRESH_RATE_IN_MS = 250;
constexpr float ASPECT = 16.f / 9.f;
struct Ray {
sf::Vector2f origin;
sf::Vector2f direction;
float angle;
Ray(sf::Vector2f origin, sf::Vector2f target) {
this->origin = origin;
this->direction = target - origin;
angle = direction.angleTo(origin).asDegrees();
};
Ray(float angle, float length) {}; // TODO
};
class Game {
public:
Game();
@@ -28,6 +46,10 @@ private:
GridLines gridLines;
Walls walls;
sf::VertexArray rayVertices;
float intersects(Ray ray, Wall wall);
void onResize();
void processEvents();
void update(double dt);
+18 -1
View File
@@ -1,14 +1,31 @@
#include "Walls.hpp"
#include "Game.hpp"
#include <SFML/Graphics/Vertex.hpp>
#include <SFML/System/Vector2.hpp>
Walls::Walls() : walls(sf::PrimitiveType::Lines) {};
Walls::Walls() : walls(sf::PrimitiveType::Lines) {
add({1.f, 1.f}, {WIDTH - 1.f, 1.f});
add({1.f, 1.f}, {1.f, HEIGHT - 1.f});
add({WIDTH - 1.f, 1.f}, {WIDTH - 1.f, HEIGHT - 1.f});
add({1.f, HEIGHT - 1.f}, {WIDTH - 1.f, HEIGHT - 1.f});
add({1000.f, 500.f}, {1500.f, 500.f});
add({500.f, 240.f}, {500.f, 760.f});
add({420.f, 240.f}, {420.f, 760.f});
add({420.f, 240.f}, {500.f, 240.f});
add({420.f, 760.f}, {500.f, 760.f});
};
void Walls::add(sf::Vector2f pointA, sf::Vector2f pointB) {
walls.append({pointA, sf::Color::Red});
walls.append({pointB, sf::Color::Red});
vertices.emplace_back(Wall{pointA, pointB});
points.emplace_back(pointA);
points.emplace_back(pointB);
};
std::vector<Wall> Walls::get() { return vertices; };
std::vector<sf::Vector2f> Walls::getPoints() { return points; };
void Walls::render(sf::RenderWindow &window) { window.draw(walls); };
+2
View File
@@ -18,12 +18,14 @@ public:
void add(sf::Vector2f pointA, sf::Vector2f pointB);
std::vector<Wall> get();
std::vector<sf::Vector2f> getPoints();
void render(sf::RenderWindow &window);
private:
sf::VertexArray walls;
std::vector<Wall> vertices;
std::vector<sf::Vector2f> points;
};
#endif // WALLS_HPP