I tried using the combination of add_raycast_sequence/remove_raycast_sequence, submit_ray_to_raycast_sequence, get_latest_result_from_raycast_sequence to do multiple raycasts, but it always crashes, as shown below
The code used is as follows,provided by GPT
import omni.kit.raycast.query
from pxr import Gf, UsdGeom
import omni.usd
# Acquire the raycast query interface.
raycast = omni.kit.raycast.query.acquire_raycast_query_interface()
# Set up your scene (for example, add a cube in the scene).
stage = omni.usd.get_context().get_stage()
cube_path = "/Cube"
cube = UsdGeom.Cube.Define(stage, cube_path)
UsdGeom.XformCommonAPI(cube.GetPrim()).SetTranslate(Gf.Vec3d(123.45, 0, 0))
# Define a list of rays (each with its origin and direction).
rays = [
omni.kit.raycast.query.Ray((1000, 0, 0), (-1, 0, 0)),
omni.kit.raycast.query.Ray((1000, 10, 0), (-1, -0.1, 0)),
omni.kit.raycast.query.Ray((1000, -10, 0), (-1, 0.1, 0))
]
# (Assume that add_raycast_sequence is used to start the sequence of raycasts)
# Add a raycast sequence – typically the API provides a way to track such sequences.
sequence_id = raycast.add_raycast_sequence() # **The code always starts crashing here**
# Submit each ray to the sequence.
for ray in rays:
raycast.submit_ray_to_raycast_sequence(sequence_id, ray)
# Later in your update loop, poll for the results:
results = raycast.get_latest_result_from_raycast_sequence(sequence_id)
for result in results:
if result.valid:
print("Hit at:", Gf.Vec3d(*result.hit_position))
else:
print("No hit detected for one of the rays.")
# Optionally, when the sequence is no longer needed, remove it.
raycast.remove_raycast_sequence(sequence_id)
Another question I have is, if I have many rays, does add_raycast_sequence and repeatedly calling submit_raycast_query have the same computation speed?