Name String
XR_ANDROID_raycast
Extension Type
Instance extension
Registered Extension Number
464
Revision
1
Extension and Version Dependencies
Last Modified Date
2024-10-02
IP Status
No known IP claims.
Contributors
Spencer Quin, Google
Nihav Jain, Google
John Pursey, Google
Jared Finder, Google
Levana Chen, Google
Kenny Vercaemer, Google
Overview
This extension allows the application to perform raycasts against trackables in the environment. Raycasts can be useful for detecting objects in the environment that a ray would intersect with. For example:
- To determine where a floating object would fall when dropped, by using a vertical raycast.
- To determine where a user is looking, by using a forward-facing raycast.
Query supported raycast capabilities
The xrEnumerateRaycastSupportedTrackableTypesANDROID function is defined as:
XrResult xrEnumerateRaycastSupportedTrackableTypesANDROID(
    XrInstance                                  instance,
    XrSystemId                                  systemId,
    uint32_t                                    trackableTypeCapacityInput,
    uint32_t*                                   trackableTypeCountOutput,
    XrTrackableTypeANDROID*                     trackableTypes);
Parameter Descriptions
- instanceis the XrInstance from which- systemIdwas retrieved.
- systemIdis the- XrSystemIdwhose supported trackable types for raycasting are being enumerated.
- trackableTypeCapacityInputis the capacity of the- trackableTypes, or 0 to retrieve the required capacity.
- trackableTypeCountOutputis a pointer to the count of the array, or a pointer to the required capacity in the case that- trackableTypeCapacityInputis insufficient.- trackableTypesis a pointer to an array of XrTrackableTypeANDROID, but can be- NULLif- trackableTypeCapacityInputis- 0.
 
- See the Buffer Size Parameters section for a detailed description of
retrieving the required trackableTypessize.
xrEnumerateRaycastSupportedTrackableTypesANDROID enumerates the trackable types that support raycasting by the current session.
Valid Usage (Implicit)
- The XR_ANDROID_raycastextension must be enabled prior to calling xrEnumerateRaycastSupportedTrackableTypesANDROID
- instancemust be a valid XrInstance handle
- trackableTypeCountOutputmust be a pointer to a- uint32_tvalue
- If trackableTypeCapacityInputis not0,trackableTypesmust be a pointer to an array oftrackableTypeCapacityInputXrTrackableTypeANDROID values
Return Codes
- XR_SUCCESS
- XR_ERROR_VALIDATION_FAILURE
- XR_ERROR_RUNTIME_FAILURE
- XR_ERROR_HANDLE_INVALID
- XR_ERROR_INSTANCE_LOST
- XR_ERROR_SIZE_INSUFFICIENT
- XR_ERROR_SYSTEM_INVALID
- XR_ERROR_FUNCTION_UNSUPPORTED
Perform a raycast
The xrRaycastANDROID function is defined as:
XrResult xrRaycastANDROID(
    XrSession                                   session,
    const XrRaycastInfoANDROID*                 rayInfo,
    XrRaycastHitResultsANDROID*                 results);
