From 77b7631f63fcd90c231041faa21215558ce0f13b Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Sun, 11 May 2025 07:02:05 +0200 Subject: [PATCH] feat: optimize color update logic in Ball class and improve vector normalization --- src/Ball.cpp | 31 ++++++++++++++++++++----------- src/Ball.hpp | 15 ++++++++++----- src/VectorMath.cpp | 7 ++++--- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/Ball.cpp b/src/Ball.cpp index 67ca554..783c28b 100644 --- a/src/Ball.cpp +++ b/src/Ball.cpp @@ -31,6 +31,15 @@ void Ball::update(float dt, const sf::Vector2f& windowSize) { } void Ball::updateColor() { + float currentSpeed = + std::sqrt(m_velocity.x * m_velocity.x + m_velocity.y * m_velocity.y); + + static float lastSpeed = 0.0f; + if (std::abs(currentSpeed - lastSpeed) < 10.0f) { + return; + } + lastSpeed = currentSpeed; + float speed = std::sqrt(m_velocity.x * m_velocity.x + m_velocity.y * m_velocity.y); @@ -39,21 +48,21 @@ void Ball::updateColor() { sf::Color targetColor; - if (t < 0.33f) { + if (t < 0.33f) { // Red to orange float scaledT = t * 3.0f; - targetColor.r = static_cast(0 + scaledT * 128); - targetColor.g = static_cast(0); - targetColor.b = static_cast(255); - } else if (t < 0.66f) { - float scaledT = (t - 0.33f) * 3.0f; - targetColor.r = static_cast(128 + scaledT * 127); + targetColor.r = static_cast(255); targetColor.g = static_cast(0 + scaledT * 165); - targetColor.b = static_cast(255 - scaledT * 255); - } else { + targetColor.b = static_cast(0); + } else if (t < 0.66f) { // Orange to yellow + float scaledT = (t - 0.33f) * 3.0f; + targetColor.r = static_cast(255); + targetColor.g = static_cast(165 + scaledT * 90); + targetColor.b = static_cast(0); + } else { // Yellow to white float scaledT = (t - 0.66f) * 3.0f; targetColor.r = static_cast(255); - targetColor.g = static_cast(165 - scaledT * 165); - targetColor.b = static_cast(0); + targetColor.g = static_cast(255); + targetColor.b = static_cast(0 + scaledT * 255); } sf::Color currentColor = m_shape.getFillColor(); diff --git a/src/Ball.hpp b/src/Ball.hpp index 634b83e..85de294 100644 --- a/src/Ball.hpp +++ b/src/Ball.hpp @@ -6,18 +6,23 @@ class Ball : public PhysicalObject { private: - sf::CircleShape m_shape; + // Position and physics data sf::Vector2f m_velocity; - float m_radius; - bool m_atRest = false; sf::Vector2f m_lastPosition; sf::Vector2f m_pixelVelocity; + float m_radius; + + // Display data + sf::CircleShape m_shape; sf::Color m_baseColor; + + // State flags + bool m_atRest = false; + mutable std::mutex m_mutex; + void handleWallCollision(const sf::Vector2f& windowSize); void updateColor(); - mutable std::mutex m_mutex; - public: Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel, const sf::Color& color); diff --git a/src/VectorMath.cpp b/src/VectorMath.cpp index 748c713..9e08c08 100644 --- a/src/VectorMath.cpp +++ b/src/VectorMath.cpp @@ -8,9 +8,10 @@ float length(const sf::Vector2f& vector) { } sf::Vector2f normalize(const sf::Vector2f& vector) { - float len = length(vector); - if (len > 0.0001f) { - return vector / len; + float squaredLen = vector.x * vector.x + vector.y * vector.y; + if (squaredLen > 0.0001f) { + float invLen = 1.0f / std::sqrt(squaredLen); + return sf::Vector2f(vector.x * invLen, vector.y * invLen); } return vector; }