Skip to content

Ray Test ​

A ray test is a collision check between the world and a ray. Users specify the starting point, direction, and length. It returns the closest hit along the ray (if any).

Arguments ​

Single-ray test (rayTest) ​

Signature (C++):

cpp
const RayCollisionList& rayTest(const Eigen::Ref<const Eigen::Vector3d, Eigen::Unaligned>& start,
                                const Eigen::Ref<const Eigen::Vector3d, Eigen::Unaligned>& direction,
                                double length,
                                size_t objectId = size_t(-10),
                                size_t localId = size_t(-10),
                                CollisionGroup collisionMask = CollisionGroup(-1));

Overloads accept {x, y, z} initializer lists for start, direction, or both. Pass a raisim::Vec<3> as vec.e().

  • start: ray origin in world frame.
  • direction: ray direction in world frame. It is normalized internally, so it does not need to be a unit vector; a zero vector is treated as +z.
  • length: ray length in meters. Only hits within [0, length] are returned; a non-positive length returns no hit.
  • objectId / localId: optional self-filter. Collisions with the body whose object world index (Object::getIndexInWorld()) and local body index match are ignored (useful for sensors attached to robots). Both must be set for the filter to take effect; the defaults disable it.
  • collisionMask: collision mask to filter which groups the ray can hit. The default (-1) allows all groups.

The returned RayCollisionList stores only the closest hit (0 or 1 item).

Example ​

The ray test itself is a single call:

cpp
auto& col = world.rayTest({0,0,5}, direction, 50.);

The ray_casting example sweeps this ray over the ground and visualizes the hit point through RaisimServer.

Getting the hit object (example) ​

To identify what was hit, check the returned RayCollisionList.

cpp
const auto& hits = world.rayTest({0, 0, 5}, direction, 50.0);
if (hits.size() == 0) {
  std::cout << "No hit\n";
} else {
  const auto& hit = hits[0];
  const auto* obj = hit.getObject();
  if (obj) {
    std::cout << "Hit object: " << obj->getName()
              << " (id=" << obj->getIndexInWorld() << ")\n";
  }
  std::cout << "Hit position: " << hit.getPosition().transpose() << "\n";
  const auto body = hit.getCollisionBody();
  if (body) {
    std::cout << "Collision body: " << body->name << "\n";
  }
}

Batch ray tests (lidar) ​

For spinning lidar-style scans, World::rayTestLidar(...) generates a rectangular yaw/pitch sampling pattern and performs the scan in one call.

Signature (C++):

cpp
void rayTestLidar(
    const Mat<3, 3>& rot,
    const Vec<3>& pos,
    double yawStartAngle,
    double yawIncrement,
    size_t yawCount,
    double pitchStartAngle,
    double pitchIncrement,
    size_t pitchCount,
    double rangeMin,
    double rangeMax,
    size_t objectId,
    size_t localId,
    CollisionGroup collisionMask,
    std::vector<Vec<3>, AlignedAllocator<Vec<3>, 32>>& scan);

Both angular axes use the same start angle, increment, count convention. For sample indices y and p, the angles are

yaw(y)=yawStartAngle+yyawIncrementpitch(p)=pitchStartAngle+ppitchIncrement

where 0≤y<yawCount and 0≤p<pitchCount. Increments are signed: use a negative increment to scan an axis in the decreasing-angle direction. A single-sample axis normally uses an increment of zero. If either count is zero, an angle is not finite, or rangeMax <= rangeMin, scan is cleared and no rays are cast.

Each ray direction in the sensor frame is

d=(cos⁡pitchcos⁡yaw,cos⁡pitchsin⁡yaw,sin⁡pitch),

so yaw rotates about the sensor z axis starting from the sensor x axis, and a positive pitch tilts the ray toward the sensor +z axis.

Minimal example for a complete yaw revolution with 16 pitch channels (rot, pos, rangeMin, rangeMax, objectId, localId, and collisionMask come from the application):

cpp
constexpr double pi = 3.14159265358979323846;
constexpr size_t yawCount = 1024;
constexpr size_t pitchCount = 16;
constexpr double yawStartAngle = -pi;
constexpr double yawIncrement = 2.0 * pi / double(yawCount);
constexpr double pitchStartAngle = -15.0 * pi / 180.0;
constexpr double pitchEndAngle = 15.0 * pi / 180.0;
constexpr double pitchIncrement =
    (pitchEndAngle - pitchStartAngle) / double(pitchCount - 1);

std::vector<raisim::Vec<3>, raisim::AlignedAllocator<raisim::Vec<3>, 32>> scan;
world.rayTestLidar(rot, pos,
                   yawStartAngle, yawIncrement, yawCount,
                   pitchStartAngle, pitchIncrement, pitchCount,
                   rangeMin, rangeMax,
                   objectId, localId, collisionMask,
                   scan);

Yaw is the outer loop and pitch is the inner loop. Consequently, rays are generated in this order:

text
(yaw[0], pitch[0]), (yaw[0], pitch[1]), ...,
(yaw[1], pitch[0]), (yaw[1], pitch[1]), ...

The output scan contains hit points in the sensor frame. It is compact: only rays that hit contribute a point, so a miss does not create a placeholder entry and indices after a miss no longer map directly to angular sample indices.

Arguments (batched lidar scan) ​

  • rot: sensor orientation (sensor frame to world frame).
  • pos: sensor position in world frame.
  • yawStartAngle, yawIncrement, yawCount: first yaw angle, signed yaw step, and number of yaw samples.
  • pitchStartAngle, pitchIncrement, pitchCount: first pitch angle, signed pitch step, and number of pitch samples.
  • rangeMin, rangeMax: minimum and maximum sensor-relative range in meters. Each collision ray starts at rangeMin and has length rangeMax - rangeMin.
  • objectId / localId: optional self-filter (same as rayTest).
  • collisionMask: collision mask to filter which groups the rays can hit.
  • scan: output hit positions in the sensor frame.

Performance notes ​

rayTestLidar is optimized for repeated structured scans. It caches the sensor-frame directions and their normalized forms for an unchanged yaw/pitch pattern. In a sufficiently large, spatially sparse scene it also builds conservative angular candidate buckets, so each ray normally checks only nearby bodies. The buckets are reused while the scene, sensor pose, ranges, self-filter, and collision mask are unchanged; changes to any of those inputs invalidate the relevant cache. Bucket bounds are conservative and every retained candidate still goes through an exact shape intersection. Sphere candidates use the same quadratic roots and range rules as the contact engine but omit the unused contact-normal calculation. No path approximates hit positions.

Dense or unsupported angular configurations automatically use the exact ray BVH instead. Small scenes use a scan-wide conservative cull and a compact flat candidate list. These fallbacks preserve the same closest-hit and compact-output semantics.

Use the public ray_scan_lidar example to inspect structured scans and large_scale_ray_test to inspect repeated ray queries. Build them together with the TCP viewer and start the viewer:

bash
cmake --build build-examples --target ray_scan_lidar large_scale_ray_test rayrai_tcp_viewer --parallel 12
./build-examples/examples/rayrai_tcp_viewer

Then start either simulation (for example, ./build-examples/examples/ray_scan_lidar) in another terminal. These examples visualize the APIs; they are not timing comparisons. For application benchmarks, time World::rayTestLidar and repeated World::rayTest calls with identical sensor poses, rays, ranges, and collision filters on one thread, and verify the hit positions before comparing timings. Relative speed depends on scene layout, motion, filters, and scan shape.

Convex collision queries ​

The following World queries use the current scene without advancing physics. Call between complete integration steps. They, like rayTest() between steps, see every rigid body where it is now: where the last step left it or where a setter such as setGeneralizedCoordinate() or setPosition() put it. Querying does not change what the next step collides with.

MethodResult
queryProximityClosest collider within the inclusive maximum distance
queryOverlapOne hit per overlapping collider, ordered by object ID, local body index and collider index
checkOverlapBoolean result with early exit
sweepShapeEarliest hit along a translation at fixed orientation

Construct a CollisionQueryShape with sphere(radius), capsule(radius, halfAxisLength), box(halfExtents), cylinder(radius, halfHeight), or convexHull(vertices). Capsules and cylinders use their local Z axis. Hull vertices are in the shape's local frame. All positions, translations, and lengths are in meters. The optional orientation is a 3×3 rotation matrix and defaults to identity.

cpp
const auto probe = raisim::CollisionQueryShape::capsule(0.012, 0.04);
const raisim::Vec<3> position{0.30, 0.10, 0.18};
const auto near = world.queryProximity(probe, position, 0.02);
const auto hit = world.sweepShape(probe, position, {0.0, 0.0, -0.10});
python
import numpy as np
import raisimpy as r

probe = r.CollisionQueryShape.capsule(0.012, 0.04)
position = np.array([0.30, 0.10, 0.18])
near = world.queryProximity(probe, position, 0.02)
hit = world.sweepShape(probe, position, np.array([0.0, 0.0, -0.10]))

A proximity or sweep miss returns std::nullopt in C++ and None in Python. Hits carry the stable object ID, local body and collider indexes, distance, world-frame witness points, and a normal directed from the collider toward the probe when defined. Triangle hits include the triangle index. Solid colliders use CollisionQueryHit::kNoTriangle in C++ and None in Python. A sweep fraction of zero indicates contact at the starting pose.

Primitive and convex colliders represent solids, and ground planes represent half-spaces. Original triangle meshes and heightmaps represent two-sided surfaces. Voxel maps represent the solid union of their occupied voxels: a probe anywhere inside it overlaps the map with distance and sweep fraction zero, and each voxel map reports at most one hit. An overlap has zero distance. Distances within the numerical contact tolerance of 1e-9 m are treated as contact. For meshes with many triangles use a finite proximity limit so distant nodes can be culled.

CollisionQueryFilter.mask selects target groups and group selects the query's group bits for each target's mask. Both default to all bits. ignoredObject excludes a stable object ID. Set ignoredLocalBody as well to limit that exclusion to one body of the object.

Mesh clearance ​

World::isMeshWithinClearance checks whether a sphere or capsule is within an inclusive clearance threshold of a rigid triangle mesh. World::meshClearance returns the clearance and closest points on the capsule and mesh in world coordinates, together with the capsule axis point used to compute them. When the capsule meets the mesh surface, both shapes share the same witness. The hit identifies the stable object ID, local body index, collider index within the object, and triangle index within that collider. A miss returns std::nullopt in C++ and None in Python.

cpp
const raisim::Vec<3> a{0.30, 0.10, 0.18}, b{0.30, 0.10, 0.26};
const bool blocked = world.isMeshWithinClearance(a, b, 0.012, 0.005);
auto closest = world.meshClearance(a, b, 0.012, 0.020);
python
import numpy as np

a = np.array([0.30, 0.10, 0.18])
b = np.array([0.30, 0.10, 0.26])
blocked = world.isMeshWithinClearance(a, b, 0.012, 0.005)
closest = world.meshClearance(a, b, 0.012, 0.020)

Coordinates, radius, and clearance are in meters. Equal endpoints query a sphere. A zero radius queries a line segment or point. The optional collisionMask selects collision groups and ignoredObject skips all colliders owned by the supplied stable object ID. Mesh colliders with a zero collision mask are skipped.

Clearance is the axis-to-triangle distance minus the radius, clamped to zero when the capsule touches or intersects the surface. Triangles are two-sided, and the query supports original concave meshes and convex mesh colliders. A shape entirely inside a closed mesh and clear of its triangles can have positive clearance. Primitive colliders and heightmaps are outside the scope of this API.

Use the boolean call for checks that only need a threshold result. It exits as soon as it finds a qualifying triangle. The nearest-point call tightens its search radius as it finds closer triangles. Both use the existing triangle BVHs and leave the current physics state intact.

Details ​

RayCollisionItem summary ​

Each hit entry stores:

  • getObject(): pointer to the hit raisim::Object (or nullptr if none).
  • getPosition(): world-frame hit position as an Eigen::Vector3d.
  • getCollisionBody(): pointer to the collision body that was hit (may be null for some objects).

RayCollisionList summary ​

Container semantics:

  • size(): number of valid hits in the list.
  • operator[](i): random access to the i-th hit.
  • begin(), end(): iterators over valid hits.
  • back(): iterator to the last valid hit.
  • setSize(n): updates the number of valid hits (used internally).
  • resize(n), reserve(n): storage management.

Notes ​

  • RayCollisionList is a lightweight container owned by the world and reused across calls. Every rayTest(...) call overwrites it, so copy any hit you need before the next ray test.
  • It contains at most one item because RaiSim keeps only the closest hit along the ray. Use list.size() to check whether a hit occurred (0 or 1).

API ​

RayCollisionItem ​

RayCollisionList ​

RaiSim — physics for robotics and AI.