MrMusAddict from the /GameDev Reddit posted an excellent little piece of C# code (and the formula) for adjusting flashlight intensity based upon energy left in the battery. Having the light slowly dim or dissipate over time adds an extra element of stress to any horror based game… we just thought it worth sharing.
So, I was wanting to determine what the best course of action was for adjusting the brightness for the flashlight in my horror game. I oftern see almost all horror games with flashlights keep the light at full brightness until the battery runs out, at which point it “clicks” and turns off. This is different from what I know to be true; the light is variable depending its battery’s charge.
I headed over to /r/flashlight for some clarification on what the relationship is between a flashlight’s brightness and its charge, as I had a feeling it was not linear (i.e. 50% charge = 50% brightness). What I found there was that incandescent (normal) flashlights’ brightness is not in fact linear in relation to the charge of the battery, and is instead has a 4th power relation to charge. This means that 0% charge = 0% brightness, 100% charge = 100% brightness, but 50% charge ~ 93% brightness. Likewise, the light does not hit 50% brightness till the charge is around 15%.
This led me to construct this formula which outputs the final brightness at the ratio shown on this graph (red line is charge, green line is brightness). The formula is comprised of the following variables;
- B_Final = Final Brightness (output)
- C_Current = Current charge of battery
- C_Max = Your max value for the battery’s charge (I use 100f for 100%, but if “full charge” for you for some reason is 3864, use that)
- C_Min = Your min value for the battery’s charge (I use 0f for 0%, which is most common)
- B_C_Max = The brightness of the flashlight when the battery is as full charge (I use 0.9f, since 1f is 100% and a little too bright for my needs)
- B_C_Min = The brightness of the flashlight when the battery is as C_Min (again, I use 0f for 0% brightness, but if you want it to be no less than 20% you can use 0.2f)
In code (C#) it looks like this:
//in your update method float chargeCurrent = yourgame.YourBatteryVariable; float chargeMax = 100f; float chargeMin = 0f; float brightMax = 0.9f //1f is full brightness float brightMin = 0f; float brightFinal = (brightMin-brightMax)*Math.Pow((chargeCurrent-chargeMax)/(chargeMax-chargeMin), 4)+brightMax; myLight.Brightness = brightFinal;
After all this, your flashlight will stay above 90% brightness for over 50% of the charge (as it normally would), and the light will dim quickly once it gets below 20% charge. This, to me, would add a lot more planning and fright for any game that requires light to keep your guy safe.
Edit: If you intend the MIN values to always be 0 (0 charge = 0 brightness), then you can eliminate the MIN variables, meaning the equation can actually be simplified to: