# [C++] Name above other players

This topic is 2294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Guys,

i have a multiplayer game where i need to draw player names above their heads.
I have Camera position/rotation and player position, and i need to calculate where to draw the 2d player name on the string.
Can someone suggest me what's the best way to do it?

Roby

##### Share on other sites
Hi,

If you know the world position of the player head, you may use your view-projection matrix to transform the position to the screen space.

Cheers!

##### Share on other sites
Alternately, place a billboarded quad above each player in world space.

##### Share on other sites
yes, why not with a little upscaling according to linear Z.

otherwise the position of the point in 2D is:
(you can use glm library)

vec4 proj = mul(vec4(vec3(worldPosition), 1), viewProjectionMatrix);
proj *= (1. / 2 * proj.w);
proj += vec4(0.5, 0.5, 0, 0);
vec2 screenPos = vec2(proj) * resolution; // resolution is a vec2.

##### Share on other sites
This might be a cool reason to put an additional bone above the head of the players' character models. Extra bones that aren't used for deformation provide an easy marker in which to locate where to render other stuff like this.

##### Share on other sites

yes, why not with a little upscaling according to linear Z.

otherwise the position of the point in 2D is:
(you can use glm library)

vec4 proj = mul(vec4(vec3(worldPosition), 1), viewProjectionMatrix);
proj *= (1. / 2 * proj.w);
proj += vec4(0.5, 0.5, 0, 0);
vec2 screenPos = vec2(proj) * resolution; // resolution is a vec2.

I liked your code, but it's not working, maybe i made something wrong?

 D3DXVECTOR3 worldPosition(100,100,500); D3DMATRIX viewProjectionMatrix=lastProjection; D3DXVECTOR4 proj; D3DXVec4Transform(&proj,&D3DXVECTOR4(worldPosition,1),&(D3DXMATRIX(viewProjectionMatrix))); proj *= (1. / 2 * proj.w); proj += D3DXVECTOR4(0.5, 0.5, 0, 0); D3DXVECTOR2 screenPos = D3DXVECTOR2(proj.x,proj.y) * 1680 * 1050; 

##### Share on other sites

D3DXVECTOR2 screenPos = D3DXVECTOR2(proj.x,proj.y) * 1680 * 1050;

Maybe you meant this : D3DXVECTOR2 screenPos = D3DXVECTOR2(proj.x* 1680,proj.y* 1050) ;

Cheers!

##### Share on other sites
The general idea should just be to take the player position, add an offset to it to generate the location in object space that you want the text to appear (i.e. add vec3(0,1.5,0) or whatever size is necessary). Then project that point using your model/view/projection matrices, giving a clip space position. Next you normalize by dividing the vector by its w value, and scale the position according to the resolution of the display - which gives the final pixel location that you want to put your text on to.

I'm pretty sure the sample code above does this already, but I just wanted to provide a textual description of the process...

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633674
• Total Posts
3013275
×