All right, I am trying to make a working healthbar for my game done in direct x 9 and for some reason nothing I do works. Basically, when I run the game and the main character collides with an enemy, the red bar for his health just travels towards the right instead of depleting to the left.

When I took a closer look I noticed that the life bars are set into the vector3D. Meaning that the health bar travels in the positive x direction. What I need is to know the steps to make a working health bar. Below are the code snippets that I have in order top create a healthbar.

/////////////////////////////////////
//Declaring the variables          //
////////////////////////////////////
IDirect3DTexture9*		 m_LifeBarTexture_Layer_1;
IDirect3DTexture9*		 m_LifeBarTexture_Layer_2;
IDirect3DTexture9*		 m_LifeBarTexture_Layer_3;
D3DXIMAGE_INFO			 m_LifeBarImageInfo_Layer_1;
D3DXIMAGE_INFO			 m_LifeBarImageInfo_Layer_2;
D3DXIMAGE_INFO			 m_LifeBarImageInfo_Layer_3;

//////////////////////////////////////
//Initializing the textures and the //
//Info                              //
/////////////////////////////////////
//Hero's Life Bar
D3DXCreateTextureFromFileEx(m_pD3DDevice,"LifeBar_Layer1_White.png",0,0,0,0,
		D3DFMT_UNKNOWN,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,
		D3DCOLOR_XRGB(255,255,255),&m_LifeBarImageInfo_Layer_1,0,&m_LifeBarTexture_Layer_1);

D3DXCreateTextureFromFileEx(m_pD3DDevice,"LifeBar_Layer2_Gray.png",0,0,0,0,
		D3DFMT_UNKNOWN,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,
		D3DCOLOR_XRGB(194,194,194),&m_LifeBarImageInfo_Layer_2,0,&m_LifeBarTexture_Layer_2);

D3DXCreateTextureFromFileEx(m_pD3DDevice,"LifeBar_Layer3_Red.png",0,0,0,0,
		D3DFMT_UNKNOWN,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,
		D3DCOLOR_XRGB(255,0,0),&m_LifeBarImageInfo_Layer_3,0,&m_LifeBarTexture_Layer_3);

/////////////////////////////////////////////////////////////////
//Rendering the Healthbars
////////////////////////////////////////////////////////////////
//Life Bar - Layer 1
D3DXMatrixIdentity(&transMat);
D3DXMatrixIdentity(&rotMat);
D3DXMatrixIdentity(&scaleMat);
D3DXMatrixIdentity(&worldMat);
D3DXMatrixScaling(&scaleMat,0.5f,0.5f,0.0f);//scale code
D3DXMatrixTranslation(&transMat,200.0f,30.0f,0.0f);
D3DXMatrixRotationZ(&rotMat,D3DXToRadian(0.0f));//Rotation code
D3DXMatrixMultiply(&transMat,&scaleMat,&transMat);
D3DXMatrixMultiply(&worldMat,&rotMat,&transMat);

m_pD3DSprite->SetTransform(&worldMat);
		m_pD3DSprite->Draw(m_LifeBarTexture_Layer_1,0,&D3DXVECTOR3(m_LifeBarImageInfo_Layer_1.Width,
			m_LifeBarImageInfo_Layer_1.Height,0.0f),0,D3DCOLOR_ARGB(255,255,255,255));

		//Life Bar - Layer 2
		D3DXMatrixIdentity(&transMat);
		D3DXMatrixIdentity(&rotMat);
		D3DXMatrixIdentity(&scaleMat);
		D3DXMatrixIdentity(&worldMat);
		D3DXMatrixScaling(&scaleMat,0.5f,0.5f,0.0f);//scale code
		D3DXMatrixTranslation(&transMat,200.0f,30.0f,0.0f);
		D3DXMatrixRotationZ(&rotMat,D3DXToRadian(0.0f));//Rotation code
		D3DXMatrixMultiply(&transMat,&scaleMat,&transMat);
		D3DXMatrixMultiply(&worldMat,&rotMat,&transMat);
		m_pD3DSprite->SetTransform(&worldMat);
		m_pD3DSprite->Draw(m_LifeBarTexture_Layer_2,0,&D3DXVECTOR3(m_LifeBarImageInfo_Layer_2.Width,
			m_LifeBarImageInfo_Layer_2.Height,0.0f),0,D3DCOLOR_ARGB(255,194,194,194));

		//Life Bar - Layer 3
		D3DXMatrixIdentity(&transMat);
		D3DXMatrixIdentity(&rotMat);
		D3DXMatrixIdentity(&scaleMat);
		D3DXMatrixIdentity(&worldMat);
		D3DXMatrixScaling(&scaleMat,0.5f,0.5f,0.0f);//scale code
		D3DXMatrixTranslation(&transMat,200.0f,30.0f,0.0f);
		D3DXMatrixRotationZ(&rotMat,D3DXToRadian(0.0f));//Rotation code
		D3DXMatrixMultiply(&transMat,&scaleMat,&transMat);
		D3DXMatrixMultiply(&worldMat,&rotMat,&transMat);
		m_pD3DSprite->SetTransform(&worldMat);

