Jump to content

Volumetric raycast in a cone shape


Wulfie Reanimator
 Share

You are about to reply to a thread that has been inactive for 1402 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

llSensor has a lot of limitations, just like llCastRay. So here's a combination that works much like a sensor, but only looks for one target.

integer CastCone(key id, vector direction, float radius, float arc)
{
    vector start = llGetPos();
    vector target = llList2Vector(llGetObjectDetails(id, (list)OBJECT_POS), 0);
    vector length = target - start;

    if (length*length > radius*radius)
        return (FALSE);

    float angle = llRot2Angle(llRotBetween(direction, length));

    if (angle > arc)
        return (FALSE);

    list ray = llCastRay(start, target, [RC_DATA_FLAGS, RC_GET_ROOT_KEY, RC_REJECT_TYPES, RC_REJECT_PHYSICAL]);

    if (llList2Integer(ray, -1) > 0 && llList2Key(ray, 0) != id)
        return (FALSE);

    return (TRUE);
}

Example use:

default
{
    touch_start(integer total_number)
    {
        llOwnerSay("Test");
        integer result = CastCone(llGetOwner(), <1,0,0> * llGetRot(), 30, 45 * DEG_TO_RAD);
        llOwnerSay((string)result);
    }
}

Replicating llSensor behavior (returns a list of avatar names inside cone):

default
{
    touch_start(integer total_number)
    {
        vector direction = <1,0,0> * llGetRot();
        list agents = llGetAgentList(AGENT_LIST_REGION, []);
        integer i = llGetListLength(agents);
        list detected;

        while (~--i)
        {
            key agent = llList2Key(agents, i);
            if (CastCone(agent, direction, 30, 45 * DEG_TO_RAD))
            {
                detected += llKey2Name(agent);
            }
        }

        llOwnerSay("Found: " + llList2CSV(detected));
    }
}

So, essentially, the CastCone function will return TRUE if the target (avatar or object) is inside the cone and there is direct line of sight to the target.

It will return TRUE when the ray hits or misses the target. This is because moving targets may have changed position since the start of the function. If the ray hits something that is not the target, it will return FALSE.

It only uses one ray and only if the target is already known to exist inside the cone. This makes it very efficient even if needed to use repeatedly.

What I'm too lazy to do (and would like to see) is similar functions for boxes (BoxCast), especially ones that aren't positioned on the source object.

Edited by Wulfie Reanimator
  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1402 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...