예제 #1
0
        public Vector3 GetRoutePosition(float dist)
        {
            dist = Mathf.Clamp(dist, 0.0f, TotalDistance);

            if (dist <= 0)
            {
                return(points_positions[0]);
            }
            else if (dist >= TotalDistance)
            {
                return(points_positions[points_num - 1]);
            }

            int point = 0;

            while (points_distances[point] < dist)
            {
                point++;
            }

            // get nearest two points, ensuring points wrap-around start & end of circuit
            p1n = point - 1;
            p2n = point;

            // found point numbers, now find interpolation value between the two middle points

            i = Mathf.InverseLerp(points_distances[p1n], points_distances[p2n], dist);

            if (smoothRoute)
            {
                p0n = Mathf.Clamp(point - 2, 0, points_num - 1);
                p3n = Mathf.Clamp(point + 1, 0, points_num - 1);


                P0 = points_positions[p0n];
                P1 = points_positions[p1n];
                P2 = points_positions[p2n];
                P3 = points_positions[p3n];

                return(Math_Functions.CatmullRom(P0, P1, P2, P3, i));
            }
            else
            {
                return(Vector3.Lerp(points_positions[p1n], points_positions[p2n], i));
            }
        }
예제 #2
0
        public Path_Point GetPathPoint(float dist, bool isSmooth)
        {
            if (_isCircuit)
            {
                dist = (dist + TotalDistance) % TotalDistance;
            }
            else
            {
                dist = Mathf.Clamp(dist, 0.0f, TotalDistance);
            }

            // find segment index
            int index = 1;

            while (_distances[index] < dist)
            {
                index++;
            }

            // the segment in the middle
            _interpolation = Mathf.InverseLerp(
                _distances[index - 1],
                _distances[index],
                dist);

            index = index % _numPoints;

            if (_isCircuit)
            {
                _four_indices[0] = ((index - 2) + _numPoints) % _numPoints;
                _four_indices[1] = ((index - 1) + _numPoints) % _numPoints;
                _four_indices[2] = index % _numPoints;
                _four_indices[3] = (index + 1) % _numPoints;
            }
            else
            {
                _four_indices[0] = Mathf.Clamp(index - 2, 0, _numPoints - 1);
                _four_indices[1] = ((index - 1) + _numPoints) % _numPoints;
                _four_indices[2] = index % _numPoints;
                _four_indices[3] = Mathf.Clamp(index + 1, 0, _numPoints - 1);
            }

            if (isSmooth)
            {
                // assign the four points with the segment in the middle
                _four_points[0] = _points[_four_indices[0]];
                _four_points[1] = _points[_four_indices[1]];
                _four_points[2] = _points[_four_indices[2]];
                _four_points[3] = _points[_four_indices[3]];

                // you need two points to get a forward direction
                _pathPoint.point = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation);
                _pathPoint.forward = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation + 0.01f) - _pathPoint.point;

                _pathPoint.forward.Normalize();
            }
            else             // strait shooting
            {
                _pathPoint.point = Vector3.Lerp(
                    _points[_four_indices[1]],
                    _points[_four_indices[2]],
                    _interpolation);

                _pathPoint.forward = _points[_four_indices[2]] - _points[_four_indices[1]];
                _pathPoint.forward.Normalize();
            }

            // 90 degree turn to right
            _pathPoint.right = Vector3.Cross(
                Vector3.Lerp(
                    _upDirections[_four_indices[1]],
                    _upDirections[_four_indices[2]],
                    _interpolation),                     // lerp
                _pathPoint.forward).normalized;          // cross

            // 90 degree turn to up
            _pathPoint.up = Vector3.Cross(_pathPoint.forward, _pathPoint.right).normalized;

            // now all directions are 90 degrees from each other

            return(_pathPoint);
        }
