# psychopy.tools.mathtools.intersectRayAABB¶

psychopy.tools.mathtools.intersectRayAABB(rayOrig, rayDir, boundsOffset, boundsExtents, dtype=None)[source]

Find the point a ray intersects an axis-aligned bounding box (AABB).

Parameters
• rayOrig (array_like) – Origin of the ray in space [x, y, z].

• rayDir (array_like) – Direction vector of the ray [x, y, z], should be normalized.

• boundsOffset (array_like) – Offset of the bounding box in the scene [x, y, z].

• boundsExtents (array_like) – Minimum and maximum extents of the bounding box.

• dtype (dtype or str, optional) – Data type for computations can either be ‘float32’ or ‘float64’. If out is specified, the data type of out is used and this argument is ignored. If out is not provided, ‘float64’ is used by default.

Returns

Coordinate in world space of the intersection and distance in scene units from rayOrig. Returns None if there is no intersection.

Return type

tuple

Examples

Get the point on an axis-aligned bounding box that the cursor is over and place a 3D stimulus there. The eye location is defined by RigidBodyPose object camera:

# get the mouse position on-screen
mx, my = mouse.getPos()

# find the point which the ray intersects on the box
result = intersectRayAABB(
camera.pos,
camera.transformNormal(win.coordToRay((mx, my))),
myStim.pos,
myStim.thePose.bounds.extents)

# if the ray intersects, set the position of the cursor object to it
if result is not None:
cursorModel.thePose.pos = result[0]
cursorModel.draw()  # don't draw anything if there is no intersect


Note that if the model is rotated, the bounding box may not be aligned anymore with the axes. Use intersectRayOBB if your model rotates.