Exemplo n.º 1
0
    public static HullCollision CircleAABBCollision(CircleHull circleHull, AABBHull boxHull)
    {
        Particle2D A = boxHull.GetComponent <Particle2D>();
        Particle2D B = circleHull.GetComponent <Particle2D>();

        Vector3 closestPoint = new Vector3(0.0f, 0.0f);
        Vector3 range        = (circleHull.transform.position + circleHull.offset) - (boxHull.transform.position + boxHull.offset);


        closestPoint = new Vector3(Mathf.Clamp(range.x, -boxHull.halfX, boxHull.halfX), Mathf.Clamp(range.y, -boxHull.halfY, boxHull.halfY));

        HullCollision col = new HullCollision();

        col.a = boxHull;
        col.b = circleHull;
        Vector3 closingVel  = B.velocity - A.velocity;
        Vector3 penetration = range - (closestPoint - circleHull.transform.position + circleHull.offset);

        col.closingVelocity = closingVel;
        col.penetration     = penetration;

        HullCollision.Contact con0 = new HullCollision.Contact();
        con0.point       = closestPoint;
        con0.restitution = Mathf.Min(boxHull.restitution, circleHull.restitution);

        Vector3 collisionNormal = new Vector3();


        if ((range - closestPoint).magnitude - circleHull.radius < 0)
        {
            if (con0.point.x == boxHull.halfX)//added mathf
            {
                collisionNormal = new Vector3(1.0f, 0.0f);
            }
            if (con0.point.x == -boxHull.halfX)//added mathf
            {
                collisionNormal = new Vector3(-1.0f, 0.0f);
            }
            if (con0.point.y == boxHull.halfY)
            {
                collisionNormal = new Vector3(0.0f, 1.0f);
            }
            if (con0.point.y == -boxHull.halfY)
            {
                collisionNormal = new Vector3(0.0f, -1.0f);
            }

            con0.normal = collisionNormal;

            col.status      = true;
            col.contacts[0] = con0;
        }
        else
        {
            col.status = false;
        }

        return(col);
    }
Exemplo n.º 2
0
    public static HullCollision CircleOBBCollision(CircleHull circleHull, OBBHull OBBHull)
    {
        Particle2D A = circleHull.GetComponent <Particle2D>();
        Particle2D B = OBBHull.GetComponent <Particle2D>();

        Vector3[] OBBCorners;

        OBBCorners = new Vector3[2];//was 4
        Vector3[] normals      = new Vector3[2];
        float[]   OBBMinMax    = new float[2];
        float[]   circleMinMax = new float[2];

        OBBCorners = getRotatedCorners(OBBHull);

        normals[0] = getUpNormal(-OBBHull.currentRotation);
        normals[1] = getRightNormal(-OBBHull.currentRotation);
        //normals[2] = getUpNormal(-OBBHull2.currentRotation);
        //normals[3] = getRightNormal(-boxHull2.currentRotation);

        HullCollision col = new HullCollision();

        col.a = circleHull;
        col.b = OBBHull;
        Vector3 range = (OBBHull.transform.position + OBBHull.offset) - (circleHull.transform.position + circleHull.offset);

        Vector3 rotatedRange = getRotatedPoint(range, new Vector3(0.0f, 0.0f), -OBBHull.currentRotation);// 2 circleHull.transform.position
        Vector3 point        = new Vector3(Mathf.Clamp(rotatedRange.x, -OBBHull.halfX, OBBHull.halfX), Mathf.Clamp(rotatedRange.y, -OBBHull.halfY, OBBHull.halfY));
        //Debug.Log("range " + range);
        //Debug.Log("rotrange " + rotatedRange);

        //float xOverlap = boxHull1.halfX + boxHull2.halfX - Mathf.Abs(range.x);
        //float yOverlap = boxHull1.halfY + boxHull2.halfY - Mathf.Abs(range.y);

        //col.penetration = new Vector3(xOverlap, yOverlap);

        Vector3 closingVel = B.velocity - A.velocity;

        col.closingVelocity = closingVel;

        HullCollision.Contact con0 = new HullCollision.Contact();
        con0.point       = new Vector3(Mathf.Clamp(range.x, -OBBHull.halfX, OBBHull.halfX), Mathf.Clamp(range.y, -OBBHull.halfY, OBBHull.halfY));
        con0.restitution = Mathf.Min(OBBHull.restitution, circleHull.restitution);
        con0.normal      = range.normalized;
        //Debug.Log("point " + point);

        col.status = false;
        if ((rotatedRange - point).magnitude - circleHull.radius < 0)
        {
            col.status      = true;
            col.contacts[0] = con0;
        }
        return(col);
    }