#need help with a search algorithm

1 messages · Page 1 of 1 (latest)

bleak wharf
#
function onRayHit()
    
    -- checking for the nearest element
    
    for name,i in pairs(ElmPos) do -- elementPositions
        
        local currdist = (i - cam.CFrame.Position).Magnitude -- current distance

        if currdist < mindist then
            mindist = currdist -- assigning the minimum distance
            minpos = i -- assinging the minimum position
            elementName = name
        end
        
    end
    
    -- making the fearline
    
    FearLine = workspace:Raycast(cam.CFrame.Position, minpos - cam.CFrame.Position,whitelist)
    
    if FearLine.Instance:FindFirstAncestor("Elements") == ElmFold then -- checking if its an element
        print("seen by: " , elementName , mindist)
        cube.Position = FearLine.Instance.Position
    else
        print("hit a wall")
        -- reset distance and position
        mindist = math.huge
        minpos = Vector3.zero
    end
    
end

took me a couple of days to make this search mechanic , turns out it dosent work if the shortest element is blocked by a wall even though theres a second shortest that isnt blocked , now i have to not only implement a nearest algorithm but also one that has a valid nieghbor (not hit by wall) , which sounds like a complete nightmare fuel

before you say this is shit code remember that blood and tears were put into it , im trying i really am

frozen hawk
bleak wharf
tall nova
#

you can use collisiongroups

bleak wharf
# tall nova you can use collisiongroups

the issue bieng i wanted the function to look for the second or third shortest element if the shortest one is blocked , but i cant scratch my head around making that algorithm , seems kinda complicated and needs planing so im leaving it for tommorow

tall nova
#

you can extend the ray by casting from the point u hit

#

if a ray starts within a part it won't detect it

bleak wharf
#

thinking of making an exclusion table for all the checked elements that were blocked by a wall

#

or something like that

#

gonna leave it for tommorow