AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   bool:IsEntVisible (https://forums.alliedmods.net/showthread.php?t=275162)

grankee 11-23-2015 16:47

bool:IsEntVisible
 
If I can make it better let me know
almost totally inspired and based on code from this forum

# requires SMLib
Code:

/**
 * Check if there is something visible between the player and the entity.
 *       
 * @param client              client entity index
 * @param entity              entity index
 * @param Float:maxdistance      max distance to check if specified
 *                          otherwise check in infinited distance
 * @param bool:FromEyePosition    determine if start position will be from eyes or AbsOrigin
 *
 * @return                  true if visible, false if not
 */
stock bool:IsEntVisible(client,entity,const Float:maxdistance=0.0,const bool:FromEyePosition=false)
{
        if((Entity_GetDistance(client,entity)>maxdistance)&&(maxdistance>0.0)) return false //if maxdistance used (!0.0) and distance longer return 0
        new Float:vClientOrigin[3],Float:vEntOrigin[3]
        FromEyePosition ? GetClientEyePosition(client,vClientOrigin) : Entity_GetAbsOrigin(client,vClientOrigin)
        Entity_GetAbsOrigin(entity,vEntOrigin)
        //Check for colliding entities
        new Handle:TrHandle=TR_TraceRayFilterEx(vClientOrigin, vEntOrigin, MASK_VISIBLE_AND_NPCS,RayType_EndPoint, TraceRayDontHitSelf, client)
        new TRIndex
        if (TR_DidHit(TrHandle))
        {
                TRIndex = TR_GetEntityIndex(TrHandle)
        }
        return bool:(TRIndex==entity)
}

public bool:TraceRayDontHitSelf(entity, mask, any:data)
{
        if(entity == data) // Check if the TraceRay hit the itself.
        {
        return false // Don't let the entity be hit
        }

        return true // It didn't hit itself
}


RedSword 11-24-2015 21:40

Re: bool:IsEntVisible
 
You should say that it requires SMLib. Also, when comparing distances without needing to know the exact distance, you can avoid a square root, which is a "somewhat heavy" math operation. To do so you'll need to pow2 on the other side of the comparison. Using SMLib you can't optimize that.

See https://sm.alliedmods.net/api/index....d=show&id=547& (third arg and SMLib source code for more info).

grankee 11-27-2015 12:37

Re: bool:IsEntVisible
 
So if I'll square argument for compare with squared result then function will be lighter?

RedSword 11-29-2015 20:39

Re: bool:IsEntVisible
 
I changed the wording in my last post, to make it clearer.

Yes, it is better to square an operand than to square-root the other operand.

Dr. Greg House 12-05-2015 19:29

Re: bool:IsEntVisible
 
I don't find this useful (and somewhat wrong and confusing) for the following reasons:

-It requires smlib
-The player's abs origin is at his foot, giving you a falsely false result most of the time (you define this as the default origin to use)
-To elaborate on that, you're also using the abs origin of the target entity (regardless of the ground hit issue, on bigger objects this can lead to issues)
-I haven't looked up if the mask triggers on hitbox or on vphysics collision (and stuff like glass), but imagine a different scenario based on a small entity just happening to be between the players.

I have the following suggestions to make this useful to others as a snippet:

-Remove the smlib requirement
-Add in a fov check before the trace ray (because it's a snippet and you don't know how many times random AM user xyz may call this)
-Make the trace ray optional (because a simple fov check might be just as useful)
-Maybe make it being hit only by world (and maybe bigger props?)
-Pick better starting/end points. (Either eye positions for clients or hitbox midpoints for entities)
-Instead of end points, just use the client's eye-angles. Imagine a player being half-way behind a wall with the end position being obscured by it, even though the player can be seen?

_pHabb 01-31-2016 16:04

Re: bool:IsEntVisible
 
Work with looking at player from something box(model)/brush(door)?


All times are GMT -4. The time now is 18:37.

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