Raised This Month: $51 Target: $400
 12% 

Finding entities in a radius?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
(-DR-)GrammerNatzi
Senior Member
Join Date: Jun 2009
Old 08-01-2009 , 21:33   Finding entities in a radius?
Reply With Quote #1

I am trying to work on something for my plug-in, and I need to find out how to find entities (the infected, as I am working in Left 4 Dead) in a radius around an X Y Z coordinate. For example, let's say I wanted to find entities around a bullet impact with a radius of about 300. How would I do this? Is it even possible?
(-DR-)GrammerNatzi is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 08-01-2009 , 23:00   Re: Finding entities in a radius?
Reply With Quote #2

If you have the coordinate of the impact, (the event should provide it) its just the distance formula.

sqrt ( (x2-x1)^2 + (y2-y1)^2 + (z1 - z2)^2 )

Or you can just look in vector.inc: GetVectorDistance(vec1, vec2)


Loop through all the entities and check if it's within the given radius:

PHP Code:
new Float:entityloc[3];

new 
maxentities GetMaxEntities(); // the highest number of entities on the map.
for (new MAXPLAYERS 1<= maxentitiesx++)  // start at 65, 1 higher than the max player index.
{
    if (!
IsValidEdict(x)) // if the int isn't a valid entity index, then stop
    
{
        continue;
    }
    
    
GetEntPropVector(xProp_Data"m_vecOrigin"entityloc); // get the coordinate of the entity.

    
if (GetVectorDistance(bulletlocentityloc) <= 300.0// check if the entity is within 300 units of the entity, if it is then do whatever you needa do.
    
{
        
// this entity is within 300 units.
    
}

Keep in mind this code was written in this little reply-box completely untested for compilation. So it might require some tweaks.

Good luck.
__________________
Greyscale is offline
DaFox
Senior Member
Join Date: Mar 2005
Old 08-02-2009 , 00:03   Re: Finding entities in a radius?
Reply With Quote #3

If your only wanting to find infected your better off using FindEntityByClassname() I think.

Also We should expose the virtual function "EnumerateElementsInSphere()" I would assume thats much faster than the current methods that we have.
DaFox is offline
AtomicStryker
Veteran Member
Join Date: Apr 2009
Location: Teutonia!!
Old 08-02-2009 , 04:57   Re: Finding entities in a radius?
Reply With Quote #4

PHP Code:
    new Float:DISTANCESETTING 300;
    
    new 
Float:impact[3];    
    
impact[0] = GetEventFloat(event,"x");
    
impact[1] = GetEventFloat(event,"y");
    
impact[2] = GetEventFloat(event,"z");

    
    for (new 
target 1target <= GetMaxClients(); target++)
    {
        if (
IsClientInGame(target))
        {
            if (
IsPlayerAlive(target) && GetClientTeam(target) == 3)
            {
                new 
Float:targetVector[3];
                
GetClientAbsimpact(targettargetVector);
                
                new 
Float:distance GetVectorDistance(targetVectorimpact);
                if (
distance DISTANCESETTING)
                    {                            
                    
//ACTIONS ON AFFECTED TARGET
                    
}
            }
        }
    } 
AtomicStryker is offline
DaFox
Senior Member
Join Date: Mar 2005
Old 08-02-2009 , 08:18   Re: Finding entities in a radius?
Reply With Quote #5

@AtomicStryker

You should be using decl instead of new for your arrays, And you should never be creating variables inside a loop like that.
DaFox is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 08-02-2009 , 14:07   Re: Finding entities in a radius?
Reply With Quote #6

Quote:
Originally Posted by AtomicStryker View Post
PHP Code:
    new Float:DISTANCESETTING 300;
    
    new 
Float:impact[3];    
    
impact[0] = GetEventFloat(event,"x");
    
impact[1] = GetEventFloat(event,"y");
    
impact[2] = GetEventFloat(event,"z");

    
    for (new 
target 1target <= GetMaxClients(); target++)
    {
        if (
IsClientInGame(target))
        {
            if (
IsPlayerAlive(target) && GetClientTeam(target) == 3)
            {
                new 
Float:targetVector[3];
                
GetClientAbsimpact(targettargetVector);
                
                new 
Float:distance GetVectorDistance(targetVectorimpact);
                if (
distance DISTANCESETTING)
                    {                            
                    
//ACTIONS ON AFFECTED TARGET
                    
}
            }
        }
    } 

I assume this is a typo:

"GetClientAbsimpact" (search replace mistake)



Code:
new Float:targetVector[3];
Move this out of the loop so you're not reinitializing it 64 times. And also use 'decl' this is faster as well. The difference between 'decl' and 'new' is that new gives the object an inital value (like 0, false, 0.0, "") while decl just leaves whatever garbage was in memory before, but you plan on overwriting it. This improves performance a little as well.


Don't use GetMaxClients(), use MaxClients. It's a dynamic variable that holds what you need and is quicker than calling a function.
__________________
Greyscale is offline
jeremyvillanueva
AlliedModders Donor
Join Date: Jan 2021
Location: dcord:Jeremy333#7632
Old 02-15-2021 , 22:27   Re: Finding entities in a radius?
Reply With Quote #7

Hi,
"GetClientAbsimpact" should be replaced by GetClientAbsOrigin

Like this,
float DISTANCESETTING = 5;

float impact[3];
float targetVector[3];

int TotalSurvivors=0;

impact[0] = GetEventFloat(event,"x");
impact[1] = GetEventFloat(event,"y");
impact[2] = GetEventFloat(event,"z");
for (int target=1;target<=MaxClients;target++)
{
if (IsClientInGame(target) && GetClientTeam(target) == 2 && IsPlayerAlive(target) && !IsFakeClient(target))
{
GetClientAbsOrigin(target, targetVector);

float distance = GetVectorDistance(targetVector, impact);
if (distance < DISTANCESETTING)
{
//ACTIONS ON AFFECTED TARGET
}
}
}

thanks a lot,
jeremyvillanueva is offline
Reply



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 16:01.


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