Exemplo n.º 1
0
    private void CollisionCapsuleBox()
    {
        FixedList128 <float3> verticesOBB = ColPhysics.GetAABBVerticesOBB(posB, extentsB);

        verticesOBB = ColPhysics.GetRotatedVerticesOBB(verticesOBB, posB, rotB);
        FixedList128 <float3> normalAxesOBB = ColPhysics.GetAxisNormalsOBB(verticesOBB[0], verticesOBB[1], verticesOBB[3], verticesOBB[4]);
        FixedList128 <float>  exte          = new FixedList128 <float>();

        exte.Add(extentsB.x);
        exte.Add(extentsB.y);
        exte.Add(extentsB.z);

        float3x2 capsuleTips    = ColPhysics.GetCapsuleEndPoints(posA, rotA, extentsA.y * 2);
        float3x2 capsuleSpheres = ColPhysics.GetCapsuleEndSpheres(capsuleTips.c0, capsuleTips.c1, extentsA.x);

        if (ColPhysics.CapsuleIntersectsBox(capsuleSpheres, posB, extentsA.x, normalAxesOBB, exte, out float distance))
        {
            isColliding = true;

            if (resolveCollisions)
            {
                ColPhysics.ResolveSphereBoxCollision(ref posA, extentsA.x, ref posB, distance);
            }
        }
    }
Exemplo n.º 2
0
    private void CollisionSphereBox()
    {
        float3 spherePos    = posA;
        float3 obbPos       = posB;
        float3 obbRot       = rotB;
        float3 obbHalfSize  = extentsB;
        float  sphereRadius = extentsA.x;

        FixedList128 <float3> vertices = ColPhysics.GetAABBVerticesOBB(obbPos, obbHalfSize);   // calculated once at startup forever stored with entity

        vertices = ColPhysics.GetRotatedVerticesOBB(vertices, obbPos, obbRot);
        FixedList128 <float> extents = new FixedList128 <float>(); // calculated once at startup forever stored with entity

        extents.Add(extentsB.x);
        extents.Add(extentsB.y);
        extents.Add(extentsB.z);

        FixedList128 <float3> axisNormals = ColPhysics.GetAxisNormalsOBB(vertices[0], vertices[1], vertices[3], vertices[4]);

        if (ColPhysics.SphereIntersectsBox(spherePos, sphereRadius, obbPos, axisNormals, extents, out float distance))
        {
            isColliding = true;

            if (resolveCollisions)
            {
                ColPhysics.ResolveSphereBoxCollision(ref spherePos, sphereRadius, ref obbPos, distance);
                posA = spherePos;
                posB = obbPos;
            }
        }
    }