Specialized components
Input and interaction system
Input target detection
12 min
overview input target detection is the process by which the ir engine determines which entities in a 3d scene are being targeted by input devices this critical subsystem enables the engine to identify the objects that users are attempting to interact with, whether through pointing, proximity, or other detection methods detection strategies the engine employs several detection strategies, or heuristics, to identify potential interaction targets these strategies are implemented in the clientinputheuristics module and provide different approaches to target identification raycasting against meshes this strategy uses precise geometric intersection testing against detailed 3d models implementation projects an invisible ray from the input device and tests for intersections with the triangular meshes of scene objects accuracy high precision, as it detects intersections with the actual visible geometry performance more computationally intensive, especially with complex meshes use cases precise targeting in detailed environments, interactive ui elements // simplified concept from functions/clientinputheuristics ts function meshheuristic( viewerentity entity, intersectiondata set\<intersectiondata>, pointerposition vector3, pointerdirection vector3 ) { raycaster set(pointerposition, pointerdirection); const interactableobjects = getinteractableobjects(); const hits = raycaster intersectobjects(interactableobjects, true); for (const hit of hits) { intersectiondata add({ entity hit object entity!, distance hit distance }); } } raycasting against bounding boxes this strategy uses simplified collision volumes for faster intersection testing implementation tests ray intersections against axis aligned bounding boxes that encompass objects accuracy lower precision than mesh raycasting, but sufficient for many interactions performance significantly faster than mesh raycasting use cases performance critical scenarios, objects with simple shapes, initial filtering // simplified concept from functions/clientinputheuristics ts function boundingboxheuristic( viewerentity entity, intersectiondata set\<intersectiondata>, pointerposition vector3, pointerdirection vector3 ) { ray origin copy(pointerposition); ray direction copy(pointerdirection); const entitieswithboundingboxes = getentitieswithboundingboxes(); for (const entity of entitieswithboundingboxes) { const boundingbox = getoptionalcomponent(entity, boundingboxcomponent); if (!boundingbox) continue; const didhit = ray intersectbox(boundingbox box, hittarget); if (didhit) { intersectiondata add({ entity, distance ray origin distanceto(hittarget) }); } } } proximity detection this strategy identifies targets based on spatial proximity rather than direct pointing implementation measures the distance between input devices and potential targets accuracy depends on the activation distance threshold defined for each target performance generally efficient, especially with spatial partitioning use cases vr hand interactions, touch like interfaces, area of effect selections // simplified concept from functions/clientinputheuristics ts function findproximity( isspatialinput boolean, sourceentity entity, sortedintersections intersectiondata\[], intersectiondata set\<intersectiondata> ) { transformcomponent getworldposition(sourceentity, worldposinputsource); const potentialtargets = getentitieswithinputcomponent(); for (const targetentity of potentialtargets) { if (targetentity === selfavatarentity) continue; const inputcomponent = getcomponent(targetentity, inputcomponent); transformcomponent getworldposition(targetentity, worldpostarget); const distsquared = worldposinputsource distancetosquared( worldpostarget); const activationdistsquared = inputcomponent activationdistance inputcomponent activationdistance; if (activationdistsquared > distsquared) { intersectiondata add({ entity targetentity, distance math sqrt(distsquared) }); } } } heuristic coordination the engine uses a coordinated approach to manage these detection strategies heuristic registration and prioritization heuristics are registered with the inputheuristicstate , which maintains an ordered list of detection strategies this ordering allows certain heuristics to take precedence over others, enabling scenarios such as ui elements having priority over world objects editor tools taking precedence over game interactions custom interaction systems overriding default behaviors the findraycastedinput function the findraycastedinput function serves as the primary coordinator for ray based heuristics // simplified concept from functions/clientinputheuristics ts export function findraycastedinput( sourceeid entity, intersectiondata set\<intersectiondata> ) { // get the pointer's current world position and direction transformcomponent getworldrotation(sourceeid, sourcerotation); direction copy(objectdirection forward) applyquaternion(sourcerotation); transformcomponent getworldposition(sourceeid, position); // get the registered heuristic functions const heuristics = getstate(inputheuristicstate); // get the viewer entity associated with this input source const viewerentity = getcomponent(sourceeid, inputsourcecomponent) sourceentity; if (!viewerentity) return; // run each heuristic in priority order for (const h of heuristics) { h heuristic(viewerentity, intersectiondata, position, direction); } } integration with the input system the target detection process is integrated with the broader input system through the following workflow input source update the clientinputsystem updates the position and orientation of input devices heuristic execution the system calls findraycastedinput and other detection functions result collection detection results are stored in the intersections field of the inputsourcecomponent target selection the system selects the most appropriate target (typically the closest valid intersection) input routing the selected target's inputcomponent is updated to include the relevant input source sequencediagram participant user participant inputdevice as input device participant cis as clientinputsystem participant heuristics as detection heuristics participant target as target entity user >>inputdevice moves/interacts with device inputdevice >>cis raw input data cis >>inputdevice update inputsourcecomponent cis >>heuristics execute detection strategies heuristics >>cis return intersection results cis >>cis select best target cis >>target update inputcomponent inputsources performance considerations target detection can be computationally intensive, particularly in complex scenes the engine employs several optimization strategies hierarchical testing using bounding box tests before more expensive mesh tests spatial partitioning limiting candidate objects based on spatial organization caching reusing previous results when appropriate selective updating only performing detection for active input sources custom heuristics allowing developers to implement specialized, optimized detection for specific scenarios next steps with an understanding of how the engine determines which entities are being targeted by input devices, the next chapter explores how the system manages and visualizes input pointers in the 3d environment next input pointer management docid\ poftbfhhtf wjgjfe2uug