Parameter Descriptions
- sessionis the XrSession that performs the raycast.
- rayInfois the XrRaycastInfoANDROID that describes the ray to cast.
- resultsis a pointer to the XrRaycastHitResultsANDROID that receives the results of the raycast.
The application can perform raycasts by calling xrRaycastANDROID.
- If a raycast intersects more trackables than XrRaycastHitResultsANDROID::maxResults, the runtime must return the hit results that are closest to the XrRaycastHitResultsANDROID::origin of the ray.
- If a raycast intersects a trackable of type
XR_TRACKABLE_TYPE_ANDROID_PLANE, that is subsumed by another plane, the runtime must return the hit result for the subsuming plane only.
- The runtime must return the hit results in closest-to-farthest order from XrRaycastInfoANDROID::origin along the XrRaycastInfoANDROID::trajectory vector.
- The runtime must return XR_ERROR_TRACKABLE_TYPE_NOT_SUPPORTED_ANDROIDif the trackable type corresponding to the XrTrackableTrackerANDROID handles in XrRaycastInfoANDROID::trackers are not enumerated by xrEnumerateRaycastSupportedTrackableTypesANDROID.
Valid Usage (Implicit)
- The XR_ANDROID_raycastextension must be enabled prior to calling xrRaycastANDROID
- sessionmust be a valid XrSession handle
- rayInfomust be a pointer to a valid XrRaycastInfoANDROID structure
- resultsmust be a pointer to an XrRaycastHitResultsANDROID structure
Return Codes
- XR_SUCCESS
- XR_SESSION_LOSS_PENDING
- XR_ERROR_FUNCTION_UNSUPPORTED
- XR_ERROR_TRACKABLE_TYPE_NOT_SUPPORTED_ANDROID
- XR_ERROR_VALIDATION_FAILURE
- XR_ERROR_RUNTIME_FAILURE
- XR_ERROR_HANDLE_INVALID
- XR_ERROR_INSTANCE_LOST
- XR_ERROR_SESSION_LOST
- XR_ERROR_OUT_OF_MEMORY
- XR_ERROR_LIMIT_REACHED
- XR_ERROR_POSE_INVALID
- XR_ERROR_TIME_INVALID
- XR_ERROR_FEATURE_UNSUPPORTED
The XrRaycastInfoANDROID structure is defined as:
typedef struct XrRaycastInfoANDROID {
    XrStructureType                     type;
    void*                               next;
    uint32_t                            maxResults;
    uint32_t                            trackerCount;
    const XrTrackableTrackerANDROID*    trackers;
    XrVector3f                          origin;
    XrVector3f                          trajectory;
    XrSpace                             space;
    XrTime                              time;
} XrRaycastInfoANDROID;
Member Descriptions
- typeis the XrStructureType of this structure.
- nextis- NULLor a pointer to the next structure in a structure chain. No such structures are defined in core OpenXR or this extension.
- maxResultsis the- uint32_tmaximum number of results to return.
- trackerCountis the- uint32_tcount of the- trackersarray.
- trackersis the array of XrTrackableTrackerANDROID that the casted ray should be tested against.
- originis the XrVector3f that the ray is cast from.
- trajectoryis the XrVector3f that the ray is targeted at.
- spaceis the XrSpace that the ray is cast in.
- timeis the- XrTimethe ray is cast at.
The XrRaycastInfoANDROID structure describes the ray to cast.
- The XrRaycastInfoANDROID::trackers array may contain trackers of different types.
- The XrRaycastInfoANDROID::trackers array must not contain multiple
trackers of the same type, otherwise the runtime must return
XR_ERROR_VALIDATION_FAILURE.
Valid Usage (Implicit)
- The XR_ANDROID_raycastextension must be enabled prior to using XrRaycastInfoANDROID
- typemust be- XR_TYPE_RAYCAST_INFO_ANDROID
- nextmust be- NULLor a valid pointer to the next structure in a structure chain
- trackersmust be a pointer to an array of- trackerCountvalid XrTrackableTrackerANDROID handles
- spacemust be a valid XrSpace handle
- The trackerCountparameter must be greater than0
- Both of spaceand the elements oftrackersmust have been created, allocated, or retrieved from the same XrSession
The XrRaycastHitResultsANDROID structure is defined as:
typedef struct XrRaycastHitResultsANDROID {
    XrStructureType               type;
    void*                         next;
    uint32_t                      resultsCapacityInput;
    uint32_t                      resultsCountOutput;
    XrRaycastHitResultANDROID*    results;
} XrRaycastHitResultsANDROID;
Member Descriptions
- typeis the XrStructureType of this structure.
- nextis- NULLor a pointer to the next structure in a structure chain. No such structures are defined in core OpenXR or this extension.
- resultsCapacityInputis the capacity of the- resultsarray, or 0 to indicate a request to retrieve the required capacity.
- resultsCountOutputis a pointer to the count of- resultswritten, or a pointer to the required capacity in the case that- resultsCapacityInputis insufficient.
- resultsis a pointer to an array of XrRaycastHitResultANDROID structures. It can be- NULLif- resultsCapacityInputis 0.
- See the Buffer Size Parameters section for a detailed description of
retrieving the required resultssize.
The XrRaycastHitResultsANDROID contains the array of hits of a raycast.
The runtime must set resultsCountOutput to be less than or equal to
XrRaycastInfoANDROID::maxResults.
Valid Usage (Implicit)
- The XR_ANDROID_raycastextension must be enabled prior to using XrRaycastHitResultsANDROID
- typemust be- XR_TYPE_RAYCAST_HIT_RESULTS_ANDROID
- nextmust be- NULLor a valid pointer to the next structure in a structure chain
- resultsmust be a pointer to an array of- resultsCapacityInputXrRaycastHitResultANDROID-based structures
- The resultsCapacityInputparameter must be greater than0
The XrRaycastHitResultANDROID structure is defined as:
typedef struct XrRaycastHitResultANDROID {
    XrTrackableTypeANDROID    type;
    XrTrackableANDROID        trackable;
    XrPosef                   pose;
} XrRaycastHitResultANDROID;
Member Descriptions
- typeis the XrTrackableTypeANDROID of the trackable that the raycast hit.
- trackableis the- XrTrackableANDROIDthat the raycast hit, or- XR_NULL_TRACKABLE_ANDROIDif the trackable- typewas- XR_TRACKABLE_TYPE_DEPTH_ANDROID.
- poseis the XrPosef that the raycast hit.
The XrRaycastHitResultANDROID contains the details of a raycast hit.
The XrRaycastHitResultANDROID::pose for a plane hit must be such that X and Z are parallel to the plane, and the Y axis is normal to the plane.
| Type of trackable hit | Description | 
| 
 | Hits horizontal and/or vertical surfaces to determine a point's correct depth and orientation. | 
