Beispiel #1
0
    public static HullCollision OBBOBBCollision(OBBHull boxHull1, OBBHull boxHull2)
    {
        Particle2D A = boxHull1.GetComponent <Particle2D>();
        Particle2D B = boxHull2.GetComponent <Particle2D>();

        Vector3[] shape1Corners;
        Vector3[] shape2Corners;
        shape1Corners = new Vector3[4];
        shape2Corners = new Vector3[4];
        Vector3[] normals      = new Vector3[4];
        float[]   shape1MinMax = new float[2];
        float[]   shape2MinMax = new float[2];

        shape1Corners = getRotatedCorners(boxHull1);
        shape2Corners = getRotatedCorners(boxHull2);

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

        HullCollision col = new HullCollision();

        col.a = boxHull1;
        col.b = boxHull2;
        Vector3 range = (boxHull2.transform.position + boxHull2.offset) - (boxHull1.transform.position + boxHull1.offset);
        //float xOverlap = boxHull1.halfX + boxHull2.halfX - Mathf.Abs(range.x);
        //float yOverlap = boxHull1.halfY + boxHull2.halfY - Mathf.Abs(range.y);

        //TRANSPORTATION

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

        //Vector3 closingVel = A.velocity - B.velocity;
        Vector3 closingVel = B.velocity - A.velocity;

        col.closingVelocity = closingVel;

        HullCollision.Contact con0 = new HullCollision.Contact();
        con0.point       = new Vector3(Mathf.Clamp(range.x, -boxHull1.halfX, boxHull1.halfX), Mathf.Clamp(range.y, -boxHull1.halfY, boxHull1.halfY));
        con0.restitution = Mathf.Min(boxHull1.restitution, boxHull2.restitution);
        con0.normal      = range.normalized;

        for (int i = 0; i < normals.Length; i++)
        {
            //Debug.Log("testing corner" + i);

            shape1MinMax = SatTest(normals[i], shape1Corners);
            shape2MinMax = SatTest(normals[i], shape2Corners);
            if (!Overlap(shape1MinMax[0], shape1MinMax[1], shape2MinMax[0], shape2MinMax[1]))
            {
                //Debug.Log("falure");
                col.status = false;
                return(col);
            }
        }
        col.status      = true;
        col.contacts[0] = con0;
        return(col);
    }
Beispiel #2
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);
    }
Beispiel #3
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);
    }
Beispiel #4
0
    public static HullCollision CircleCircleCollision(CircleHull circleHull1, CircleHull circleHull2)
    {
        // *IMPORTANT* for circle and square the collision only wirks with obejct1 - object 2 and not viceversa, must be a prob in clollision resolution
        Vector3 c1Offset = circleHull1.offset;
        Vector3 c2Offset = circleHull2.offset;

        Vector3 range   = (circleHull2.transform.position + c2Offset) - (circleHull1.transform.position + c1Offset); // make sure offsets arent screwing things up
        float   overlap = (circleHull2.radius + circleHull1.radius) - range.magnitude;

        HullCollision col = new HullCollision();

        col.a           = circleHull1;
        col.b           = circleHull2;
        col.penetration = range * overlap;

        HullCollision.Contact con0 = new HullCollision.Contact();
        con0.point       = (range.normalized * circleHull1.radius);
        con0.point      += new Vector3(circleHull1.transform.position.x, circleHull1.transform.position.y);
        con0.normal      = range.normalized;
        con0.restitution = Mathf.Min(circleHull1.restitution, circleHull2.restitution);

        col.contacts[0] = con0;

        Particle2D c1 = circleHull1.GetComponentInParent <Particle2D>();
        Particle2D c2 = circleHull2.GetComponentInParent <Particle2D>();

        Vector3 closingVel = c2.velocity - c1.velocity; // started as c1 -c2

        col.closingVelocity = closingVel;

        if (overlap > 0)
        {
            col.status = true;
            return(col);
        }
        else
        {
            col.status = false;
            return(col);
        }
    }
Beispiel #5
0
    public static HullCollision AABBAABBCollision(AABBHull boxHull1, AABBHull boxHull2)
    {
        Vector3    min0, max0, min1, max1;
        Vector3    b1Offset = boxHull1.offset;
        Vector3    b2Offset = boxHull2.offset;
        Particle2D A        = boxHull1.GetComponent <Particle2D>();
        Particle2D B        = boxHull2.GetComponent <Particle2D>();

        min0 = boxHull1.transform.position - new Vector3(boxHull1.halfX, boxHull1.halfY) + b1Offset;
        max0 = boxHull1.transform.position + new Vector3(boxHull1.halfX, boxHull1.halfY) + b1Offset;
        min1 = boxHull2.transform.position - new Vector3(boxHull2.halfX, boxHull2.halfY) + b2Offset;
        max1 = boxHull2.transform.position + new Vector3(boxHull2.halfX, boxHull2.halfY) + b2Offset;

        Vector3 range = (boxHull2.transform.position + b2Offset) - (boxHull1.transform.position + b1Offset); // make sure offsets arent screwing things up

        HullCollision col = new HullCollision();

        col.a = boxHull1;
        col.b = boxHull2;

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

        //TRANSPORTATION

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

        //Vector3 closingVel = A.velocity - B.velocity;
        Vector3 closingVel = B.velocity - A.velocity;

        col.closingVelocity = closingVel;

        HullCollision.Contact con0 = new HullCollision.Contact();
        con0.point       = new Vector3(Mathf.Clamp(range.x, -boxHull1.halfX, boxHull1.halfX), Mathf.Clamp(range.y, -boxHull1.halfY, boxHull1.halfY));
        con0.restitution = Mathf.Min(boxHull1.restitution, boxHull2.restitution);

        if (max0.x >= min1.x && max1.x >= min0.x)
        {
            if (max0.y >= min1.y && max1.y >= min0.y)
            {
                Vector3 collisionNormal = new Vector3();

                if (con0.point.x == boxHull1.halfX)//added mathf
                {
                    collisionNormal = new Vector3(1.0f, 0.0f);
                }
                if (con0.point.x == -boxHull1.halfX)//added mathf
                {
                    collisionNormal = new Vector3(-1.0f, 0.0f);
                }
                if (con0.point.y == boxHull1.halfY)
                {
                    collisionNormal = new Vector3(0.0f, 1.0f);
                }
                if (con0.point.y == -boxHull1.halfY)
                {
                    collisionNormal = new Vector3(0.0f, -1.0f);
                }

                con0.normal = collisionNormal;

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