コード例 #1
0
        //Update the circles to see if we can identify the coordinates from geometry
        void UpdateCircles(Vector3 carPos, float carHeading)
        {
            circlePositions = SkeletonCar.GetCirclePositions(carPos, carHeading, circlePositions);

            for (int i = 0; i < circleOBj.Length; i++)
            {
                AddCoordinates(circleOBj[i], circlePositions[i].x, circlePositions[i].z);
            }
        }
コード例 #2
0
        //Approximate the car's area with circles to detect if there's an obstacle within the circle
        private static bool ObstacleDetectionCircles(Vector3 carPos, float heading, CarData carData, Rectangle carCornerPos)
        {
            bool hasInvalidPosition = false;

            Vector3[] circlePositions = new Vector3[3];

            //Get the position of the 3 circles that approximates the size of the car
            circlePositions = SkeletonCar.GetCirclePositions(carPos, heading, circlePositions);

            //
            //Find all obstacles close to the car
            //
            //The car's length is 5 m and each obstacle is 1 m, so add a little to be on the safe side
            //float searchRadius = carData.GetLength() * 0.5f + 1.5f;

            //List<ObstacleData> obstaclesThatAreClose = FindCloseObstaclesWithinRadius(carPos, searchRadius * searchRadius);

            List <ObstacleData> obstaclesThatAreClose = FindCloseObstaclesCell(carPos, carCornerPos);

            //If there are no obstacle close, then return
            if (obstaclesThatAreClose.Count == 0)
            {
                return(hasInvalidPosition);
            }


            //
            //If there are obstacles around the car, then we have to see if some of them intersect
            //
            //The radius of one circle that approximates the area of the car
            //The width of the car is 2 m but the circle has to be larger to provide a margin of safety
            float circleRadius = 1.40f;

            //But we also need to add the radius of the box obstacle which has a width of 1 m
            float criticalRadius = circleRadius + 0.5f;

            //And square it to speed up
            float criticalRadiusSqr = criticalRadius * criticalRadius;

            //Loop through all circles and detect if there's an obstacle within the circle
            for (int i = 0; i < circlePositions.Length; i++)
            {
                Vector3 currentCirclePos = circlePositions[i];

                //Is there an obstacle within the radius of this circle
                for (int j = 0; j < obstaclesThatAreClose.Count; j++)
                {
                    float distSqr = (currentCirclePos - obstaclesThatAreClose[j].centerPos).sqrMagnitude;

                    if (distSqr < criticalRadiusSqr)
                    {
                        hasInvalidPosition = true;

                        return(hasInvalidPosition);
                    }
                }
            }


            return(hasInvalidPosition);
        }