Health Meter

hpmeterThis week I’m going to talk about the health meter. I fixed it so it works correctly and is completely functional this Monday night, but I had begun working on it a bit the week before but didn’t finish it. I also added a wave effect on it on Thursday. The health meter behaves like a bowl of water. For example, when the health meter is full it’s completely round and green, but when you have only half of your health the lower half of the health meter will be a green half circle, like how it looks in the above image. The wave effect I mentioned above works by having the liquid in the ‘bowl’ constantly vary slightly in height.

The health meter consists of two layers; a green background layer which in green shows the amount of health, and a gray metal front layer that is just for nice looks. The bowl of water effect is achieved by cutting the upper half of the sprite using setTextureRect(), which is a part of SFML.

When the health changes it transitions smoothly in a fashion that makes it feel a lot like a liquid. This is achieved by using something called linear interpolation, or lerp for short. Lerp can be used with all kinds of things to get smooth transitions. My implementation of lerp looks like this: (1-t) * start + t * end. Start is what the health meter currently displays, end is the player’s actual health, and t is how fast it will move each frame; which is the game’s delta time times the transition speed.

The health meters fills up at the start of the game and decreases a bit each time you lose health. The height of the health in the health meter constantly moves a few pixels up and down to enhance the feeling of it being a liquid. This is achieved by applying sin(current_time * speed) * amount to the health height.

I’m quite happy with the result. I think the liquid effect looks really nice. We have planned in the future to have the health distributed between the four sectors of the planet, with each sector having an equal amount of health. The health meter would then only be empty when all of the sectors are destroyed. For example, if one sector is completely destroyed but all others are completely okay, then ¾ of the health meter will be filled.

About Linus Bjernhagen

2015 Programming