View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# [C++] Name above other players

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.

7 replies to this topic

### #1roby65  Members

Posted 29 August 2012 - 06:34 AM

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

### #2kauna  Members

Posted 29 August 2012 - 06:40 AM

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!

### #3SiCrane  Moderators

Posted 29 August 2012 - 06:51 AM

Alternately, place a billboarded quad above each player in world space.

### #4Lightness1024  Members

Posted 29 August 2012 - 07:19 AM

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.

### #5CC Ricers  Members

Posted 29 August 2012 - 08:25 AM

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

### #6roby65  Members

Posted 29 August 2012 - 02:06 PM

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;


### #7kauna  Members

Posted 29 August 2012 - 06:30 PM

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

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

Cheers!

### #8Jason Z  Members

Posted 30 August 2012 - 07:24 AM

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

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

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.