Thursday, December 19, 2013

Another way to trace line

In addition to the method of official documentation, there is a method taken from the code Source SDK 2013. The code is intended for the console command "surfaceprop".

Code displays all the information about the surface (entities), model, texture, as well as the distance to the point of impact trace.

Open up server/physics.cpp and find the CON_COMMAND_F(surfaceprop, "Reports the surface properties at the cursor", FCVAR_CHEAT ) line.

CON_COMMAND_F(surfaceprop, "Reports the surface properties at the cursor", FCVAR_CHEAT )
{
if ( !UTIL_IsCommandIssuedByServerAdmin() )
return;

CBasePlayer *pPlayer = UTIL_GetCommandClient();

trace_t tr;
Vector forward;
pPlayer->EyeVectors( &forward );
UTIL_TraceLine(pPlayer->EyePosition(), pPlayer->EyePosition() + forward * MAX_COORD_RANGE,
MASK_SHOT_HULL|CONTENTS_GRATE|CONTENTS_DEBRIS, pPlayer, COLLISION_GROUP_NONE, &tr );

if ( tr.DidHit() )
{
const model_t *pModel = modelinfo->GetModel( tr.m_pEnt->GetModelIndex() );
const char *pModelName = STRING(tr.m_pEnt->GetModelName());
if ( tr.DidHitWorld() && tr.hitbox > 0 )
{
ICollideable *pCollide = staticpropmgr->GetStaticPropByIndex( tr.hitbox-1 );
pModel = pCollide->GetCollisionModel();
pModelName = modelinfo->GetModelName( pModel );
}
CFmtStr modelStuff;
if ( pModel )
{
modelStuff.sprintf("%s.%s ", modelinfo->IsTranslucent( pModel ) ? "Translucent" : "Opaque",
modelinfo->IsTranslucentTwoPass( pModel ) ? "  Two-pass." : "" );
}

// Calculate distance to surface that was hit
Vector vecVelocity = tr.startpos - tr.endpos;
int length = vecVelocity.Length();

Msg("Hit surface \"%s\" (entity %s, model \"%s\" %s), texture \"%s\"\n", physprops->GetPropName( tr.surface.surfaceProps ), tr.m_pEnt->GetClassname(), pModelName, modelStuff.Access(), tr.surface.name);
Msg("Distance to surface: %d\n", length );
}
}