/* This is where the problem lies */
		m_pD3DSprite->Draw(m_LifeBarTexture_Layer_3,0,&D3DXVECTOR3((int)m_LifeBarImageInfo_Layer_3.Width * (float)(LifeBar_Hero/100.0f),
			m_LifeBarImageInfo_Layer_3.Height,0.0f),0,D3DCOLOR_ARGB(255,255,0,0));
/*****************************************************************************/

/////////////////////////////////////////////
//Updating the hero's collision with the enemies
//////////////////////////////////////////////
for(int i = 0; i < 8; i++)
	{
		if((m_MainPosition.z > PosEnemy[i].z - 5.0f)&&(m_MainPosition.z < PosEnemy[i].z + 5.0f))
		{
			if((m_MainPosition.x > PosEnemy[i].x - 5.0f)&&(m_MainPosition.x < PosEnemy[i].x + 5.0f))
			{
				LifeBar_Hero += 1.0f;
			}
		}
	}

Few ways you can do it.

1. Your going to want a frame. Which you'll draw like a normal texture. Unless your going with a really thin one or do some really cool animations an interesting frame helps the aesthetic.

2. Assuming you have just a straight bar

class HealthBar
{

}

Few ways you can do it.

1. Your going to want a frame. Which you'll draw like a normal texture. Unless your going with a really thin one or do some really cool animations an interesting frame helps the aesthetic.

2. Assuming you have just a straight bar this code should work though you'll have to modify it to fit how you ship the data over to the gpu.


BLEH stupid laptop ruined my post and can't recover so i'll give you the dime version of what i did before

class HealthBar
{
 protected:
 texture2D* m_frame;
 texture2D* m_healthbar;

 RECT m_frame_quad;
 RECT m_healthbar_quad;
 RECT m_max_healthbar_quad;

/*These are pointers that should be health by a character and you can attatch them to these. That means you don't have to make calls to actually update the values within the healthbar. Trick to game design always update ui after characters though 1 frame of ui lag wouldn't really be noticeable it's nice not have built-in lag*/

unsigned int m_prev_value;
unsigned int* m_value;
unsigned int* m_max_value;
public:
//This is what your interested in
void Update();

}

void HealthBar::Update()
{
 if(m_prev_value != *m_value)
{
float percentage = *m_value/(*m_max_value);
float new_width = m_maxhealthbar_data.width*(1-percentage);

//Use this code if you want it reduce from the left
 m_healthbar_data.x = m_maxhealthbar_data.x + new_width;
m_healthbar_data.width = m_maxhealthbar_data.width - new_width;
//Code for descent from right is simply: m_healthbar_data.width = m_maxhealthbar_data.width - new_width;

/*You now have the proper x,y position of the healthbar and the width to render the frame and healthbar and have them work how you want
How you update the quad depends on the shader solution you use. If you want to create a world matrix from an instanced matrix, geometry shader, or you might even just ship them over one by one. Just remember the rect has data x,y,width,height or you could even do x_l,x_r,y_top,y_bottom or whatever just make the code work with that and ship the data over*/



}
m_prev_value = *m_value;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.