Archived

This topic is now archived and is closed to further replies.

Can anybody see what's wrong with my math here?

This topic is 5378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm blitting a bitmap with an alpha value. The alpha value for this bitmap is not per-pixel, and it changes over time as follows: * Between 0 and 2500 milliseconds, the alpha value gradually rises from 100 to 200. * Between 2501 and 3000 milliseconds, the alpha value decreases gradually from 200 to 0. So basically, it's an animation from semitransparent, to almost opaque, to completely transparent. To accomplish this, each frame I come up with a new alpha value using the following function:
    
int GetAlpha()
{

 int lapsed = GetTickCount() - anim_start;
 int result;

 if(lapsed <= 2500)
 {

  int x = (lapsed / 2500) * 100;
  result = x + 100;

 }
 else
 {

  lapsed -= 2500;
  int x = (lapsed / 500) * 200;
  result = 200 - x;

 } 
 
 return result;

}
    
But what ends up happening when I run this program is it appears as though the alpha value remains at 100 (or some similar number) for 2.5 seconds, and for the last .5 seconds it's at 200 or so. There's no gradual change at all. Looking at my function, is there something wrong with my math that's causing this to happen? [edited by - utwo on March 22, 2003 10:18:20 PM]

Share this post


Link to post
Share on other sites
The problem is in these two lines:


int x = (lapsed / 2500) * 100;
int x = (lapsed / 500) * 200;


They should be:


int x = (int) (lapsed / 2500.0 * 100.0);
int x = (int) (lapsed / 500.0 * 200.0);



The problem is dividing an integer by an integer in C++ gives you an integer, not a floating point value. So for the first 2499 milliseconds, lapsed / 2500 is 0. Then it jumps to 1 for the last millisecond. Similarly for the first 499 milliseconds of the second part of the animation lapsed / 500 is returning 0, then it jumps to 1 for the last millisecond. So instead of a gradual transition you get constant values with some brief spikes.

Share this post


Link to post
Share on other sites