I'm using the cosine function in C++ to simulate flashing for a sprite in my game. The method looks like this:
(anything in the sf
namespace is part of the SFML graphics library)
void Player::update(const float& deltaTime)
{
mAccumulatedTime += deltaTime;
float opacity = abs(cosf(5*mAccumulatedTime)) * 255;
static int numFlashes = 0;
if (opacity == 255) {
cout << ++numFlashes << endl;
}
mSprite.setFillColor(sf::Color(255, 255, 255, opacity));
}
So every time opacity
is equal to 255 (basically the passing of one full period), numFlashes
should be incremented. The problem is, cos()
isn't perfect, meaning it doesn't exactly reach 1 and 0, so the if
condition is rarely met. If I use rough checking like if (opacity > 255*0.9999)
, then numFlashes
becomes really high, really fast.
Does anyone know a way to accurately check when a full period has passed? Or is that just not possible?
deltaTime
by 1,000,000 in the method-call, so NOW it's in seconds. \$\endgroup\$