//Check if the car outside of map (MATT) public static bool TargetPositionWithinTrack(Vector3 carRearWheelPos, float heading, CarData carData) { bool withinTrack = true; //Make the car bigger than it is to be on the safe side float marginOfSafety = 0.5f; float carLength = carData.GetLength() + marginOfSafety; float carWidth = carData.GetWidth() + marginOfSafety; //Find the center pos of the car (carPos is at the rearWheels) float distCenterToRearWheels = carData.GetDistanceToRearWheels(); float xCenter = carRearWheelPos.x + distCenterToRearWheels * Mathf.Sin(heading); float zCenter = carRearWheelPos.z + distCenterToRearWheels * Mathf.Cos(heading); Vector3 carPos = new Vector3(xCenter, carRearWheelPos.y, zCenter); //Find all corners of the car Rectangle carCornerPos = SkeletonCar.GetCornerPositions(carPos, heading, carWidth, carLength); //Detect if any of the corners is outside of the map if ( !PathfindingController.IsPositionWithinGrid(carCornerPos.FL) || !PathfindingController.IsPositionWithinGrid(carCornerPos.FR) || !PathfindingController.IsPositionWithinGrid(carCornerPos.BL) || !PathfindingController.IsPositionWithinGrid(carCornerPos.BR)) { //At least one of the corners is outside of the map withinTrack = false; } return(withinTrack); }
//Check if the car is colliding with an obstacle or is outside of map public static bool HasCarInvalidPosition(Vector3 carRearWheelPos, float heading, CarData carData) { bool hasInvalidPosition = false; //Make the car bigger than it is to be on the safe side float marginOfSafety = 0.5f; float carLength = carData.GetLength() + marginOfSafety; float carWidth = carData.GetWidth() + marginOfSafety; //Find the center pos of the car (carPos is at the rearWheels) float distCenterToRearWheels = carData.GetDistanceToRearWheels(); float xCenter = carRearWheelPos.x + distCenterToRearWheels * Mathf.Sin(heading); float zCenter = carRearWheelPos.z + distCenterToRearWheels * Mathf.Cos(heading); Vector3 carPos = new Vector3(xCenter, carRearWheelPos.y, zCenter); // // Step 1. Check if the car's corners is inside of the map // //Find all corners of the car Rectangle carCornerPos = SkeletonCar.GetCornerPositions(carPos, heading, carWidth, carLength); //Detect if any of the corners is outside of the map if ( !PathfindingController.IsPositionWithinGrid(carCornerPos.FL) || !PathfindingController.IsPositionWithinGrid(carCornerPos.FR) || !PathfindingController.IsPositionWithinGrid(carCornerPos.BL) || !PathfindingController.IsPositionWithinGrid(carCornerPos.BR)) { //At least one of the corners is outside of the map hasInvalidPosition = true; return(hasInvalidPosition); } // // Step 2. Check if the car's center position is far away from an obstacle // //We dont need to check if the car is colliding with an obstacle if the distance to an obstacle is great IntVector2 cellPos = PathfindingController.ConvertCoordinateToCellPos(carPos); //The car is not colliding with anything if the steps to an obstacle is greater than the length of the car if (ObstaclesController.distanceToClosestObstacle[cellPos.x, cellPos.z] > carData.GetLength()) { //This is a valid position hasInvalidPosition = false; return(hasInvalidPosition); } // // Step 3. Check if the car is hitting an obstacle // //Find all corners of the car Rectangle carCornerPosFat = SkeletonCar.GetCornerPositions(carPos, heading, carWidth, carLength); //Method 1 - Use the car's corners and then rectangle-rectangle-intersection with the obstacles hasInvalidPosition = ObstacleDetectionCorners(carPos, carCornerPosFat); //Method 2 - Approximate the car with circles //hasInvalidPosition = ObstacleDetectionCircles(carCenterPos, heading, carData, carCornerPosFat); return(hasInvalidPosition); }