public bool Intersects(WorldObjectModel physicalModel) { Plane p = new Plane(Vector3.up, 0); float enter; bool intersects = p.Raycast(WorldSpaceRay, out enter); if (intersects) { Vector3 entryPoint = TurtleUtils.WorldSpacePointToCafeCoordinates(WorldSpaceRay.GetPoint(enter)); intersects = intersects && Math.Abs(entryPoint.x - physicalModel.Position.x) < physicalModel.Width / 2f; intersects = intersects && Math.Abs(entryPoint.y - physicalModel.Position.y) < physicalModel.Height / 2f; } return intersects; }
public bool Intersects(WorldObjectModel physicalModel) { Plane p = new Plane(Vector3.up, 0); float enter; bool intersects = p.Raycast(WorldSpaceRay, out enter); if (intersects) { Vector3 entryPoint = TurtleUtils.WorldSpacePointToCafeCoordinates(WorldSpaceRay.GetPoint(enter)); intersects = intersects && Math.Abs(entryPoint.x - physicalModel.Position.x) < physicalModel.Width / 2f; intersects = intersects && Math.Abs(entryPoint.y - physicalModel.Position.y) < physicalModel.Height / 2f; } return(intersects); }
public bool CanBePlacedAt(WorldObjectModel model, Vector2 position) { int modelX = (int)Math.Round(model.Position.x); int modelY = (int)Math.Round(model.Position.y); for (int i = modelX - model.Width / 2; i < modelX + model.Width / 2; i++) { for (int j = modelY - model.Height / 2; j < modelY + model.Height / 2; i++) { if (GetModelAtLocation(new Vector2(i, j)) != null) { return false; } } } return true; }
public bool CanBePlacedAt(WorldObjectModel model, Vector2 position) { int modelX = (int)Math.Round(model.Position.x); int modelY = (int)Math.Round(model.Position.y); for (int i = modelX - model.Width / 2; i < modelX + model.Width / 2; i++) { for (int j = modelY - model.Height / 2; j < modelY + model.Height / 2; i++) { if (GetModelAtLocation(new Vector2(i, j)) != null) { return(false); } } } return(true); }
/// <summary> /// Gets an adjacent space for a model to move to in order to reach a given target. /// </summary> /// <param name="model">The model for which the next space should be found.</param> /// <param name="target">The model's intended destination.</param> /// <param name="ignoreLayers">Layers that the object ignores when pathfinding.</param> /// <returns>An adjacent space if the model has a path to its target, or the model's /// original space if there is no such path.</returns> public Vector2 Pathfind(WorldObjectModel model, WorldObjectModel destination) { if (!graphValid) { RebuildPathfindingGraph(); } Vector2 current = model.Position.RoundComponents(); Vector2 target = destination.Position.RoundComponents(); if (current == target) { // what are we doing return target; } // temp var List<RoomGraphNode> nodesToAdd = new List<RoomGraphNode>(4); Dictionary<RoomGraphNode, RoomGraphNode> prevPointers = new Dictionary<RoomGraphNode, RoomGraphNode>(); Queue<RoomGraphNode> queue = new Queue<RoomGraphNode>(); // initial population prevPointers[emptySpaceGraph[current]] = null; queue.Enqueue(emptySpaceGraph[current]); while (queue.Count > 0) { RoomGraphNode currentNode = queue.Dequeue(); // Check if we are at the target if (currentNode.Position == target) { // we found it while (prevPointers[currentNode].Position != current) { currentNode = prevPointers[currentNode]; } return currentNode.Position; } // TODO: Pathfinding currently ignores size completely. (Is this OK?) // Get current node's neighbors nodesToAdd.Clear(); foreach (var adjacentNode in currentNode.AdjacencyList.GetElementsInRandomOrder()) { if (!prevPointers.ContainsKey(adjacentNode) && (adjacentNode.ModelAtNode == null || currentNode.ModelAtNode == adjacentNode.ModelAtNode || adjacentNode.ModelAtNode == destination)) { nodesToAdd.Add(adjacentNode); } } // If current node is original, we want to favor the direction that the object's currently facing if (currentNode.Position == current) { for (int i = 0; i < nodesToAdd.Count; i++) { if (nodesToAdd[i].Position - current == model.Direction) { nodesToAdd.Swap(0, i); break; } } } // Add valid neighbors to queue foreach (var nextNode in nodesToAdd) { prevPointers[nextNode] = currentNode; queue.Enqueue(nextNode); } } // couldn't find it Debug.Log("Couldn't find a path from " + current + " to " + target); return current; }
/// <summary> /// Gets an adjacent space for a model to move to in order to reach a given target. /// </summary> /// <param name="model">The model for which the next space should be found.</param> /// <param name="target">The model's intended destination.</param> /// <param name="ignoreLayers">Layers that the object ignores when pathfinding.</param> /// <returns>An adjacent space if the model has a path to its target, or the model's /// original space if there is no such path.</returns> public Vector2 Pathfind(WorldObjectModel model, WorldObjectModel destination) { if (!graphValid) { RebuildPathfindingGraph(); } Vector2 current = model.Position.RoundComponents(); Vector2 target = destination.Position.RoundComponents(); if (current == target) { // what are we doing return(target); } // temp var List <RoomGraphNode> nodesToAdd = new List <RoomGraphNode>(4); Dictionary <RoomGraphNode, RoomGraphNode> prevPointers = new Dictionary <RoomGraphNode, RoomGraphNode>(); Queue <RoomGraphNode> queue = new Queue <RoomGraphNode>(); // initial population prevPointers[emptySpaceGraph[current]] = null; queue.Enqueue(emptySpaceGraph[current]); while (queue.Count > 0) { RoomGraphNode currentNode = queue.Dequeue(); // Check if we are at the target if (currentNode.Position == target) { // we found it while (prevPointers[currentNode].Position != current) { currentNode = prevPointers[currentNode]; } return(currentNode.Position); } // TODO: Pathfinding currently ignores size completely. (Is this OK?) // Get current node's neighbors nodesToAdd.Clear(); foreach (var adjacentNode in currentNode.AdjacencyList.GetElementsInRandomOrder()) { if (!prevPointers.ContainsKey(adjacentNode) && (adjacentNode.ModelAtNode == null || currentNode.ModelAtNode == adjacentNode.ModelAtNode || adjacentNode.ModelAtNode == destination)) { nodesToAdd.Add(adjacentNode); } } // If current node is original, we want to favor the direction that the object's currently facing if (currentNode.Position == current) { for (int i = 0; i < nodesToAdd.Count; i++) { if (nodesToAdd[i].Position - current == model.Direction) { nodesToAdd.Swap(0, i); break; } } } // Add valid neighbors to queue foreach (var nextNode in nodesToAdd) { prevPointers[nextNode] = currentNode; queue.Enqueue(nextNode); } } // couldn't find it Debug.Log("Couldn't find a path from " + current + " to " + target); return(current); }