Specialized components
Input and interaction system
Input system
14 min
overview the input system serves as the central coordinator for all input processing in the ir engine it manages the flow of information from hardware devices to game entities, ensuring that inputs are properly captured, processed, and routed to the appropriate receivers clientinputsystem the core component of the input system is the clientinputsystem , an entity component system (ecs) system that executes once per frame to handle all client side input processing this system performs several critical functions input source updates refreshes all input sources with the latest data from hardware devices target detection determines which entities in the scene are being targeted by input devices source receiver connection links input sources to the appropriate input receivers state management ensures input states are properly maintained between frames processing pipeline the clientinputsystem follows a specific sequence of operations during each frame 1\ update input sources first, the system refreshes all input sources with the latest hardware data xr controllers updates position and rotation from the xr session mouse/screen pointers calculates raycasts from the current cursor position through the camera button and axis states reads the current state of all buttons and analog inputs 2\ determine targets once input sources are updated, the system identifies which entities are being targeted uses raycasting for pointer based inputs (mouse, xr controllers) may use proximity detection for non pointer inputs stores intersection results in the input source components 3\ connect sources to receivers after identifying targets, the system links input sources to the appropriate receivers updates the inputsources array in each target entity's inputcomponent handles input focus and capture for entities that need exclusive input control ensures that input receivers have access to the relevant input sources 4\ prepare for next frame finally, the system performs cleanup operations to prepare for the next frame resets transient states like down and up flags clears consumed input markers handles any pending input focus changes implementation details the clientinputsystem is implemented in systems/clientinputsystem tsx here's a simplified representation of its main execution function // simplified conceptual structure from systems/clientinputsystem tsx const execute = () => { // 1 update xr input source transforms (e g , vr controllers) for (const eid of xrspacesquery()) { // update transform from xr pose } // 2 update pointer based input source raycasters (e g , mouse) for (const eid of pointersquery()) { // update raycaster from camera and pointer position // update transform of the pointer entity } // 3 update gamepad button/axis states in inputsourcecomponents for (const sourceeid of inputsourcequery()) { // read hardware gamepad/controller states clientinputfunctions updategamepadinput(sourceeid); } // 4 determine targets and assign inputsourcecomponents to inputcomponents const capturedentity = getentitycapturinginput(); for (const sourceeid of inputsourcequery()) { // find targets and connect sources to receivers clientinputfunctions assigninputsources(sourceeid, capturedentity); } }; helper functions the clientinputsystem relies on several helper functions from functions/clientinputfunctions ts updategamepadinput // simplified from functions/clientinputfunctions ts export function updategamepadinput(entity entity) { const inputsource = getcomponent(entity, inputsourcecomponent); const gamepad = inputsource source gamepad; if (!gamepad) return; // for each button on the gamepad for (let i = 0; i < gamepad buttons length; i++) { const hardwarebutton = gamepad buttons\[i]; const buttonstate = inputsource buttons\[i]; // update buttonstate based on hardware state // set pressed, down, value properties } // update axis values similarly } assigninputsources // simplified from functions/clientinputfunctions ts export function assigninputsources(sourceentity entity, capturedentity entity) { // find entities intersected by the source's raycaster const sortedintersections = findraycastedinput(sourceentity); // create list of input sources for targets to listen to const finalinputsources = \[sourceentity, globalsources]; // if an entity has captured input, only it receives the input if (capturedentity !== undefinedentity) { setinputsources(capturedentity, finalinputsources); } else { // otherwise, assign to all intersected entities for (const intersection of sortedintersections) { setinputsources(intersection entity, finalinputsources); } } } setinputsources // simplified from functions/clientinputfunctions ts export function setinputsources(targetentity entity, sourcestoadd entity\[]) { // find the inputcomponent on the target entity const inputcomponent = getmutablecomponent(targetentity, inputcomponent); // add the source entities to the inputsources list if (inputcomponent) { inputcomponent inputsources merge(sourcestoadd); } } input processing flow example the following sequence diagram illustrates how the input system processes a mouse click on an interactive object sequencediagram participant user participant hardware as mouse hardware participant os as browser/os participant cis as clientinputsystem participant mouseisc as mouse inputsourcecomponent participant targetic as target's inputcomponent user >>hardware clicks left button hardware >>os "left mouse button down" event os >>cis raw input event cis >>mouseisc update raycaster cis >>mouseisc update button state (primaryclick down = true) cis >>cis raycast from mouseisc, hits target entity cis >>targetic add mouseisc to inputsources list note right of targetic target's game logic can now detect\<br/>the click via inputcomponent getbuttons() cis >>cis (next frame) reset mouseisc primaryclick down system integration the input system integrates with several other components of the engine camera system provides the viewpoint for raycasting transform system updates the spatial position of input devices physics system used for raycasting and collision detection entity component system provides the framework for component queries and updates next steps with an understanding of how the input system coordinates the overall input processing pipeline, the next chapter explores the specific techniques used to determine which entities are being targeted by input devices next input target detection docid 0rcidl kz4rdefjmr2nw