// // Is a cell intersecting with a rectangle // public static bool IsCellIntersectingWithRectangle(Vector3 cellPos, float cellSize, Rectangle obstacle) { bool isColliding = false; float halfCellSize = cellSize * 0.5f; Vector3 FL = new Vector3(cellPos.x - halfCellSize, cellPos.y, cellPos.z + halfCellSize); Vector3 FR = new Vector3(cellPos.x + halfCellSize, cellPos.y, cellPos.z + halfCellSize); Vector3 BL = new Vector3(cellPos.x - halfCellSize, cellPos.y, cellPos.z - halfCellSize); Vector3 BR = new Vector3(cellPos.x + halfCellSize, cellPos.y, cellPos.z - halfCellSize); Rectangle cell = new Rectangle(FL, FR, BL, BR); //Step 1. AABB if (Intersections.AreIntersectingAABB(obstacle, cell)) { //Step 2. Triangle-triangle intersections if (Intersections.AreRectangleRectangleIntersecting(obstacle, cell)) { isColliding = true; } } return(isColliding); }
// // Methods that returns obstacles that are close to a car, because it's slow to check ALL obstacles // //Method 1 - Search through all obstacles to find which are close within a radius //private static List<Obstacle> FindCloseObstaclesWithinRadius(Vector3 pos, float radiusSqr) //{ // //The list with close obstacles // List<Obstacle> closeObstacles = new List<Obstacle>(); // //Method 1 - Search through all obstacles to find which are close // //The list with all obstacles in the map // List<Obstacle> allObstacles = ObstaclesController.allObstacles; // //Find close obstacles // for (int i = 0; i < allObstacles.Count; i++) // { // float distSqr = (pos - allObstacles[i].centerPos).sqrMagnitude; // //Add to the list of close obstacles if close enough // if (distSqr < radiusSqr) // { // closeObstacles.Add(allObstacles[i]); // } // } // return closeObstacles; //} //Method 2 - Find all obstacles the car might collide with by checking surrounding cells //might not be accurate because we dont alway knows how far from the car we should search //private static List<Obstacle> FindCloseObstaclesCell(Vector3 carPos) //{ // //The list with close obstacles // List<Obstacle> closeObstacles = new List<Obstacle>(); // IntVector2 carCellPos = PathfindingController.ConvertCoordinateToCellPos(carPos); // //Check an area of cells around the car's cell for obstacles // //The car is 5 m long so search 3 m to each side? // int searchArea = 3; // for (int x = -searchArea; x <= searchArea; x++) // { // for (int z = -searchArea; z <= searchArea; z++) // { // IntVector2 cellPos = new IntVector2(carCellPos.x + x, carCellPos.z + z); // //Is this cell within the map? // if (PathfindingController.IsCellWithinGrid(cellPos)) // { // //Add all obstacles from this list to the list of close obstacles // List<Obstacle> obstaclesInCell = ObstaclesController.allObstaclesInEachCell[cellPos.x, cellPos.z]; // if (obstaclesInCell != null) // { // for (int i = 0; i < obstaclesInCell.Count; i++) // { // //Might add the same obstacle more than one time, but maybe that's not a big problem? // closeObstacles.Add(obstaclesInCell[i]); // } // } // } // } // } // return closeObstacles; //} //Method 3 - Find all obstacles the car might collide with by using AABB public static List <Obstacle> FindCloseObstaclesAABB(Rectangle carRect, Map map) { List <Obstacle> closeObstacles = new List <Obstacle>(); //The list with all obstacles in the map List <Obstacle> allObstacles = map.allObstacles; //Find close obstacles for (int i = 0; i < allObstacles.Count; i++) { Rectangle obsRect = allObstacles[i].cornerPos; //Are the AABB intersecting? if (Intersections.AreIntersectingAABB(carRect, obsRect)) { closeObstacles.Add(allObstacles[i]); } } return(closeObstacles); }