예제 #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);
    }
예제 #2
0
    public static HullCollision AABBOBBCollision(AABBHull AABBHull, OBBHull OBBHull)
    {
        Particle2D A = AABBHull.GetComponent <Particle2D>();
        Particle2D B = OBBHull.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[0] = AABBHull.transform.position - new Vector3(AABBHull.halfX, AABBHull.halfY) + AABBHull.offset;
        shape1Corners[1] = AABBHull.transform.position - new Vector3(AABBHull.halfX, -AABBHull.halfY) + AABBHull.offset;
        shape1Corners[2] = AABBHull.transform.position + new Vector3(AABBHull.halfX, AABBHull.halfY) + AABBHull.offset;
        shape1Corners[3] = AABBHull.transform.position + new Vector3(AABBHull.halfX, -AABBHull.halfY) + AABBHull.offset;
        shape2Corners    = getRotatedCorners(OBBHull);

        normals[0] = new Vector3(0.0f, 1.0f, 0.0f);
        normals[1] = new Vector3(1.0f, 0.0f, 0.0f);
        normals[2] = getUpNormal(-OBBHull.currentRotation);
        normals[3] = getRightNormal(-OBBHull.currentRotation);

        HullCollision col = new HullCollision();

        col.a = AABBHull;
        col.b = OBBHull;
        Vector3 range = (OBBHull.transform.position + OBBHull.offset) - (AABBHull.transform.position + AABBHull.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, -AABBHull.halfX, AABBHull.halfX), Mathf.Clamp(range.y, -AABBHull.halfY, AABBHull.halfY));
        con0.restitution = Mathf.Min(AABBHull.restitution, OBBHull.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);
    }
예제 #3
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);
    }
예제 #4
0
    public static void AABBAABBCollision(CollisionManager.HullCollision col)
    {
        Vector3 min0, max0, min1, max1;

        AABBHull A = col.a.GetComponent <AABBHull>();
        AABBHull B = col.b.GetComponent <AABBHull>();

        min0 = A.transform.position - A.halfSize + A.localCenter;
        max0 = A.transform.position + A.halfSize + A.localCenter;
        min1 = B.transform.position - B.halfSize + B.localCenter;
        max1 = B.transform.position + B.halfSize + B.localCenter;

        Vector3 range = (B.transform.position + B.localCenter) - (A.transform.position + A.localCenter); // make sure offsets arent screwing things up

        float xOverlap = A.halfSize.x + B.halfSize.x - Mathf.Abs(range.x);
        float yOverlap = A.halfSize.y + B.halfSize.y - Mathf.Abs(range.y);
        float zOverlap = A.halfSize.z + B.halfSize.z - Mathf.Abs(range.z); // was math.abs

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

        //Vector3 closingVel = A.velocity - B.velocity;
        Vector3 closingVel = B.GetComponent <Particle3D>().velocity - A.GetComponent <Particle3D>().velocity;

        col.closingVelocity = closingVel;

        CollisionManager.HullCollision.Contact con0 = new CollisionManager.HullCollision.Contact();
        con0.point       = new Vector3(Mathf.Clamp(range.x, -A.halfSize.x, A.halfSize.x), Mathf.Clamp(range.y, -A.halfSize.y, A.halfSize.y), Mathf.Clamp(range.z, -A.halfSize.z, A.halfSize.z));
        con0.restitution = Mathf.Min(A.restitution, B.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 == A.halfSize.x)//added mathf
                {
                    collisionNormal = new Vector3(1.0f, 0.0f, 0.0f);
                }
                if (con0.point.x == -A.halfSize.x)//added mathf
                {
                    collisionNormal = new Vector3(-1.0f, 0.0f, 0.0f);
                }
                if (con0.point.y == A.halfSize.y)
                {
                    collisionNormal = new Vector3(0.0f, 1.0f, 0.0f);
                }
                if (con0.point.y == -A.halfSize.y)
                {
                    collisionNormal = new Vector3(0.0f, -1.0f, 0.0f);
                }
                if (con0.point.z == A.halfSize.z)
                {
                    collisionNormal = new Vector3(0.0f, 0.0f, 1.0f);
                }
                if (con0.point.z == -A.halfSize.z)
                {
                    collisionNormal = new Vector3(0.0f, 0.0f, -1.0f);
                }

                con0.normal = collisionNormal;

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