• Find entity closest to a point
    7 replies, posted
I am good at many things. Math is not one of them. I have a feeling this requires math. Anyway, I'm attempting to perform an eye trace from the player, then find all entities withinin a radius of the HitPos with findinsphere, and out of all of the found entities, return which entity is the closest to the center of the search area. It's probably simple, but I'm not able to figure it out. Tips?
your idea is the right one so do it [editline]5th April 2015[/editline] the "math" is just checking vector:Distance(vector) for > or <
use DistToSqr, it's cheaper
Ok yes I know what I want it to do but I'm unable to actually accomplish it. I'm not sure how to sort through and return the entity with the smallest distance other than creating like 3 tables, which I can't imagine is right...
create two variables outside your loop (or one table) to store the lowest distance and the entity it belongs to loop through all the entities, if its distance is lower than what your variable is, change your variable at the end of your loop, your variable will be the lowest distance and the closest entity
Something like... [code] local trace = {} trace.start = client:GetPos() trace.endpos = client:GetAimVector()*1000 - Vector(0,0,64) trace.filter = {client} local data = util.TraceLine(trace) local ply for k,v in pairs(ents.FindInSphere(data.HitPos, 200)) do if v:IsPlayer() then if (v:GetPos():DistToSqr(data.HitPos) < ply:GetPos():DistToSqr(data.HitPos)) or !ply then ply = v end end end [/code]
Is there some reason you're not using ply:GetEyeTrace().HitPos? That's the spot the player sees themselves as looking at. [code] local ply = FindMetaTable("Player") function ply:FindClosestEyeTraceSomething(range) local dist = range+50 local closest local hitpos = ply:GetEyeTrace().HitPos for k, v in pairs(ents.FindInSphere(hitpos,range)) do local calcdist = v:GetPos():Distance(hitpos) if calcdist < dist then dist = calcdist closest = v end end return closest end [/code] [editline]5th April 2015[/editline] and to find the closest something (i didn't add a check for entity class there but you can do that yourself) you can then just run this on a player object as closestsomething = ply:FindClosestEyeTraceSomething(200) [editline]5th April 2015[/editline] didn't use disttosqr because i've not used it before and i don't know how that would effect my simply using the range+50 as the starting comparison distance
[QUOTE=bitches;47460577]Is there some reason you're not using ply:GetEyeTrace().HitPos? That's the spot the player sees themselves as looking at. [code] local ply = FindMetaTable("Player") function ply:FindClosestEyeTraceSomething(range) local dist = range+50 local closest local hitpos = ply:GetEyeTrace().HitPos for k, v in pairs(ents.FindInSphere(hitpos,range)) do local calcdist = v:GetPos():Distance(hitpos) if calcdist < dist then dist = calcdist closest = v end end return closest end [/code] [editline]5th April 2015[/editline] and to find the closest something (i didn't add a check for entity class there but you can do that yourself) you can then just run this on a player object as closestsomething = ply:FindClosestEyeTraceSomething(200) [editline]5th April 2015[/editline] didn't use disttosqr because i've not used it before and i don't know how that would effect my simply using the range+50 as the starting comparison distance[/QUOTE] sorry I'm late, but I was using it while the player is in a vehicle, and since geteyetrace hits the vehicle, I had to make my own trace. PortalGod's suggestion got it working for me just fine. Thanks for your help though.
Sorry, you need to Log In to post a reply to this thread.