示例#1
0
        //Returns true if "point" is in a rectangle mad up of RectA to RectD. The line point is assumed to be on the same
        //plane as the rectangle. If the point is not on the plane, use ProjectPointOnPlane() first.
        public static bool IsPointIn2DRectangle(Vector3 point, Vector3 rectA, Vector3 rectC, Vector3 rectB, Vector3 rectD)
        {
            Vector3 vector;
            Vector3 linePoint;

            //get the center of the rectangle
            vector = rectC - rectA;
            float size = -(vector.magnitude / 2f);

            vector = ExtVector3.AddVectorLength(vector, size);
            Vector3 middle = rectA + vector;

            Vector3 xVector = rectB - rectA;
            float   width   = xVector.magnitude / 2f;

            Vector3 yVector = rectD - rectA;
            float   height  = yVector.magnitude / 2f;

            linePoint = ExtLine.ProjectPointOnLine(middle, xVector.normalized, point);
            vector    = linePoint - point;
            float yDistance = vector.magnitude;

            linePoint = ExtLine.ProjectPointOnLine(middle, yVector.normalized, point);
            vector    = linePoint - point;
            float xDistance = vector.magnitude;

            if ((xDistance <= width) && (yDistance <= height))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#2
0
        public void AddLineLocal(Vector3 p1, Vector3 p2)
        {
            ExtLine line = new ExtLine(p1, p2);

            _listLinesLocal = ExtArray.Add(_listLinesLocal, line);
            UpdateGlobalLineFromLocalOnes();
        }
示例#3
0
        //Returns true if a line segment (made up of linePoint1 and linePoint2) is fully or partially in a rectangle
        //made up of RectA to RectD. The line segment is assumed to be on the same plane as the rectangle. If the line is
        //not on the plane, use ProjectPointOnPlane() on linePoint1 and linePoint2 first.
        public static bool IsLineInRectangle(Vector3 linePoint1, Vector3 linePoint2, Vector3 rectA, Vector3 rectB, Vector3 rectC, Vector3 rectD)
        {
            bool pointAInside = false;
            bool pointBInside = false;

            pointAInside = IsPointIn2DRectangle(linePoint1, rectA, rectC, rectB, rectD);

            if (!pointAInside)
            {
                pointBInside = IsPointIn2DRectangle(linePoint2, rectA, rectC, rectB, rectD);
            }

            //none of the points are inside, so check if a line is crossing
            if (!pointAInside && !pointBInside)
            {
                bool lineACrossing = ExtLine.AreLineSegmentsCrossing(linePoint1, linePoint2, rectA, rectB);
                bool lineBCrossing = ExtLine.AreLineSegmentsCrossing(linePoint1, linePoint2, rectB, rectC);
                bool lineCCrossing = ExtLine.AreLineSegmentsCrossing(linePoint1, linePoint2, rectC, rectD);
                bool lineDCrossing = ExtLine.AreLineSegmentsCrossing(linePoint1, linePoint2, rectD, rectA);

                if (lineACrossing || lineBCrossing || lineCCrossing || lineDCrossing)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }
示例#4
0
        public bool GetClosestPointIfWeCan(Vector3 k, out Vector3 closestPoint, GravityOverrideLineTopDown[] gravityOverride)
        {
            if (_listLines.Length == 0 || gravityOverride.Length != _listLines.Length)
            {
                closestPoint = Vector3.zero;
                return(false);
            }
            closestPoint = ExtLine.GetClosestPointFromLines(k, _listLines, out int indexLine);
            bool canApplyGravity = _listLines[indexLine].GetClosestPointIfWeCan(k, out closestPoint, gravityOverride[indexLine]);

            return(canApplyGravity);
        }
示例#5
0
        public ExtPolyLines(Vector3 position,
                            Quaternion rotation,
                            Vector3 localScale) : this()
        {
            _position   = position;
            _rotation   = rotation;
            _localScale = localScale;

            _listLinesLocal    = new ExtLine[3];
            _listLines         = new ExtLine[3];
            _listLinesLocal[0] = new ExtLine(new Vector3(0, 0, 0), new Vector3(-0.3f, 0, -0.2f));
            _listLinesLocal[1] = new ExtLine(new Vector3(0, 0, 0), new Vector3(0.3f, 0, -0.2f));
            _listLinesLocal[2] = new ExtLine(new Vector3(0, 0, 0), new Vector3(0, 0, 0.3f));
            UpdateMatrix();
        }
    public ExtTriangle(Vector3 a, Vector3 b, Vector3 c,
                       bool _unidirectionnal, bool _inverseDirection, bool _infinitePlane,
                       bool _noGravityBorders)
    {
        EdgeAb = new ExtLine(a, b);
        EdgeBc = new ExtLine(b, c);
        EdgeCa = new ExtLine(c, a);

        unidirectionnal  = _unidirectionnal;
        inverseDirection = _inverseDirection;
        infinitePlane    = _infinitePlane;
        noGravityBorders = _noGravityBorders;

        TriNorm = Vector3.Cross(a - b, a - c);

        //try to inverse normal
        //if (unidirectionnal && inverseDirection)
        //    TriNorm *= -1;
    }
示例#7
0
    /// <summary>
    /// from a given path (sets of points), get the closest position of pos from this path, and return the
    /// current percentage inside
    /// </summary>
    /// <param name="pos"></param>
    /// <param name="chunkPath"></param>
    /// <param name="precision"></param>
    /// <returns></returns>
    public static float GetPercentageFromPosToChunkPath(Vector3 pos, Vector3[] chunkPath, float precision = 1f)
    {
        if (chunkPath == null || chunkPath.Length == 0)
        {
            //Debug.LogWarning("out of bounds");
            return(0);
        }

        float percent    = 0f;
        float lenghtPath = ExtMathf.GetLenghtOfPath(chunkPath);

        Vector3[] closestPointInLines = new Vector3[chunkPath.Length];
        float[]   lenghtAllLine       = new float[chunkPath.Length];

        for (int i = 0; i < closestPointInLines.Length - 1; i++)
        {
            ExtLine line = new ExtLine(chunkPath[i], chunkPath[i + 1]);
            closestPointInLines[i] = line.ClosestPointTo(pos);

            lenghtAllLine[i] = (float)line.GetLenght();
        }

        int     indexFound   = -1;
        Vector3 closestPoint = ExtMathf.GetClosestPoint(pos, closestPointInLines, ref indexFound);

        if (indexFound >= lenghtAllLine.Length)
        {
            //Debug.LogWarning("out of bounds");
            return(0);
        }

        float   lenghtLineClose = lenghtAllLine[indexFound];
        Vector3 veccA           = chunkPath[indexFound];
        int     indexPlusOne    = indexFound + 1;

        if (indexPlusOne >= chunkPath.Length)
        {
            indexPlusOne = 0;
        }
        Vector3 veccB = chunkPath[indexPlusOne];
        float   percentageAlongCLosestLine = ExtMathf.GetPercentageAlong(veccA, veccB, closestPoint);

        float lenghtTraveledInThisLine = (percentageAlongCLosestLine * lenghtLineClose) / 1f;

        //add lenght, from start to the line found
        float lenghtFromZeroToThisPoint = 0f;

        for (int i = 0; i < indexFound; i++)
        {
            lenghtFromZeroToThisPoint += lenghtAllLine[i];
        }
        lenghtFromZeroToThisPoint += lenghtTraveledInThisLine;

        //then add the additionnal percentage
        percent = (lenghtFromZeroToThisPoint * 100f) / lenghtPath;

        //Debug.Log("ChunkLenght: " + lenghtPath + ", lenght closest line: " + lenghtLineClose + ", percent along this line: " + percentageAlongCLosestLine
        //    + "lenght From Zero to this point: " + lenghtFromZeroToThisPoint + ", total percent: " + percent);

        return(percent);
    }
示例#8
0
 /// <summary>
 /// Return the closest point from all lines
 /// </summary>
 public Vector3 GetClosestPoint(Vector3 k)
 {
     return(ExtLine.GetClosestPointFromLines(k, _listLines, out int indexLine));
 }