Exemplo n.º 1
0
    public void Start()
    {
        Vector2[] meshVertices;
        mtv            = new MTV();
        spriteRenderer = GetComponent <SpriteRenderer>();
        sprite         = spriteRenderer.sprite;
        meshVertices   = sprite.vertices;

        //determines the vertices in a game object
        if (gameObject.name.Contains("Square"))
        {
            vertices.Add(meshVertices[0]);
            vertices.Add(meshVertices[2]);
            vertices.Add(meshVertices[1]);
            vertices.Add(meshVertices[3]);
        }
        else if (gameObject.name.Contains("Hexagon"))
        {
            vertices.Add(meshVertices[0]);
            vertices.Add(meshVertices[5]);
            vertices.Add(meshVertices[4]);
            vertices.Add(meshVertices[3]);
            vertices.Add(meshVertices[2]);
            vertices.Add(meshVertices[1]);
        }
        else if (gameObject.name.Contains("Triangle"))
        {
            for (int i = 0; i < 3; i++)
            {
                vertices.Add(sprite.vertices[i]);
            }
        }

        //sets the axes for polygons
        //SetAxes(vertices);
    }
Exemplo n.º 2
0
    private Vector2 PointOfCollision(GameObject other, Vector2 MTV)
    {
        //edge collision tolerance
        float tolerance = 0.01f;

        //find the point lease in the direction of MTV
        List <Vector2> closestPoints1 = new List <Vector2>();
        float          currentMin;
        float          dotProd;
        int            numPoint = vertices.Count;

        Vector2 currentPoint;
        Vector2 currentPos = transform.position;
        //other colliding polygon
        Polygon otherPolygon = other.GetComponent <Polygon>();

        //begin by setting the current min to the first point of
        //currentPoint min to the 1st point
        currentPoint = transform.TransformPoint(vertices[0]);
        currentMin   = Vector2.Dot(currentPoint, MTV);
        closestPoints1.Add(currentPoint);

        //loops through remaining points
        for (int i = 1; i < numPoint; i++)
        {
            //translate to world space
            currentPoint = transform.TransformPoint(vertices[i]);
            //calc distance towars object 2
            dotProd = Vector2.Dot(currentPoint, MTV);
            //if the current point is the same distance in the direction
            //of obj2 along the MTV
            if (Mathf.Abs(dotProd - currentMin) < Mathf.Epsilon + tolerance)
            {
                //add new point
                closestPoints1.Add(currentPoint);
            }
            //else if it is less then the current distance
            else if (dotProd < currentMin - Mathf.Epsilon)
            {
                //clear the list of current points and set the new one
                currentMin = dotProd;
                closestPoints1.Clear();
                closestPoints1.Add(currentPoint);
            }
        }
        //if only one closest point: collsions
        if (closestPoints1.Count == 1)
        {
            return(closestPoints1[0]);
        }

        //check 2nd polygon
        List <Vector2> closestPoint2 = new List <Vector2>();
        float          currentMax;

        numPoint = otherPolygon.vertices.Count;
        Vector2        otherPos      = other.transform.position;
        List <Vector2> otherVertices = otherPolygon.vertices;
        MTV            otherMtv      = other.GetComponent <Polygon>().mtv;

        //begin by setting the current max of the first
        //point of the 2nd polygon
        currentPoint = transform.TransformPoint(otherVertices[0]);
        currentMax   = Vector2.Dot(currentPoint, otherMtv.smallAxis);
        closestPoint2.Add(currentPoint);

        //loops through remaining points
        for (int i = 1; i < numPoint; i++)
        {
            //translate to world space
            currentPoint = transform.TransformPoint(otherVertices[i]);
            //calc distance towars object 2
            dotProd = Vector2.Dot(currentPoint, otherMtv.smallAxis);
            //if the current point is the same distance in the direction
            //of obj2 along the MTV
            if (Mathf.Abs(dotProd - currentMax) < Mathf.Epsilon + tolerance)
            {
                //add new point
                closestPoint2.Add(currentPoint);
            }
            //else if it is less then the current distance
            else if (dotProd > currentMax + Mathf.Epsilon)
            {
                //clear the list of current points and set the new one
                currentMax = dotProd;
                closestPoint2.Clear();
                closestPoint2.Add(currentPoint);
            }
        }
        //if only one closest point: collsions
        if (closestPoint2.Count == 1)
        {
            return(closestPoint2[0]);
        }
        //find the two inner points
        //if needed
        Vector2 edge = new Vector2(-MTV.y, MTV.x);

        closestPoints1.AddRange(closestPoint2);
        //determines the min and max
        currentMin = currentMax = Vector2.Dot(closestPoints1[0], edge);
        int minIndex, maxIndex;

        minIndex = maxIndex = 0;
        numPoint = closestPoints1.Count;
        for (int i = 0; i < numPoint; i++)
        {
            dotProd = Vector2.Dot(closestPoints1[i], edge);
            if (dotProd < currentMin)
            {
                currentMin = dotProd;
                minIndex   = i;
            }
            if (dotProd > currentMax)
            {
                currentMax = dotProd;
                maxIndex   = i;
            }
        }
        //remove min and max indices
        closestPoints1.RemoveAt(minIndex);
        if (minIndex < maxIndex)
        {
            --maxIndex;
        }
        closestPoints1.RemoveAt(maxIndex);

        //take the avg of the two remaining indices
        Vector2 closestPoint = (closestPoints1[0] + closestPoint2[0]) * .5f;

        return(closestPoint);
    }