mirror of
https://github.com/Floriansylvain/SFMLplayground.git
synced 2026-08-19 11:43:24 +02:00
feat: optimize color update logic in Ball class and improve vector normalization
This commit is contained in:
+20
-11
@@ -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<std::uint8_t>(0 + scaledT * 128);
|
||||
targetColor.g = static_cast<std::uint8_t>(0);
|
||||
targetColor.b = static_cast<std::uint8_t>(255);
|
||||
} else if (t < 0.66f) {
|
||||
float scaledT = (t - 0.33f) * 3.0f;
|
||||
targetColor.r = static_cast<std::uint8_t>(128 + scaledT * 127);
|
||||
targetColor.r = static_cast<std::uint8_t>(255);
|
||||
targetColor.g = static_cast<std::uint8_t>(0 + scaledT * 165);
|
||||
targetColor.b = static_cast<std::uint8_t>(255 - scaledT * 255);
|
||||
} else {
|
||||
targetColor.b = static_cast<std::uint8_t>(0);
|
||||
} else if (t < 0.66f) { // Orange to yellow
|
||||
float scaledT = (t - 0.33f) * 3.0f;
|
||||
targetColor.r = static_cast<std::uint8_t>(255);
|
||||
targetColor.g = static_cast<std::uint8_t>(165 + scaledT * 90);
|
||||
targetColor.b = static_cast<std::uint8_t>(0);
|
||||
} else { // Yellow to white
|
||||
float scaledT = (t - 0.66f) * 3.0f;
|
||||
targetColor.r = static_cast<std::uint8_t>(255);
|
||||
targetColor.g = static_cast<std::uint8_t>(165 - scaledT * 165);
|
||||
targetColor.b = static_cast<std::uint8_t>(0);
|
||||
targetColor.g = static_cast<std::uint8_t>(255);
|
||||
targetColor.b = static_cast<std::uint8_t>(0 + scaledT * 255);
|
||||
}
|
||||
|
||||
sf::Color currentColor = m_shape.getFillColor();
|
||||
|
||||
+10
-5
@@ -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);
|
||||
|
||||
+4
-3
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user