//
        // 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);
        }
        //
        // Use the car's corners and then rectangle-rectangle-intersection with the obstacles to check if the car is intersecting with an obstacle
        //
        private static bool IsCarIntersectingWithObstacles(Rectangle carRectangle, IntVector2 carCenterCellPos, Map map)
        {
            bool hasInvalidPosition = false;

            ////Alternative 1. Find if one of the cells the car is intersecting is an obstacle
            ////This is slower because it requires a lot of triangle-triangle intersections to identify the cells
            ////which is the same as intersecting with the obstacles in the first place
            //bool isCellIntersectedByCarObstacle = FindIfCellBlockedByRectangleIsObstacle(carRectangle, carCenterCellPos, map);


            //if (isCellIntersectedByCarObstacle)
            //{
            //    hasInvalidPosition = true;

            //    return hasInvalidPosition;
            //}

            //return false;



            //But if we want more accurate collision detection we have to take into account that the obstacle
            //may not block the entire cell, so the can can maybe move into this position without colliding with an obstacle

            //Find all obstacles that are close to the car, so we dont have to check all obstacles
            //List<Obstacle> obstaclesThatAreClose = FindCloseObstaclesCell(carPos);
            //List<Obstacle> obstaclesThatAreClose = FindCloseObstaclesAABB(carCorners, map);

            //Maybe more efficient to instead of finding obstacles that are close, go through all obstacles
            //because the obstacle detection algorithm is using AABB anyway
            List <Obstacle> obstaclesThatAreClose = map.allObstacles;

            for (int i = 0; i < obstaclesThatAreClose.Count; i++)
            {
                Rectangle obstacleCorners = obstaclesThatAreClose[i].cornerPos;

                //Rectangle-rectangle intersection, which is here multiple triangle-triangle intersection tests
                if (Intersections.AreRectangleRectangleIntersecting(carRectangle, obstacleCorners))
                {
                    hasInvalidPosition = true;

                    return(hasInvalidPosition);
                }
            }

            return(hasInvalidPosition);
        }
Example #3
0
        //Instantiate one cube and add its position to the array
        void AddObstacle(Map map, Rectangle avoidRect)
        {
            //Generate random coordinates in the map
            float posX = Random.Range(1f, map.MapWidth - 1f);
            float posZ = Random.Range(1f, map.MapWidth - 1f);
            //Rotation
            float rotY = Random.Range(0f, 360f);
            //Size
            float sizeX = Random.Range(Parameters.minObstacleSize, Parameters.maxObstacleSize);
            float sizeZ = Random.Range(Parameters.minObstacleSize, Parameters.maxObstacleSize);

            Vector3 pos = new Vector3(posX, 0.5f, posZ);

            Quaternion rot = Quaternion.Euler(0f, rotY, 0f);

            Vector3 scale = new Vector3(sizeX, 1f, sizeZ);

            //Update the prefab with the new data
            obstaclePrefabObj.transform.position   = pos;
            obstaclePrefabObj.transform.rotation   = rot;
            obstaclePrefabObj.transform.localScale = scale;

            Obstacle newObstacle = new Obstacle(obstaclePrefabObj.transform);


            //The obstacle shouldnt intersect with the start area
            if (Intersections.AreRectangleRectangleIntersecting(avoidRect, newObstacle.cornerPos))
            {
                return;
            }


            //Add a new obstacle object at this position
            Instantiate(obstaclePrefabObj, obstaclesParent);

            map.allObstacles.Add(newObstacle);
        }
        //
        // Check if the trailer is colliding with the drag vehicle
        //
        public static bool IsTrailerCollidingWithDragVehicle(
            Vector3 semiRearWheelPos, float semiHeading, CarData semiData,
            Vector3 trailerRearWheelPos, float trailerHeading, CarData trailerData)
        {
            bool isColliding = false;

            //Use triangle-traingle intersection so we need the rectangles
            Vector3 trailerCenter = trailerData.GetCenterPos(trailerRearWheelPos, trailerHeading);

            Rectangle trailerRect = CarData.GetCornerPositions(trailerCenter, trailerHeading, trailerData.carWidth * 0.9f, trailerData.CarLength);

            //The semi's cabin rectangle
            Vector3 cabinCenter = semiData.GetSemiCabinCenter(semiRearWheelPos, semiHeading);

            //Make it slightly shorter or too many false collisions
            Rectangle semiRect = CarData.GetCornerPositions(cabinCenter, semiHeading, semiData.carWidth, semiData.cabinLength * 0.95f);

            if (Intersections.AreRectangleRectangleIntersecting(trailerRect, semiRect))
            {
                return(true);
            }

            return(isColliding);
        }