feat: raycasting part 3

This commit is contained in:
Floriansylvain
2025-12-28 17:34:44 +01:00
parent f59a48ed51
commit 763a236882
5 changed files with 33 additions and 17 deletions
+10 -10
View File
@@ -111,25 +111,25 @@ void Game::update(double dt) {
player.update(dt, window, worldView); player.update(dt, window, worldView);
// INIT/UPDATE RAYS ------------------------------------------ // INIT/UPDATE RAYS ------------------------------------------
auto mousePos = sf::Mouse::getPosition(window); sf::Vector2 playerPosition = player.position;
auto mousePosWorld = window.mapPixelToCoords(mousePos, worldView);
rayVertices.clear(); rayVertices.clear();
hitPoints.clear(); hitPoints.clear();
for (sf::Vector2 targetPoint : walls.getPoints()) { for (sf::Vector2 targetPoint : walls.getPoints()) {
hitPoints.emplace_back(updateRay(mousePosWorld, targetPoint, 0.f)); hitPoints.emplace_back(updateRay(playerPosition, targetPoint, 0.f));
hitPoints.emplace_back(updateRay(mousePosWorld, targetPoint, 0.001f)); hitPoints.emplace_back(updateRay(playerPosition, targetPoint, 0.001f));
hitPoints.emplace_back(updateRay(mousePosWorld, targetPoint, -0.001f)); hitPoints.emplace_back(updateRay(playerPosition, targetPoint, -0.001f));
} }
std::sort(hitPoints.begin(), hitPoints.end(), std::sort(hitPoints.begin(), hitPoints.end(),
[mousePosWorld](sf::Vector2f a, sf::Vector2f b) -> bool { [playerPosition](sf::Vector2f a, sf::Vector2f b) -> bool {
auto angleA = atan2(mousePosWorld.y - a.y, mousePosWorld.x - a.x); auto angleA =
auto angleB = atan2(mousePosWorld.y - b.y, mousePosWorld.x - b.x); atan2(playerPosition.y - a.y, playerPosition.x - a.x);
auto angleB =
atan2(playerPosition.y - b.y, playerPosition.x - b.x);
return angleA < angleB; return angleA < angleB;
}); });
rayVertices.append({mousePosWorld, lightColor}); rayVertices.append({playerPosition, lightColor});
for (auto hitPoint : hitPoints) { for (auto hitPoint : hitPoints) {
rayVertices.append({hitPoint, lightColor}); rayVertices.append({hitPoint, lightColor});
} }
+1 -1
View File
@@ -12,7 +12,7 @@
#include "Player.hpp" #include "Player.hpp"
#include "Walls.hpp" #include "Walls.hpp"
constexpr bool VSYNC = false; constexpr bool VSYNC = true;
constexpr int WIDTH = 1920; constexpr int WIDTH = 1920;
constexpr int HEIGHT = 1080; constexpr int HEIGHT = 1080;
constexpr int DEBUG_REFRESH_RATE_IN_MS = 250; constexpr int DEBUG_REFRESH_RATE_IN_MS = 250;
+7 -2
View File
@@ -1,4 +1,5 @@
#include "Player.hpp" #include "Player.hpp"
#include "Game.hpp"
#include <SFML/Graphics/CircleShape.hpp> #include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/View.hpp> #include <SFML/Graphics/View.hpp>
@@ -11,8 +12,12 @@ void Player::update(double dt, sf::RenderWindow &window, const sf::View &view) {
auto mousePos = sf::Mouse::getPosition(window); auto mousePos = sf::Mouse::getPosition(window);
auto mousePosWorld = window.mapPixelToCoords(mousePos, view); auto mousePosWorld = window.mapPixelToCoords(mousePos, view);
circleShape.setPosition( if ((mousePosWorld.x > WIDTH || mousePosWorld.x < 0) ||
{mousePosWorld.x - shapeRadius, mousePosWorld.y - shapeRadius}); (mousePosWorld.y > HEIGHT || mousePosWorld.y < 0))
return;
position = {mousePosWorld.x - shapeRadius, mousePosWorld.y - shapeRadius};
circleShape.setPosition(position);
}; };
void Player::render(sf::RenderWindow &window) { window.draw(circleShape); }; void Player::render(sf::RenderWindow &window) { window.draw(circleShape); };
+3
View File
@@ -5,11 +5,14 @@
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/View.hpp> #include <SFML/Graphics/View.hpp>
#include <SFML/System/Vector2.hpp>
class Player { class Player {
public: public:
Player(float size, sf::Color color); Player(float size, sf::Color color);
sf::Vector2f position;
void update(double dt, sf::RenderWindow &window, const sf::View &view); void update(double dt, sf::RenderWindow &window, const sf::View &view);
void render(sf::RenderWindow &window); void render(sf::RenderWindow &window);
+12 -4
View File
@@ -4,10 +4,18 @@
#include <SFML/System/Vector2.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}); constexpr int dWidth = WIDTH * 2;
add({1.f, 1.f}, {1.f, HEIGHT - 1.f}); constexpr int dHeight = HEIGHT * 2;
add({WIDTH - 1.f, 1.f}, {WIDTH - 1.f, HEIGHT - 1.f});
add({1.f, HEIGHT - 1.f}, {WIDTH - 1.f, HEIGHT - 1.f}); constexpr sf::Vector2f topLeft = {-WIDTH, -HEIGHT};
constexpr sf::Vector2f topRight = {dWidth, -HEIGHT};
constexpr sf::Vector2f bottomLeft = {-WIDTH, dHeight};
constexpr sf::Vector2f bottomRight = {dWidth, dHeight};
add(topLeft, topRight);
add(topLeft, bottomLeft);
add(topRight, bottomRight);
add(bottomLeft, bottomRight);
add({1000.f, 500.f}, {1500.f, 500.f}); add({1000.f, 500.f}, {1500.f, 500.f});
add({500.f, 240.f}, {500.f, 760.f}); add({500.f, 240.f}, {500.f, 760.f});