Ejemplo n.º 1
0
    //------------------------------------Private Functions-------------------------------------

    private void CheckForCollision(PSI_Collider col1, PSI_Collider col2)
    {
        // Sphere on sphere.
        if (col1.pColliderType == ColliderType.Sphere && col2.pColliderType == ColliderType.Sphere)
        {
            if (PSI_Physics.SphereSphereCollisionOccured((PSI_SphereCollider)col1, (PSI_SphereCollider)col2))
            {
                PSI_Physics.HandleSphereSphereCollision((PSI_SphereCollider)col1, (PSI_SphereCollider)col2);
                FindObjectOfType <DebugManager>().CollisionOccured(col1, col2);
            }
        }

        // AABB on AABB.
        if (col1.pColliderType == ColliderType.AABB && col2.pColliderType == ColliderType.AABB)
        {
            if (PSI_Physics.AABBAABBCollisionOccured((PSI_AABBCollider)col1, (PSI_AABBCollider)col2))
            {
                PSI_Physics.HandleAABBAABBCollision((PSI_AABBCollider)col1, (PSI_AABBCollider)col2);
                FindObjectOfType <DebugManager>().CollisionOccured(col1, col2);
            }
        }

        // Sphere on plane.
        if ((col1.pColliderType == ColliderType.Sphere && col2.pColliderType == ColliderType.Plane) ||
            (col1.pColliderType == ColliderType.Plane && col2.pColliderType == ColliderType.Sphere))
        {
            PSI_SphereCollider sphere = (PSI_SphereCollider)((col1.pColliderType == ColliderType.Sphere) ? col1 : col2);
            PSI_PlaneCollider  plane  = (PSI_PlaneCollider)((col1.pColliderType == ColliderType.Sphere) ? col2 : col1);
            if (PSI_Physics.SpherePlaneCollisionOccured(sphere, plane))
            {
                PSI_Physics.HandleSpherePlaneCollision(sphere, plane);
                FindObjectOfType <DebugManager>().CollisionOccured(col1, col2);
            }
        }
    }
Ejemplo n.º 2
0
    public static void HandleAABBAABBCollision(PSI_AABBCollider col1, PSI_AABBCollider col2)
    {
        // Calculating the collision vector.
        Vector3 colVector = col1.pPosition - col2.pPosition;

        // Determining what axis the collision occured on.
        List <float> colVectorAxes = new List <float>()
        {
            Mathf.Abs(colVector.x), Mathf.Abs(colVector.y), Mathf.Abs(colVector.z)
        };

        colVectorAxes.Sort();
        string axisName = "";

        if (Mathf.Abs(colVector.x) == colVectorAxes[2])
        {
            axisName = "x";
        }
        if (Mathf.Abs(colVector.y) == colVectorAxes[2])
        {
            axisName = "y";
        }
        if (Mathf.Abs(colVector.z) == colVectorAxes[2])
        {
            axisName = "z";
        }

        // Generating an axis aligned collision vector.
        object tempVector = Vector3.zero;

        typeof(Vector3).GetField(axisName).SetValue(tempVector, typeof(Vector3).GetField(axisName).GetValue(colVector));
        Vector3 aaColVector = (Vector3)tempVector;

        // Getting the size of each collider on the collision axis.
        float size1 = (float)col1.Size.GetType().GetField(axisName).GetValue(col1.Size);
        float size2 = (float)col2.Size.GetType().GetField(axisName).GetValue(col2.Size);

        // Calculating the minimum translation vector needed to reset the two colliders positions.
        var minTranslationVector = aaColVector * ((((size1 + size2) * 0.5f) - aaColVector.magnitude) / aaColVector.magnitude);

        // Calculating the inverse masses once for efficiency.
        var inverseMass1 = 1.0f / col1.pRigidBody.pMass;
        var inverseMass2 = 1.0f / col2.pRigidBody.pMass;

        // Moving the two colliders so they are not overlapping.
        col1.transform.Translate(minTranslationVector * (inverseMass1 / (inverseMass1 + inverseMass2)));
        col2.transform.Translate(-minTranslationVector * (inverseMass2 / (inverseMass1 + inverseMass2)));

        // Applying the impulse to the bodies.
        var impulse = PSI_Physics.CalculateImpulse(col1.pRigidBody, col2.pRigidBody, minTranslationVector);

        col1.pRigidBody.Velocity += impulse.Impulse1;
        col2.pRigidBody.Velocity += impulse.Impulse2;
    }
Ejemplo n.º 3
0
    //-----------------------------------Handling Collisions------------------------------------

    public static void HandleSphereSphereCollision(PSI_SphereCollider col1, PSI_SphereCollider col2)
    {
        // Calculating the collision vector.
        var colVector = col1.pPosition - col2.pPosition;

        // Calculating the minimum translation vector needed to reset the two colliders positions.
        var minTranslationVector = colVector * (((col1.Radius + col2.Radius) - colVector.magnitude) / colVector.magnitude);

        // Calculating the inverse masses once for efficiency.
        var inverseMass1 = 1.0f / col1.pRigidBody.pMass;
        var inverseMass2 = 1.0f / col2.pRigidBody.pMass;

        // Moving the two colliders so they are not overlapping.
        col1.transform.Translate(minTranslationVector * (inverseMass1 / (inverseMass1 + inverseMass2)));
        col2.transform.Translate(-minTranslationVector * (inverseMass2 / (inverseMass1 + inverseMass2)));

        // Applying the impulse to the bodies.
        var impulse = PSI_Physics.CalculateImpulse(col1.pRigidBody, col2.pRigidBody, minTranslationVector);

        col1.pRigidBody.Velocity += impulse.Impulse1;
        col2.pRigidBody.Velocity += impulse.Impulse2;
    }
Ejemplo n.º 4
0
    public static void HandleSpherePlaneCollision(PSI_SphereCollider sphereCol, PSI_PlaneCollider planeCol)
    {
        float   distToProjectedPoint = Vector3.Dot(planeCol.pNormal, (sphereCol.pPosition - planeCol.pPosition));
        Vector3 projectedPoint       = sphereCol.pPosition - distToProjectedPoint * planeCol.pNormal;

        // Calculating the collision vector.
        var colVector = sphereCol.pPosition - projectedPoint;

        // Calculating the minimum translation vector needed to reset the sphere.
        var minTranslationVector = colVector * ((sphereCol.Radius - colVector.magnitude) / colVector.magnitude);

        // Moving the sphere collider so it is not overlapping.
        sphereCol.transform.Translate(minTranslationVector);

        // Applying the impulse to the bodies.
        if (planeCol.pRigidBody != null)
        {
            sphereCol.pRigidBody.Velocity += PSI_Physics.CalculateImpulseAgainstStaticRB(sphereCol.pRigidBody, planeCol.pRigidBody, minTranslationVector);
        }
        else
        {
            sphereCol.pRigidBody.Velocity += PSI_Physics.CalculateImpulseAgainstStaticCollider(sphereCol.pRigidBody, minTranslationVector);
        }
    }