예제 #3
0
        /*
         *@dist: distance 点距离起始点的距离(两点之间直线段距离累加)
         * return:返回一个Path_Point 依据传入的distance
         */
        public Path_Point GetPathPoint(float dist, bool isSmooth)
        {
            if (_isCircuit)
            {
                dist = (dist + TotalDistance) % TotalDistance;
            }
            else
            {
                dist = Mathf.Clamp(dist, 0.0f, TotalDistance);//限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
            }
            // find segment index
            int index = 1;

            while (_distances[index] < dist)
            {
                index++;
            }

            // the segment in the middle
            // static float InverseLerp(float from, float to, float value);  eg: (5,10,8)==3/5
            _interpolation = Mathf.InverseLerp(
                _distances[index - 1],
                _distances[index],
                dist);//计算两个值之间的Lerp参数。也就是value在from和to之间的比例值。

            //防止是圆的情况
            index = index % _numPoints;

            // 获取插值两边各两个点的index,总共4个点;
            if (_isCircuit)
            {
                _four_indices[0] = ((index - 2) + _numPoints) % _numPoints;
                _four_indices[1] = ((index - 1) + _numPoints) % _numPoints;
                _four_indices[2] = index % _numPoints;
                _four_indices[3] = (index + 1) % _numPoints;
            }
            else
            {
                _four_indices[0] = Mathf.Clamp(index - 2, 0, _numPoints - 1);
                _four_indices[1] = ((index - 1) + _numPoints) % _numPoints;
                _four_indices[2] = index % _numPoints;
                _four_indices[3] = Mathf.Clamp(index + 1, 0, _numPoints - 1);
            }

            if (isSmooth)
            {
                // assign the four points with the segment in the middle在中间分配四个点与段
                _four_points[0] = _points[_four_indices[0]];
                _four_points[1] = _points[_four_indices[1]];
                _four_points[2] = _points[_four_indices[2]];
                _four_points[3] = _points[_four_indices[3]];

                // you need two points to get a forward direction你需要两点来获得前进的方向
                _pathPoint.point = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation);
                // 获取方向增加点 interpolation值:0.001f
                _pathPoint.forward = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation + interpolationPara) - _pathPoint.point;

                _pathPoint.forward.Normalize();
            }
            else // strait shooting
            {
                _pathPoint.point = Vector3.Lerp(
                    _points[_four_indices[1]],
                    _points[_four_indices[2]],
                    _interpolation);

                _pathPoint.forward = _points[_four_indices[2]] - _points[_four_indices[1]];
                _pathPoint.forward.Normalize();
            }

            // 90 degree turn to right90度右转
            // Vector3.Lerp p=form+(to-form)*t
            //_interpolation = Mathf.InverseLerp
            // 计算点在up方向上的Vector3
            Vector3 tempUpDirection = Vector3.Lerp(_upDirections[_four_indices[1]], _upDirections[_four_indices[2]], _interpolation);

            _pathPoint.right = Vector3.Cross(tempUpDirection, _pathPoint.forward).normalized; // cross

            // 90 degree turn to up90度上转
            _pathPoint.up = Vector3.Cross(_pathPoint.forward, _pathPoint.right).normalized;

            //tempUpDirection.normalized 与 _pathPoint.up不相同

            // now all directions are 90 degrees from each other

            return(_pathPoint);
        }
예제 #4
0
        private void IsNotCircuit(float dist)
        {
            dist = Mathf.Clamp(dist, 0.0f, TotalDistance);

            // find segment index
            int index = 1;

            while (_distances[index] < dist)
            {
                index++;
            }

            // the segment in the middle
            _four_indices[1] = index - 1;
            _four_indices[2] = index;

            _interpolation = Mathf.InverseLerp(
                _distances[_four_indices[1]],
                _distances[_four_indices[2]],
                dist);


            if (_isSmooth)
            {
                _four_indices[0] = Mathf.Clamp(index - 2, 0, _points.Length - 1);
                _four_indices[3] = Mathf.Clamp(index + 1, 0, _points.Length - 1);

                // assign the four points with the segment in the middle
                _four_points[0] = _points[_four_indices[0]];
                _four_points[1] = _points[_four_indices[1]];
                _four_points[2] = _points[_four_indices[2]];
                _four_points[3] = _points[_four_indices[3]];

                // you need two points to get a forward direction
                pathPoint.point = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation);
                pathPoint.forward = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation + 0.01f) - pathPoint.point;

                pathPoint.forward.Normalize();
            }
            else             // strait shooting
            {
                pathPoint.point = Vector3.Lerp(
                    _points[_four_indices[1]],
                    _points[_four_indices[2]],
                    _interpolation);

                pathPoint.forward = _points[_four_indices[2]] - _points[_four_indices[1]];
                pathPoint.forward.Normalize();
            }

            // 90 degree turn to right
            pathPoint.right = Vector3.Cross(
                Vector3.Lerp(
                    transform.InverseTransformDirection(_children_transforms[_four_indices[1]].up),
                    transform.InverseTransformDirection(_children_transforms[_four_indices[2]].up),
                    _interpolation),                     // lerp
                pathPoint.forward).normalized;           // cross

            // 90 degree turn to up
            pathPoint.up = Vector3.Cross(pathPoint.forward, pathPoint.right).normalized;


            // now all points are 90 degrees from each other
        }