2D Health Bars for Units in 3D Environment

Started by
6 comments, last by occams_razor 10 years, 1 month ago

Hi guys,

I am making a 3D RTS game with Direct3D. Basically, I want to make 2D health bars hovering over my units that show how many hitpoints they have left after receiving wounds. I'm not sure what a good way to go about this would be because the thing is, the health bars have to always face the camera, no matter what angle they are at from the center of the viewport. I also want them to look as close to perfectly rectangular as possible, but I want them to look smaller if they are farther away.

I don't want to post code, so I'm just looking for a "general concept" type of answer.

Thanks

Advertisement

Search for "directx billboards" or "opengl billboards". The basic idea is to project screen aligned 2d geometry into the 3d world.

This satisfies my requirements. Thanks. Is there a way to mark your answer as the correct one?

Keep UI, gameplay, and display of the game world separate.


Allow game objects to have an interface. In one of the best interface naming conventions I've seen that reminds me of kittehs, it might be IHasHealthBar, or IHasMouseBubble, or IHasWhatever.

Your UI code is already going to need to query objects as your mouse moves over it. If the object implements IHasHealthBar, that likely means it provides a string and a few integers. The UI can do whatever it wants with those numbers. The UI might put a ring around the mouse cursor, or it might display a little bar above or below the object's bounding box, or it might do some really cool graphical effects that your artists imagine up.

But ultimately the health bar display will have no effect on the gameplay systems or on the game object other than that the object will have some functions called occasionally. The display and management of the bars is all stuff contained within the UI system.


This satisfies my requirements. Thanks. Is there a way to mark your answer as the correct one?

The upvotes usually does give people the idea or you can just select quote his statement.

Another approach is projecting the 3D points above the characters' heads onto the screen as 2D coordinates, and rendering the life bars at those points with the 2D commands of your engine.
This way the life bars remain constant in size (as opposed to the billboards that change in size relative to the camera). Since they are 2D graphics, they will naturally face the camera.

Just to clarify what Kryzon said: D3DXVec3Project(....). This converts a 3D point in space into a 2D point "on screen". Be careful, objects behind the camera will show up as being in front of it. You can avoid this by a simple polarity check by inversely transforming the 3D point by the camera's view matrix.

EDIT:

I forgot about D3DXVec3Dot(...). It can be used to determine if the object is in the same general "direction" as the view direction:


D3DXVECTOR3 objDir=ObjLoc-CameraLoc;
if (D3DXVec3Dot(&objDir,&viewDir)>0.0f){
//render the bar
......
}

Now, this doesn't tell you whether the bar is "on screen", it tells you it's angle is somewhere forward of the camera.

Search for "directx billboards" or "opengl billboards". The basic idea is to project screen aligned 2d geometry into the 3d world.

Ok guys, I think billboards are basically going to work for me. But I appreciate all of your answers, they were all good. I also don't mind the health bars getting smaller in the distance, so thats the reason I went with billboards. Thanks.

This topic is closed to new replies.

Advertisement