# How can I model a “weapon overheating” mechanic?

I want to create a weapon overheating system very similar to the plasma rifle in Halo. You can watch a video of the plasma rifle firing.

What I want to do is to create a flexible logic that can be used for multiple weapons for different in-game feelings. Here is my proposed system

1. Create a bar that is split from 0 - 100.
2. Each bullet has a heat value as an integer.
3. Every bullet adds this value to the bar.
4. The bar has a cooldown set as some value per second.
5. If the bar goes over 100 with any bullet fire the bullet still shoots but then the weapon is deactivated for a period defined as it's "cooldown."

Now this system is very simple but visually it will be very linear, with a simple growth and decay. I wanted to try to create a more exponential system that would mean the bar would jump quickly to the middle then remain and hover near the top / hot bit of the bar for a long period to create a sense of anticipation just before it overheats.

What would be a good formula to achieve that result?

To have the gun heat up quickly but reach its overheat threshold slowly, you could take the gun's current temperature into account in the heat dissipation, rather than having a constant rate of heat dispersion. That's actually how it works in the real world: the rate of heat / thermal transfer is proportional to the difference in temperature. Wikipedia has a good article on Heat Transfer that covers this.

I strongly recommend a decimal value instead of int for tracking heat on your weapons, and to not have a strict cap so much as a threshold over which the weapon goes into cooldown to prevent damage from overheating.

A heat transfer rate equation might be helpful.

Rate = k•(T1 - T2)/m
Where k is the heat transfer coefficient of the gun's material.
Since you're not modelling a specific
T1 is temperature of the world around the gun
T2 is temperature of the gun
m is mass.


You can probably implement this for your game as

void cooldown(double timeElapsed) {
gunHeat -= timeElapsed * COOLING_RATE * gunHeat;
}


Where gunHeat is how much hotter than the surrounding air your gun is, aka your heat variable; COOLING_RATE is a constant value for the gun according to its materials and how good it is at keeping itself cool; and timeElapsed is the fraction of a second of the game world that has been simulated since cooldown() was last called. Then you just add a constant value to gunHeat whenever the gun fires.

An improved version could take into account the temperature of the environment around the player:

void cooldownBetter(double timeElapsed, double localTemperature) {
gunTemperature += timeElapsed * COOLING_RATE * (localTemperature - gunTemperature);
}


In either case, when the gun's heat/temperature is above a certain threshold, it stops shooting until the heat drops below a threshold. This could be the same threshold, so that shooting would effectively just slow down, or a lower heat/temperature so that you have to wait but then begin shooting again as fast as before.

You'll have to test and tweak COOLING_RATE to match what you want to go for for each gun.

• Yes I like trying to represent it like the real world a lot. I shall set specific cooling rates for each weapon and test the feel for each to create variety. – kinnth Jan 6 '14 at 23:11
• Kinnth, try not to forget that when it comes to games: if it looks like the real thing and it works, it works. Don't try to model everything after the real world, imo it's a waste. Sean Middleditch solution works great as well. – Sidar Jan 6 '14 at 23:43

Your weapon needs two values: the current heat value (int, probably) and a cooldown timer (float). Initialize both to 0.

When a shot is fired, add the bullet's heat value to the gun's. Cap it at 100. When this value reaches 100, set the cooldown value to your cooldown period time in seconds. Reduce this by your time delta every frame, clamped at a minimum to 0. Don't let the gun fire shots if cooldown is not 0.

In pseudocode:

Gun.fire(bullet):
this.cooldown = max(this.cooldown - game.time_delta, 0)

if this.cooldown == 0:
create_and_fire_bullet_instance(this.muzzle.transform, bullet)

this.heat = min(this.heat + bullet.heat, 100)
if this.heat == 100:
this.cooldown = 3 seconds

• if this.heat >= 100: this.cooldown = 100 ( capping it) – Sidar Jan 6 '14 at 20:48
• Bah I meant, heat = 100. Unless you just go with cooldown. – Sidar Jan 6 '14 at 20:55
• heat cannot be >=100 in the code I posted due to the call to min right above it. Or am I misunderstanding? – Sean Middleditch Jan 6 '14 at 22:24
• Eh, no you're right. Brain completely ignored that line. – Sidar Jan 6 '14 at 23:40

Try this out. Track shots and heat separately, and assign:

heat = round((shots * 100) / (shots + 100));


You'll need to tweak it to get the exact behavior you want, I'm sure, possibly with methods like:

heat = min(round((shots * 110) / (shots + 110)), 100);


but the basic curve should be pretty close to what you want.

For cooldown, decrement shots at some rate (I suggest trying 1 per 300 ms and tweaking from there) and recalculate heat as above.

• That doesn't seem to solve the cooldown portion at all. – Sean Middleditch Jan 6 '14 at 18:41
• @SeanMiddleditch: Okay, addressed. – chaos Jan 6 '14 at 20:58