private static Tuple3 <Pos, WayReference, Vector3> GetHumanSpawnInfo(long nodeId) { Pos spawnPos = nodes [nodeId]; // Position to spawn/despawn Pos spawnNode; WayReference closestWay = null; Vector3 closestPoint = Vector3.zero; if (NodeIndex.nodeWayWalkPathIndex.ContainsKey(spawnPos.Id)) { // Spawn in a node spawnNode = spawnPos; closestWay = NodeIndex.nodeWayWalkPathIndex [spawnPos.Id] [0]; closestPoint = Game.getCameraPosition(spawnNode); } else { // Calculate which node is closest spawnNode = PosHelper.getClosestNode(spawnPos, NodeIndex.walkNodes); // Pick closest wayReference Vector3 spawnPosVector = Game.getCameraPosition(spawnPos); float closestDistance = float.MaxValue; List <WayReference> ways = NodeIndex.nodeWayIndex [spawnNode.Id]; foreach (WayReference way in ways) { Vector3 wayStart = Game.getCameraPosition(way.node1); Vector3 wayEnd = Game.getCameraPosition(way.node2); Vector3 projectedPoint = Math3d.ProjectPointOnLineSegment(wayStart, wayEnd, spawnPosVector); float distance = PosHelper.getVectorDistance(spawnPosVector, projectedPoint); if (distance < closestDistance) { closestDistance = distance; closestWay = way; closestPoint = projectedPoint; } } // DebugFn.arrow (spawnPosVector, closestPoint); } // DebugFn.square (closestPoint); return(Tuple3.New(spawnNode, closestWay, closestPoint)); }
public static WayReference getClosestWayReference(Vector3 point) { float closestDistance = float.MaxValue; WayReference closestWay = null; List <WayReference> carWays = allWayReferences.FindAll(wayRef => wayRef.way.CarWay); foreach (WayReference way in carWays) { Vector3 wayStart = Game.getCameraPosition(way.node1); Vector3 wayEnd = Game.getCameraPosition(way.node2); Vector3 projectedPoint = Math3d.ProjectPointOnLineSegment(wayStart, wayEnd, point); float distance = PosHelper.getVectorDistance(point, projectedPoint); if (distance < closestDistance) { closestDistance = distance; closestWay = way; } } return(closestWay); }