Jump to content

draw 2D health bars


Rick
 Share

Recommended Posts

I'm drawing health bars for my units by getting the model AABB and position in 3D space. I then use the y1 value of the AABB in replace of the y value in the position to make a new Vec3 object which I then pass to CameraUnproject. So in my case this should get me a2D point directly above the models head. I then move that point some pixels to the left and that's the X for my rectangle. This works pretty well but I would expect the rectangle to always look pretty much center over the head all the time. In the 2 pictures I've rotated on the left and right side and you can see the health bar sort of slides to one side or the other.

 

HealthBar1.png

 

HealthBar2.png

 

for k1,v1 in pairs(v) do
		aabb = GetEntityAABB(v1.model)
		pos = v1.model:GetPosition(1)

		-- replace the y value with the height of the bounding box
		pos.y = aabb.y1 + .25
		result = CameraUnproject(camera, pos)

		SetColor(COLOR_RED)
		DrawRect(result.x - 25, result.y, 50, 5)

		SetColor(COLOR_GREEN)
		DrawRect(result.x - 25, result.y, 50, 5)
end

Link to comment
Share on other sites

//Maybe get the width of the AABB bounding box showing on-screen and adjust pixels appropriately?

 

Looking like its too long.. Rick, maybe you sould just take the origin of model (if it is in center in 3d), move up (in 3d) and move (in 2d) panel half-of-its-width left?

Working on LeaFAQ :)

Link to comment
Share on other sites

Model origin is the middle by the feet. I wanted to use the height of the BB because each model has a different height, I just didn't want to hardcode that per model. And I am doing 1/2 the panel width to the left. So basically I'm doing that you are saying. The only difference being I'm using BB top y value instead of hardcoding. That shouldn't affect the x placement of the 2D image though? This isn't a massive issue, just something that would be nice to always have it look perfect. I've moved on, but might come back later.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...