Create health bar?


How can I create a health bar in JMonkey? Is there some documentation that can help?

I am working on a game, specifically, “Battleship”. I want to add health bars on each ship. For the main ship, “the player”, I want to have a fixed-place health bar in the top-left corner of the screen. For the enemy ships, I want to add a health bar that moves with each enemy ship.

While trying to figure out a solution to this idea, I found that everyone was talking about using “BillboardNode”, to create a health bar. On knowing this, I started to search on what a “Billboard Node” means and tried to find any documentation that can explain how to use it. But, with no luck. :frowning: I don’t know if I’m missing something, but I can’t find any documentation to help.

I did find a code that creates a health bar:

But when I try to use it in my code, it doesn’t work. So there must be something that I am missing!

This is the code that I tried to use in my game:
[java] BillboardControl billboard = new BillboardControl();
Geometry healthbar = new Geometry(“healthbar”, new Quad(4f, 0.2f));
Material mathb = mat.clone();
mathb.setColor(“Color”, ColorRGBA.Red);
healthbar.move(0, 7, 2);

I would appreciate any help. Thanks!

What is “it doesn’t work” mean? maybe post a screenshot

Haven’t used BillboardControl in a while, not sure how it handles nested children.

You can also use cam.getScreenCoordinates, which will allow you to put in onto the guiNode, or use a gui library (Nifty)

@wezrule. Sorry for not being clear. What I mean by “it doesn’t work” is that there is no visual effect in the program. When I run, no health bar appears next to the ship.

Excuse me, but what does cam.getScreenCoordinates do? When I searched in the tutorials, it had a “?” in the usage column of cam.getScreenCoordinates. (There was no explanation.)

@wezrule, it worked. :slight_smile: Thank you for your time!

You may want to take a look at
for bit more colorful health indicator.

I’m attaching health.getSpatial to billboard node created with

val Node billboard = new Node(“Billboard”);
val BillboardControl billboardControl = new BillboardControl();

which in turn is attached to main mode node with height coordinate translated based on model bounding box.

Thank you @abies!

I was able to create a health bar that moves with the player ship, but how can I make a health bar (for the player) that is fixed on the top-left corner of the screen?

attach it the guiNode

Thank you. It worked. :slight_smile: