mirror of
https://github.com/Floriansylvain/whateversfml.git
synced 2026-08-19 11:43:22 +02:00
feat: raycasting part 3
This commit is contained in:
+10
-10
@@ -111,25 +111,25 @@ void Game::update(double dt) {
|
||||
player.update(dt, window, worldView);
|
||||
|
||||
// INIT/UPDATE RAYS ------------------------------------------
|
||||
auto mousePos = sf::Mouse::getPosition(window);
|
||||
auto mousePosWorld = window.mapPixelToCoords(mousePos, worldView);
|
||||
|
||||
sf::Vector2 playerPosition = player.position;
|
||||
rayVertices.clear();
|
||||
hitPoints.clear();
|
||||
|
||||
for (sf::Vector2 targetPoint : walls.getPoints()) {
|
||||
hitPoints.emplace_back(updateRay(mousePosWorld, targetPoint, 0.f));
|
||||
hitPoints.emplace_back(updateRay(mousePosWorld, targetPoint, 0.001f));
|
||||
hitPoints.emplace_back(updateRay(mousePosWorld, targetPoint, -0.001f));
|
||||
hitPoints.emplace_back(updateRay(playerPosition, targetPoint, 0.f));
|
||||
hitPoints.emplace_back(updateRay(playerPosition, targetPoint, 0.001f));
|
||||
hitPoints.emplace_back(updateRay(playerPosition, targetPoint, -0.001f));
|
||||
}
|
||||
std::sort(hitPoints.begin(), hitPoints.end(),
|
||||
[mousePosWorld](sf::Vector2f a, sf::Vector2f b) -> bool {
|
||||
auto angleA = atan2(mousePosWorld.y - a.y, mousePosWorld.x - a.x);
|
||||
auto angleB = atan2(mousePosWorld.y - b.y, mousePosWorld.x - b.x);
|
||||
[playerPosition](sf::Vector2f a, sf::Vector2f b) -> bool {
|
||||
auto angleA =
|
||||
atan2(playerPosition.y - a.y, playerPosition.x - a.x);
|
||||
auto angleB =
|
||||
atan2(playerPosition.y - b.y, playerPosition.x - b.x);
|
||||
return angleA < angleB;
|
||||
});
|
||||
|
||||
rayVertices.append({mousePosWorld, lightColor});
|
||||
rayVertices.append({playerPosition, lightColor});
|
||||
for (auto hitPoint : hitPoints) {
|
||||
rayVertices.append({hitPoint, lightColor});
|
||||
}
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
#include "Player.hpp"
|
||||
#include "Walls.hpp"
|
||||
|
||||
constexpr bool VSYNC = false;
|
||||
constexpr bool VSYNC = true;
|
||||
constexpr int WIDTH = 1920;
|
||||
constexpr int HEIGHT = 1080;
|
||||
constexpr int DEBUG_REFRESH_RATE_IN_MS = 250;
|
||||
|
||||
+7
-2
@@ -1,4 +1,5 @@
|
||||
#include "Player.hpp"
|
||||
#include "Game.hpp"
|
||||
#include <SFML/Graphics/CircleShape.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 mousePosWorld = window.mapPixelToCoords(mousePos, view);
|
||||
|
||||
circleShape.setPosition(
|
||||
{mousePosWorld.x - shapeRadius, mousePosWorld.y - shapeRadius});
|
||||
if ((mousePosWorld.x > WIDTH || mousePosWorld.x < 0) ||
|
||||
(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); };
|
||||
|
||||
@@ -5,11 +5,14 @@
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/Graphics/View.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
class Player {
|
||||
public:
|
||||
Player(float size, sf::Color color);
|
||||
|
||||
sf::Vector2f position;
|
||||
|
||||
void update(double dt, sf::RenderWindow &window, const sf::View &view);
|
||||
void render(sf::RenderWindow &window);
|
||||
|
||||
|
||||
+12
-4
@@ -4,10 +4,18 @@
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
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});
|
||||
constexpr int dWidth = WIDTH * 2;
|
||||
constexpr int dHeight = HEIGHT * 2;
|
||||
|
||||
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({500.f, 240.f}, {500.f, 760.f});
|
||||
|
||||
Reference in New Issue
Block a user