mirror of
https://github.com/Floriansylvain/SFMLplayground.git
synced 2026-08-19 11:43:24 +02:00
feat: update Ball and PhysicalObject classes to include window size/resize
This commit is contained in:
+6
-6
@@ -16,7 +16,7 @@ Ball::Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel,
|
||||
m_shape.setFillColor(color);
|
||||
}
|
||||
|
||||
void Ball::update(float dt) {
|
||||
void Ball::update(float dt, const sf::Vector2f& windowSize) {
|
||||
if (m_atRest) return;
|
||||
m_velocity.y += Constants::GRAVITY * dt;
|
||||
m_shape.move(m_velocity * dt);
|
||||
@@ -25,7 +25,7 @@ void Ball::update(float dt) {
|
||||
m_pixelVelocity = (currentPosition - m_lastPosition) / dt;
|
||||
m_lastPosition = currentPosition;
|
||||
|
||||
handleWallCollision();
|
||||
handleWallCollision(windowSize);
|
||||
}
|
||||
|
||||
void Ball::draw(sf::RenderWindow& window) { window.draw(m_shape); }
|
||||
@@ -41,7 +41,7 @@ sf::Vector2f Ball::getVelocity() const { return m_velocity; }
|
||||
|
||||
bool Ball::isAtRest() const { return m_atRest; }
|
||||
|
||||
void Ball::handleWallCollision() {
|
||||
void Ball::handleWallCollision(const sf::Vector2f& windowSize) {
|
||||
sf::Vector2f pos = m_shape.getPosition();
|
||||
|
||||
auto handleAxis = [&](int axis, float min, float max, float& velocity,
|
||||
@@ -60,12 +60,12 @@ void Ball::handleWallCollision() {
|
||||
(axis == 0 ? pos.x : pos.y) = value;
|
||||
};
|
||||
|
||||
handleAxis(0, 0.f, Constants::WIDTH, m_velocity.x, m_radius,
|
||||
handleAxis(0, 0.f, windowSize.x, m_velocity.x, m_radius,
|
||||
Constants::RESTITUTION);
|
||||
handleAxis(1, 0.f, Constants::HEIGHT, m_velocity.y, m_radius,
|
||||
handleAxis(1, 0.f, windowSize.y, m_velocity.y, m_radius,
|
||||
Constants::RESTITUTION);
|
||||
|
||||
if (pos.y + m_radius >= Constants::HEIGHT - 1.0f) {
|
||||
if (pos.y + m_radius >= windowSize.y - 1.0f) {
|
||||
m_velocity.x *= Constants::FRICTION;
|
||||
if (std::abs(m_velocity.x) < 5.f) m_velocity.x = 0.f;
|
||||
if (std::abs(m_pixelVelocity.x) < Constants::REST_PIXEL_VELOCITY &&
|
||||
|
||||
+2
-2
@@ -12,13 +12,13 @@ class Ball : public PhysicalObject {
|
||||
sf::Vector2f m_lastPosition;
|
||||
sf::Vector2f m_pixelVelocity;
|
||||
|
||||
void handleWallCollision();
|
||||
void handleWallCollision(const sf::Vector2f& windowSize);
|
||||
|
||||
public:
|
||||
Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel,
|
||||
const sf::Color& color);
|
||||
|
||||
void update(float dt) override;
|
||||
void update(float dt, const sf::Vector2f& windowSize) override;
|
||||
void draw(sf::RenderWindow& window) override;
|
||||
void applyImpulse(const sf::Vector2f& impulse) override;
|
||||
void resolveCollision(Ball& other);
|
||||
|
||||
+1
-1
@@ -9,5 +9,5 @@ constexpr float FRICTION = 0.9f;
|
||||
constexpr float BALL_RADIUS = 20.f;
|
||||
constexpr float IMPULSE = 2500.f;
|
||||
constexpr float REST_PIXEL_VELOCITY = 2.0f;
|
||||
constexpr unsigned BALL_QUANTITY = 10;
|
||||
constexpr unsigned BALL_QUANTITY = 500;
|
||||
} // namespace Constants
|
||||
|
||||
+24
-2
@@ -21,6 +21,7 @@ Game::Game()
|
||||
m_window.create(sf::VideoMode({Constants::WIDTH, Constants::HEIGHT}),
|
||||
"SFML Playground");
|
||||
m_window.setVerticalSyncEnabled(true);
|
||||
m_windowSize = sf::Vector2f(Constants::WIDTH, Constants::HEIGHT);
|
||||
m_objects.clear();
|
||||
|
||||
auto balls = BallFactory::generateBalls();
|
||||
@@ -108,7 +109,11 @@ void Game::update() {
|
||||
if (dt > 0.1f) dt = 0.1f;
|
||||
|
||||
for (auto &object : m_objects) {
|
||||
object->update(dt);
|
||||
if (auto *ball = dynamic_cast<Ball *>(object.get())) {
|
||||
ball->update(dt, m_windowSize);
|
||||
} else {
|
||||
object->update(dt, m_windowSize);
|
||||
}
|
||||
}
|
||||
|
||||
auto grid = buildSpatialGrid();
|
||||
@@ -143,7 +148,24 @@ void Game::render() {
|
||||
|
||||
void Game::run() {
|
||||
while (m_window.isOpen()) {
|
||||
m_inputManager.processEvents(m_window);
|
||||
while (const std::optional event = m_window.pollEvent()) {
|
||||
if (event->is<sf::Event::Closed>()) {
|
||||
m_window.close();
|
||||
return;
|
||||
}
|
||||
if (const auto *resized = event->getIf<sf::Event::Resized>()) {
|
||||
sf::Vector2f position(0.f, 0.f);
|
||||
sf::Vector2f size(static_cast<float>(resized->size.x),
|
||||
static_cast<float>(resized->size.y));
|
||||
sf::FloatRect visibleArea(position, size);
|
||||
m_window.setView(sf::View(visibleArea));
|
||||
m_windowSize = size;
|
||||
}
|
||||
if (auto kP = event->getIf<sf::Event::KeyPressed>())
|
||||
processKeyPressed(*kP);
|
||||
if (auto mP = event->getIf<sf::Event::MouseButtonPressed>())
|
||||
processMousePressed(*mP);
|
||||
}
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ class Game {
|
||||
private:
|
||||
float m_timeScale = 1.0f;
|
||||
sf::RenderWindow m_window;
|
||||
sf::Vector2f m_windowSize;
|
||||
std::vector<std::unique_ptr<PhysicalObject>> m_objects;
|
||||
sf::Clock m_clock;
|
||||
InputManager m_inputManager;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
class PhysicalObject {
|
||||
public:
|
||||
virtual ~PhysicalObject() = default;
|
||||
virtual void update(float dt) = 0;
|
||||
virtual void update(float dt, const sf::Vector2f& windowSize) = 0;
|
||||
virtual void draw(sf::RenderWindow& window) = 0;
|
||||
virtual void applyImpulse(const sf::Vector2f& impulse) = 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user