Raised This Month: $32 Target: $400
 8% 

bool:IsEntVisible


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
grankee
Member
Join Date: Jun 2008
Old 11-23-2015 , 16:47   bool:IsEntVisible
Reply With Quote #1

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
}

Last edited by grankee; 11-27-2015 at 12:42.
grankee is offline
RedSword
SourceMod Plugin Approver
Join Date: Mar 2006
Location: Quebec, Canada
Old 11-24-2015 , 21:40   Re: bool:IsEntVisible
Reply With Quote #2

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).
__________________
My plugins :
Red Maze
Afk Bomb
RAWR (per player/rounds Awp Restrict.)
Kill Assist
Be Medic

You can also Donate if you appreciate my work

Last edited by RedSword; 11-29-2015 at 20:37.
RedSword is offline
grankee
Member
Join Date: Jun 2008
Old 11-27-2015 , 12:37   Re: bool:IsEntVisible
Reply With Quote #3

So if I'll square argument for compare with squared result then function will be lighter?
grankee is offline
RedSword
SourceMod Plugin Approver
Join Date: Mar 2006
Location: Quebec, Canada
Old 11-29-2015 , 20:39   Re: bool:IsEntVisible
Reply With Quote #4

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.
__________________
My plugins :
Red Maze
Afk Bomb
RAWR (per player/rounds Awp Restrict.)
Kill Assist
Be Medic

You can also Donate if you appreciate my work
RedSword is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 12-05-2015 , 19:29   Re: bool:IsEntVisible
Reply With Quote #5

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?
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.
Dr. Greg House is offline
_pHabb
Senior Member
Join Date: Jul 2013
Location: GetCountry(pHabb);
Old 01-31-2016 , 16:04   Re: bool:IsEntVisible
Reply With Quote #6

Work with looking at player from something box(model)/brush(door)?
_pHabb is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:16.


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