[C++] Name above other players

Started by
6 comments, last by Jason Z 11 years, 7 months ago
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
Advertisement
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!
Alternately, place a billboarded quad above each player in world space.
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.
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.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor


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;

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


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

Cheers!
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...

This topic is closed to new replies.

Advertisement