| 
 | Uses depth information from the entire scene to determine a point's correct depth and orientation. | 
Valid Usage (Implicit)
- The XR_ANDROID_raycastextension must be enabled prior to using XrRaycastHitResultANDROID
- typemust be a valid XrTrackableTypeANDROID value
Example code for raycasting
The following example code demonstrates how to perform raycasts.
XrSession session; // previously initialized
XrTime updateTime; // previously initialized
XrSpace appSpace;  // space created for XR_REFERENCE_SPACE_TYPE_LOCAL.
XrPosef headPose;  // latest pose of the HMD.
XrTrackableTrackerANDROID planeTracker; // tracker for plane trackables.
XrTrackableTrackerANDROID depthTracker; // tracker for depth trackables.
// Perform a raycast against multiple trackers.
XrTrackableTrackerANDROID trackers[] = {
  &planeTracker,
  &depthTracker,
};
XrRaycastInfoANDROID rayInfo = {XR_TYPE_RAYCAST_INFO_ANDROID};
rayInfo.trackerCount = sizeof(trackers) / sizeof(XrTrackableTrackerANDROID);
rayInfo.trackers = trackers;
rayInfo.origin = headPose.position;
rayInfo.trajectory = CalculateForwardDirectionFromHeadPose(headPose);
rayInfo.space = appSpace;
rayInfo.time = updateTime;
uint32_t totalHitResults = 0;
constexpr uint32 NUM_DESIRED_RESULTS = 2;
XrRaycastHitResultANDROID hitResult[NUM_DESIRED_RESULTS];
XrRaycastHitResultsANDROID hitResults = {XR_TYPE_RAYCAST_HIT_RESULTS_ANDROID};
hitResults.maxResults = NUM_DESIRED_RESULTS;
hitResults.resultsCapacityInput = NUM_DESIRED_RESULTS;
hitResults.results = hitResult;
XrResult result = xrRaycastANDROID(session, &rayInfo, &hitResults);
if (result == XR_SUCCESS && hitResults.resultsCountOutput >= 1) {
  // Hit results are returned in closest-to-farthest order in
  // hitResults.results[0] .. hitResults.results[hitResults.resultsCountOutput - 1]
}
New Enum Constants
XrStructureType enumeration is extended with:
- XR_TYPE_RAYCAST_INFO_ANDROID
- XR_TYPE_RAYCAST_HIT_RESULTS_ANDROID
New Structures
New Functions
Issues
Version History
- Revision 1, 2024-10-02 (Kenny Vercaemer)
- Initial extension description
 
OpenXR™ and the OpenXR logo are trademarks owned by The Khronos Group Inc. and are registered as a trademark in China, the European Union, Japan and the United Kingdom.
