AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Origin to Hud (https://forums.alliedmods.net/showthread.php?t=341306)

EFFx 01-16-2023 07:40

Origin to Hud
 
Was reading some threads about useful stocks in a russian forum and discovered this from 4 years ago: https://dev-cs.ru/threads/222/page-5#post-60600

Basically, it just does a lot of math to convert the user origin to hud position. Only thing I did was add the floatclamp so it sticks correctly to the user's screen even if the teammate is not in sight. Without it, the hud just goes to random places in the screen when the teammate is not visible.

PHP Code:

#include <amxmodx>
#include <fakemeta>
#include <xs>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"


public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_clcmd("say test""test")
}

public 
test(id)
{
    
set_task(0.1"updateHud"id, .flags "b")
}

public 
updateHud(id)
{    
    new 
iPlayers[32], iNumFloat:fOrigin[3], Float:fHudPos[2]
    
get_players(iPlayersiNum"ac")
    for(new 
iPlayeri;iNum;i++)
    {
        
iPlayer iPlayers[i]
        if(
get_user_team(iPlayer) == get_user_team(id))
        {
            
pev(iPlayerpev_originfOrigin)
            
WorldToScreen(idfOriginfHudPos)

            
set_hudmessage(255255255floatclamp(fHudPos[0], 0.01.0), floatclamp(fHudPos[1], 0.01.0), 00.10.10.00.0)
            
show_hudmessage(id"Teammate: %n"iPlayer)
        }
    }
}

WorldToScreenidFloat:point[3], Float:screen[2] )
{
    new 
bool:behind;
    new 
Float:w;
    new 
Float:worldToScreen[4][4];
    new 
Float:worldviewMatrix[4][4];
    new 
Float:projectionMatrix[4][4];
    
    new 
Float:viewangles[3];
    new 
Float:viewofs[3];
    new 
Float:vieworigin[3];
    
    
pev(idpev_originvieworigin);
    
pev(idpev_v_angleviewangles);
    
pev(idpev_view_ofsviewofs);
    
xs_vec_add(vieworiginviewofsvieworigin);
    
R_SetupModelviewMatrix(worldviewMatrixviewanglesvieworigin);
    
R_SetupProjectionMatrixprojectionMatrix );
    
Matrix4x4_ConcatworldToScreenprojectionMatrixworldviewMatrix );
    
    
screen[0] = worldToScreen[0][0] * point[0] + worldToScreen[0][1] * point[1] + worldToScreen[0][2] * point[2] + worldToScreen[0][3];
    
screen[1] = worldToScreen[1][0] * point[0] + worldToScreen[1][1] * point[1] + worldToScreen[1][2] * point[2] + worldToScreen[1][3];
    
worldToScreen[3][0] * point[0] + worldToScreen[3][1] * point[1] + worldToScreen[3][2] * point[2] + worldToScreen[3][3];
    
    if( 
0.001 )
    {
        
screen[0] *= 100000;
        
screen[1] *= 100000;
        
behind true;
    }
    else
    {
        new 
Float:invw 1.0 w;
        
screen[0] *= invw;
        
screen[1] *= invw;
        
behind false;
    }
    
screen[0] =  0.5 screen[0] * 1.0;
    
screen[1] = -0.5 screen[1] * 1.0;
    
screen[0] += 0.5 1.0;
    
screen[1] += 0.5 1.0;
    
    return 
behind;
}

Matrix4x4_CreateModelviewFloat:out[4][4] )
{
    
out[0][0] = out[1][1] = out[2][2] = 0.0;
    
out[3][0] = out[0][3] = 0.0;
    
out[3][1] = out[1][3] = 0.0;
    
out[3][2] = out[2][3] = 0.0;
    
out[3][3] = 1.0;
    
out[1][0] = out[0][2] = out[2][1] = 0.0;
    
out[2][0] = out[0][1] = -1.0;
    
out[1][2] = 1.0;
}

Matrix4x4_Copy(Float:out[4][4], Float:in[4][4])
{
    for(new 
0;4;i++)
    {
        for(new 
4j++)
        {
            
out[i][j] = in[i][j];  
        }
    }
}

Matrix4x4_CreateRotateFloat:out[4][4], FloatangleFloatxFloatyFloat)
{
    new 
FloatlenFloat:cFloat:s;
    
    
len z;
    if( 
len != 0.0 len 1.0 floatsqrootlen );
    
*= len;
    
*= len;
    
*= len;
    
    
angle *= (-M_PI 180.0);
    
floatsin(angle);
    
floatcos(angle);
    
    
out[0][0]=* (x);
    
out[0][1]=* (c) + s;
    
out[0][2]=* (c) - s;
    
out[0][3]=0.0;
    
out[1][0]=* (c) - s;
    
out[1][1]=* (y);
    
out[1][2]=* (c) + s;
    
out[1][3]=0.0;
    
out[2][0]=* (c) + s;
    
out[2][1]=* (c) - s;
    
out[2][2]=* (z);
    
out[2][3]=0.0;
    
out[3][0]=0.0;
    
out[3][1]=0.0;
    
out[3][2]=0.0;
    
out[3][3]=1.0;
}

Matrix4x4_ConcatFloat:out[4][4], const Float:in1[4][4], const Float:in2[4][4] )
{
    
out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0] + in1[0][3] * in2[3][0];
    
out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1] + in1[0][3] * in2[3][1];
    
out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2] + in1[0][3] * in2[3][2];
    
out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + in1[0][2] * in2[2][3] + in1[0][3] * in2[3][3];
    
out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0] + in1[1][3] * in2[3][0];
    
out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1] + in1[1][3] * in2[3][1];
    
out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2] + in1[1][3] * in2[3][2];
    
out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + in1[1][2] * in2[2][3] + in1[1][3] * in2[3][3];
    
out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0] + in1[2][3] * in2[3][0];
    
out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1] + in1[2][3] * in2[3][1];
    
out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2] + in1[2][3] * in2[3][2];
    
out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + in1[2][2] * in2[2][3] + in1[2][3] * in2[3][3];
    
out[3][0] = in1[3][0] * in2[0][0] + in1[3][1] * in2[1][0] + in1[3][2] * in2[2][0] + in1[3][3] * in2[3][0];
    
out[3][1] = in1[3][0] * in2[0][1] + in1[3][1] * in2[1][1] + in1[3][2] * in2[2][1] + in1[3][3] * in2[3][1];
    
out[3][2] = in1[3][0] * in2[0][2] + in1[3][1] * in2[1][2] + in1[3][2] * in2[2][2] + in1[3][3] * in2[3][2];
    
out[3][3] = in1[3][0] * in2[0][3] + in1[3][1] * in2[1][3] + in1[3][2] * in2[2][3] + in1[3][3] * in2[3][3];
}

Matrix4x4_ConcatRotateFloat:out[4][4], Float:angle,FloatxFloatyFloat)
{
    new 
Float:base[4][4], Float:temp[4][4];
    
    
Matrix4x4_Copybaseout );
    
Matrix4x4_CreateRotatetempanglexy);
    
Matrix4x4_Concatoutbasetemp );
}

Matrix4x4_CreateTranslateFloat:out[4][4], Float:xFloat:yFloat)
{
    
out[0][0] = 1.0;
    
out[0][1] = 0.0;
    
out[0][2] = 0.0;
    
out[0][3] = x;
    
out[1][0] = 0.0;
    
out[1][1] = 1.0;
    
out[1][2] = 0.0;
    
out[1][3] = y;
    
out[2][0] = 0.0;
    
out[2][1] = 0.0;
    
out[2][2] = 1.0;
    
out[2][3] = z;
    
out[3][0] = 0.0;
    
out[3][1] = 0.0;
    
out[3][2] = 0.0;
    
out[3][3] = 1.0;
}

Matrix4x4_ConcatTranslateFloat:out[4][4], Float:xFloat:yFloat:)
{
    new 
Floatbase[4][4], Floattemp[4][4];
    
    
Matrix4x4_Copybaseout );
    
Matrix4x4_CreateTranslatetempxy);
    
Matrix4x4_Concatoutbasetemp );
}

Matrix4x4_CreateProjectionFloat:out[4][4], Float:xMaxFloatxMinFloatyMaxFloatyMinFloatzNearFloat:zFar )
{
    
out[0][0] = ( 2.0 zNear ) / ( xMax xMin );
    
out[1][1] = ( 2.0 zNear ) / ( yMax yMin );
    
out[2][2] = -( zFar zNear ) / ( zFar zNear );
    
out[3][3] = out[0][1] = out[1][0] = out[3][0] = out[0][3] = out[3][1] = out[1][3] = 0.0;
    
    
out[2][0] = 0.0;
    
out[2][1] = 0.0;
    
out[0][2] = ( xMax xMin ) / ( xMax xMin );
    
out[1][2] = ( yMax yMin ) / ( yMax yMin );
    
out[3][2] = -1.0;
    
out[2][3] = -( 2.0 zFar zNear ) / ( zFar zNear );
}

R_SetupProjectionMatrixFloat:m[4][4] )
{
    new 
Float:xMinFloat:xMaxFloat:yMinFloat:yMaxFloat:zNearFloat:zFar;
    
    
zNear 4.0
    
    zFar 
floatmax256.08192.0 );
    
    
yMax zNear floattan74.0 M_PI 360.0 );
    
yMin = -yMax;
    
    
xMax zNear floattan90.0 M_PI 360.0 );
    
xMin = -xMax;
    
    
Matrix4x4_CreateProjectionmxMaxxMinyMaxyMinzNearzFar );
}

R_SetupModelviewMatrixFloatm[4][4], Float:viewangles[3], Float:vieworg[3])
{
    
Matrix4x4_CreateModelview);
    
Matrix4x4_ConcatRotatem, -viewangles[2], 1.00.00.0 );
    
Matrix4x4_ConcatRotatem, -viewangles[0], 0.01.00.0 );
    
Matrix4x4_ConcatRotatem, -viewangles[1], 0.00.01.0 );
    
Matrix4x4_ConcatTranslatem, -vieworg[0], -vieworg[1], -vieworg[2] );



https://i.imgur.com/vAikL2d.jpg

meTaLiCroSS 01-22-2023 18:53

Re: Origin to Hud
 
Long time ago I saw a GHW_Chronic code that did this but it lacks of precision when origin is outside player's viewcone, so I highly thank you for sharing this and the corresponding author.

EFFx 01-23-2023 01:37

Re: Origin to Hud
 
I was noticing that problem as soon as I started testing the code. By using the debug to print the values of fHudPos[0] and fHudPos[1], I noticed that the values suddenly jumped from 0.1 or 0.9 to negative values, which I guess was making left origin positions go right positions in the hud. Also even large numbers were found such as 2383181.0 one time, after clamping the values it all went well.

I always wanted something like that, after finding out about it I decided to share. The calculation was well built and even though I don't understand it, seems impressive.


All times are GMT -4. The time now is 07:58.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.