Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

so Im making a game and I need a 'bar' to show how much time has passed in ms (dont worry about the time 'mechanics' I only work with the correct numbers). So lets say that the bar is 128px wide and that I want the timer (the time passed since start in ms) to be displayed within that bar properly, so that e.g. if the time passed is half way of the maximum, then the bar should be half full. Could anyone please tell me the formula because I cant remember/find it.

(btw. all variables needed are the curTime, maxTime and the progress of the bar/the max bar width)

share|improve this question
7  
This is elementary maths... – gronostaj Jan 7 at 23:08
2  
Also relevant: creating a circular loading bar – Anko Jan 7 at 23:49
1  
I know this is elementary math but at 11pm after a rough day I kinda didnt care. – Ivan Jerza Jan 8 at 7:33
up vote 10 down vote accepted

You do it with math.

private static const int maxBarWidth = 128;

//...

float ratio = (float)curTime / maxTime;
float barProgressInPixels = ratio * maxBarWidth;

Unless you're looking at figuring out how to render this information to the screen.

share|improve this answer
    
No this is what I was looking for, thanks :D – Ivan Jerza Jan 7 at 20:49
    
That's a ratio, though, which times 100 would be a percentage. – Cees Timmerman Jan 7 at 23:04
    
I didnt really bother with what i said, I just couldnt remember it lol – Ivan Jerza Jan 8 at 7:28
    
@CeesTimmerman While that is true, when creating a progress bar, you don't want to multiply it by 100 to get the numerical percentage. – Draco18s Jan 8 at 14:57
    
@CeesTimmerman Just as an FYI, I approved the edit, but I had originally used "percentage" as the variable name, not "ratio" so that the asker could not say, "Hey, where's the percentage?" – Draco18s Jan 8 at 15:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.