Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!


2D Health Bars for Units in 3D Environment


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 occams_razor   Members   -  Reputation: 135

Like
1Likes
Like

Posted 17 February 2014 - 04:43 PM

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



Sponsor:

#2 mark ds   Members   -  Reputation: 1663

Like
6Likes
Like

Posted 17 February 2014 - 05:37 PM

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


Edited by mark ds, 17 February 2014 - 05:45 PM.


#3 occams_razor   Members   -  Reputation: 135

Like
0Likes
Like

Posted 17 February 2014 - 05:55 PM

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



#4 frob   Moderators   -  Reputation: 31232

Like
2Likes
Like

Posted 17 February 2014 - 05:59 PM

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.


#5 warnexus   Prime Members   -  Reputation: 1535

Like
1Likes
Like

Posted 17 February 2014 - 09:19 PM


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.



#6 Kryzon   Prime Members   -  Reputation: 3692

Like
3Likes
Like

Posted 18 February 2014 - 03:14 PM

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.

Edited by Kryzon, 19 February 2014 - 01:57 PM.


#7 Hawkblood   Members   -  Reputation: 734

Like
3Likes
Like

Posted 18 February 2014 - 03:31 PM

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.


Edited by Hawkblood, 18 February 2014 - 05:26 PM.


#8 occams_razor   Members   -  Reputation: 135

Like
2Likes
Like

Posted 18 February 2014 - 08:43 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS