Esempio n. 1
0
 public static bool AABBAABBCollisionOccured(PSI_AABBCollider col1, PSI_AABBCollider col2)
 {
     return((col1.pPosition.x < col2.pPosition.x + col2.Size.x) &&
            col1.pPosition.x + col1.Size.x > col2.pPosition.x &&
            col1.pPosition.y < col2.pPosition.y + col2.Size.y &&
            col1.pPosition.y + col1.Size.y > col2.pPosition.y &&
            col1.pPosition.z < col2.pPosition.z + col2.Size.z &&
            col1.pPosition.z + col1.Size.z > col2.pPosition.z);
 }
Esempio 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;
    }