mirror of
https://github.com/Floriansylvain/SFMLplayground.git
synced 2026-08-19 19:53:25 +02:00
feat: velocity tracking w/ rendering lines
This commit is contained in:
+25
-2
@@ -5,7 +5,10 @@
|
||||
#include "Constants.hpp"
|
||||
|
||||
Ball::Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel)
|
||||
: m_radius(radius), m_velocity(vel) {
|
||||
: m_radius(radius),
|
||||
m_velocity(vel),
|
||||
m_lastPosition(pos),
|
||||
m_pixelVelocity(vel) {
|
||||
m_shape.setRadius(radius);
|
||||
m_shape.setOrigin(sf::Vector2f(radius, radius));
|
||||
m_shape.setPosition(pos);
|
||||
@@ -13,17 +16,30 @@ Ball::Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel)
|
||||
}
|
||||
|
||||
void Ball::update(float dt) {
|
||||
if (m_atRest) return;
|
||||
m_velocity.y += Constants::GRAVITY * dt;
|
||||
m_shape.move(m_velocity * dt);
|
||||
|
||||
sf::Vector2f currentPosition = m_shape.getPosition();
|
||||
m_pixelVelocity = (currentPosition - m_lastPosition) / dt;
|
||||
m_lastPosition = currentPosition;
|
||||
|
||||
handleWallCollision();
|
||||
}
|
||||
|
||||
void Ball::draw(sf::RenderWindow& window) { window.draw(m_shape); }
|
||||
|
||||
void Ball::applyImpulse(const sf::Vector2f& impulse) { m_velocity += impulse; }
|
||||
void Ball::applyImpulse(const sf::Vector2f& impulse) {
|
||||
m_velocity += impulse;
|
||||
m_atRest = false;
|
||||
}
|
||||
|
||||
sf::Vector2f Ball::getPosition() const { return m_shape.getPosition(); }
|
||||
|
||||
sf::Vector2f Ball::getVelocity() const { return m_velocity; }
|
||||
|
||||
bool Ball::isAtRest() const { return m_atRest; }
|
||||
|
||||
void Ball::handleWallCollision() {
|
||||
sf::Vector2f pos = m_shape.getPosition();
|
||||
|
||||
@@ -51,6 +67,13 @@ void Ball::handleWallCollision() {
|
||||
if (pos.y + m_radius >= Constants::HEIGHT - 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 &&
|
||||
std::abs(m_pixelVelocity.y) < Constants::REST_PIXEL_VELOCITY) {
|
||||
m_velocity = {0.f, 0.f};
|
||||
m_atRest = true;
|
||||
}
|
||||
} else {
|
||||
m_atRest = false;
|
||||
}
|
||||
|
||||
m_shape.setPosition(pos);
|
||||
|
||||
Reference in New Issue
Block a user