Cast a line from start to end and report collision data for intersections with objects.
list llCastRay( vector start, vector end, list options );
Example return of successful raycast, using the default options:
[key object_uuid, vector hit_position, integer status_code]
In the case of an error, or if the ray hits nothing, the resulting list only contains the status code:
[integer status_code]
Returns a list of strided values on a successful hit, with an additional integer status_code at the end.
Each stride consists of two mandatory values {key uuid, vector position} and optionally {integer link_number, vector normal}. (See RC_DATA_FLAGS for details.)
A negative status_code is an error code, otherwise it is the number of hits (and strides) returned.
status_code is a number tacked onto the end of the strided list to give you extra information about the ray cast.
If the cast succeeded, it will be >=0 and will indicate the number of hits.
If the ray cast failed (which should only happen right now if the simulator performance is running low), you'll get a negative status code. RCERR_SIM_PERF_LOW will be used as the status code if the overall physics time in the simulator is too high to perform raycasts. The idea is that you will know to try your cast again in a few frames.
status_code error codes and their meanings.
| Status Code | V | Description |
|---|---|---|
| RCERR_UNKNOWN | -1 | The raycast failed for an unspecified reason. Please submit a bug report. |
| RCERR_SIM_PERF_LOW | -2 | The raycast failed because simulator performance is low. Wait a while and then try again. If possible reduce the scene complexity. |
| RCERR_CAST_TIME_EXCEEDED | -3 | The raycast failed because the parcel or agent has exceeded the maximum time allowed for raycasting. This resource pool is continually replenished, so waiting a few frames and retrying is likely to succeed. |
Note: SCR-199 indicates that pools have been removed from the main grid, so this return code should not appear.
Tips for Efficient Raycasts:
RC_REJECT_LAND | RC_REJECT_PHYSICAL | RC_REJECT_NONPHYSICALoptions flags and their parameters
| Flag | V | Parameters | Default Value | Description |
|---|---|---|---|---|
| [ RC_REJECT_TYPES ] | 0 | [ integer filter ] | [ 0 ] | Mask used to ignore specific types of objects (and avatars). |
| [ RC_DATA_FLAGS ] | 2 | [ integer flags ] | [ 0 ] | Described in the RC_DATA_FLAGS section. |
| [ RC_MAX_HITS ] | 3 | [ integer max_hits ] | [ 1 ] | Maximum number of hits to return. Maximum value is 256. To avoid performance issues, keep it small. |
| [ RC_DETECT_PHANTOM ] | 1 | [ integer detect_phantom ] | [ FALSE ] | Set to TRUE (or nonzero) to detect phantom AND volume detect objects. It is not possible to detect only phantom objects or only volume detect objects. If set to TRUE, phantom and volume detect objects will always be detected, even if RC_REJECT_NONPHYSICAL and RC_REJECT_PHYSICAL are set in RC_REJECT_TYPES. |
filter is a bitwise-or combination of the following constants: RC_REJECT_AGENTS, RC_REJECT_PHYSICAL, RC_REJECT_NONPHYSICAL, and RC_REJECT_LAND.
RC_REJECT_TYPES and their meanings.
| Reject Type | V | Description |
|---|---|---|
| RC_REJECT_AGENTS | 1 | Avatars won't be detected. |
| RC_REJECT_PHYSICAL | 2 | Physical objects won't be detected. |
| RC_REJECT_NONPHYSICAL | 4 | The opposite of the above flag. Objects without physics won't be detected. For phantom objects, see RC_DETECT_PHANTOM. |
| RC_REJECT_LAND | 8 | Land won't be detected. This refers to actual ground only. |
If you reject everything, a script runtime error will be generated (as it makes no sense to do this). Using 0 as the filter value will accept all types (default).
Also note that seated agents are treated like unseated agents. As in, you either get seated and unseated agents in your results, or you use RC_REJECT_AGENTS and get neither.
flags is a bitwise-or combination of: RC_GET_NORMAL, RC_GET_ROOT_KEY, and RC_GET_LINK_NUM.
RC_DATA_FLAGS and their meanings.
| Data Flag | V | Description |
|---|---|---|
| RC_GET_NORMAL | 1 | Stride includes the surface normal that was hit. |
| RC_GET_ROOT_KEY | 2 | The hit uuid will be replaced by the object's root instead of any child. |
| RC_GET_LINK_NUM | 4 | Stride includes the link number that was hit. |
This basic example will cast a ray from the center of the object, 10 meters forward, depending on the object's rotation.
default
{
touch_start(integer total_number)
{
vector start = llGetPos();
vector end = start + <10,0,0> * llGetRot();
list data = llCastRay(start, end, []);
llOwnerSay(llList2CSV(data));
}
}
This is an example attachment that casts a ray based on the owner's camera in mouselook. It has many applications for things like weapons, scripted interactions with the world (like allowing a HUD to display information about things the user is looking at), etc.
integer gTargetChan = -9934917;
default
{
attach(key id)
{
if (id != NULL_KEY)
{
llRequestPermissions(id,PERMISSION_TAKE_CONTROLS|PERMISSION_TRACK_CAMERA);
}
}
run_time_permissions (integer perm)
{
if (perm & PERMISSION_TAKE_CONTROLS|PERMISSION_TRACK_CAMERA)
{
llTakeControls(CONTROL_LBUTTON|CONTROL_ML_LBUTTON,TRUE,FALSE);
}
}
control (key id, integer level, integer edge)
{
// User must be in mouselook to aim the weapon
if (level & edge & CONTROL_LBUTTON)
{
llSay(0,"You must be in Mouselook to shoot. Type \"CTRL + M\" or type \"Esc\" and scroll your mouse wheel forward to enter Mouselook.");
}
// User IS in mouselook
if (level & edge & CONTROL_ML_LBUTTON)
{
vector start = llGetCameraPos();
// Detect only a non-physical, non-phantom object. Report its root prim's UUID.
list results = llCastRay(start, start+<60.0,0.0,0.0>*llGetCameraRot(),[RC_REJECT_TYPES,RC_REJECT_PHYSICAL|RC_REJECT_AGENTS|RC_REJECT_LAND,RC_DETECT_PHANTOM,FALSE,RC_DATA_FLAGS,RC_GET_ROOT_KEY,RC_MAX_HITS,1]);
llTriggerSound(llGetInventoryName(INVENTORY_SOUND,0),1.0);
llSleep(0.03);
key target = llList2Key(results,0);
// Tell target that it has been hit.
llRegionSayTo(target,gTargetChan,"HIT");
// Target, scripted to listen on gTargetChan, can explode, change color, fall over .....
}
}
}
This example handles the caveat about rays extending outside of region bounds by calculating the point where the ray intersects with the region's edge.
vector GetRegionEdge(vector start, vector dir)
{
float scaleGuess;
float scaleFactor = 4095.99;
if (dir.x)
{
scaleFactor = ((dir.x > 0) * 255.99 -start.x) / dir.x;
}
if (dir.y)
{
scaleGuess = ((dir.y > 0) * 255.99 - start.y) / dir.y;
if (scaleGuess < scaleFactor) scaleFactor = scaleGuess;
}
if (dir.z)
{
scaleGuess = ((dir.z > 0) * 4095.99 - start.z) / dir.z;
if (scaleGuess < scaleFactor) scaleFactor = scaleGuess;
}
return start + dir * scaleFactor;
}
default
{
touch_start(integer total_number)
{
vector start = llGetPos();
vector direction = <1,0,0> * llGetRot();
vector end = GetRegionEdge(start, direction);
list data = llCastRay(start, end, []);
llOwnerSay(llList2CSV(data));
}
}
This example casts a ray from the center of the object, 25 meters north, while applying different RC_REJECT_TYPES each time.
integer filter;// default is 0
default
{
state_entry()
{
string ownerName = llKey2Name(llGetOwner());
llOwnerSay("Hello, " + ownerName + "!");
}
touch_start(integer total_number)
{
vector start = llGetPos();
vector end = start - <0.0, -25.0, 0.0>;
if ( filter > 8 )
filter = 0;
llOwnerSay("Filter " + (string)filter);
list results = llCastRay(start, end, [RC_REJECT_TYPES, filter, RC_MAX_HITS, 4] );
integer hitNum = 0;
// Handle error conditions here by checking llList2Integer(results, -1) >= 0
while (hitNum < llList2Integer(results, -1))
{
// Stride is 2 because we didn't request normals or link numbers
key uuid = llList2Key(results, 2*hitNum);
string name = "Land"; // if (uuid == NULL_KEY)
if (uuid != NULL_KEY)
name = llKey2Name(uuid);
llOwnerSay("Hit " + name + ".");
++hitNum;
}
++filter;
}
}
Use llDumpList2String to see what the output looks like when you try a new set of flags.
To quickly get the status code use llList2Integer(result, -1).
Ideas for uses: