Strata - v1.4.10
    Preparing search index...

    Function findClosestNode

    • Finds the closest node in the graph to a given 3D position.

      Searches all nodes in the graph and returns the ID of the one nearest to the specified position. Essential for converting world positions (like player clicks or spawn points) into graph nodes for pathfinding.

      Parameters

      Returns NodeId | undefined

      Node ID of the closest node, or undefined if graph is empty

      import { findClosestNode } from '@jbcom/strata/core/pathfinding';

      function handleGroundClick(clickEvent) {
      const clickPosition = clickEvent.point; // { x, y, z } from raycaster

      // Find nearest navigation node to the click
      const targetNode = findClosestNode(graph, clickPosition);

      if (targetNode) {
      const agentNode = findClosestNode(graph, agentPosition);
      const path = pathfinder.find(agentNode, targetNode);

      if (path.found) {
      followPath(path.positions);
      }
      }
      }
      import { findClosestNode } from '@jbcom/strata/core/pathfinding';

      function spawnEnemy(spawnPosition) {
      // Snap spawn position to nearest walkable node
      const spawnNode = findClosestNode(graph, spawnPosition);

      if (spawnNode) {
      const nodeData = graph.getNode(spawnNode);
      if (nodeData && nodeData.data.walkable) {
      // Spawn enemy at exact node position
      createEnemy(nodeData.data.position);
      }
      }
      }
      function canPlaceBuilding(buildingPos, minDistance = 5) {
      const nearestNode = findClosestNode(graph, buildingPos);

      if (!nearestNode) return false;

      const node = graph.getNode(nearestNode);
      const distance = calculateDistance(buildingPos, node.data.position);

      // Don't allow placement too far from navigation network
      return distance < minDistance;
      }
      const agents = [
      { pos: { x: 5, y: 0, z: 5 } },
      { pos: { x: 15, y: 0, z: 10 } },
      { pos: { x: 8, y: 0, z: 20 } }
      ];

      const targetPos = { x: 25, y: 0, z: 25 };
      const targetNode = findClosestNode(graph, targetPos);

      agents.forEach(agent => {
      const startNode = findClosestNode(graph, agent.pos);
      const path = pathfinder.find(startNode, targetNode);

      if (path.found) {
      assignPath(agent, path);
      }
      });