예제 #1
0
    public bool CheckCollisions()
    {
        bool hasCollided = false;

        for (int i = 0; i < otherRigidBody.Count; i++)
        {
            BasicRigidBody other = otherRigidBody[i];
            Vector3        point;
            Vector3        normal;

            if (SphereSphereCollision(other, out point, out normal))
            {
                hasCollided = true;
                Vector3 newVelocity  = PhysicsManager.VectorAfterNormalForce(velocity, normal);
                Vector3 lostVelocity = velocity - newVelocity;

                Vector3 o_newVelocity = PhysicsManager.VectorAfterNormalForce(other.velocity, -normal);
                Vector3 o_losVelocity = other.velocity - o_newVelocity;

                Vector3 avgLostVelocity = (lostVelocity + o_losVelocity) / 2.0f;

                velocity       = newVelocity * physManager.friction + avgLostVelocity;
                other.velocity = o_newVelocity * physManager.friction + avgLostVelocity;

                Vector3 differenceVector = point - position;
                float   squareMag        = differenceVector.sqrMagnitude;
                float   dist             = Mathf.Sqrt(squareMag);

                Vector3 offset = normal * (radius - dist);

                position += offset;

                other.position -= offset;
            }
        }
        for (int i = 0; i < otherColliders.Count; i++)
        {
            BoxCollider other = otherColliders[i];
            Vector3     point;
            Vector3     normal;

            if (SphereAABBCollision(other, out point, out normal))
            {
                if (other.gameObject.CompareTag("Axis_Y_Win"))
                {
                    if (playerWin == 1)
                    {
                        ruleManager.AddPlayerScore();
                    }
                    else if (playerWin == 2)
                    {
                        ruleManager.AddOtherScore();
                    }

                    playerWin = 3;
                }
                hasCollided = true;
                //Vector3 newVelocity = PhysicsManager.VectorAfterNormalForce(velocity, normal);

                //velocity = newVelocity * physManager.friction;

                // For temp solution this is fine but not ideal in the most case
                if (other.gameObject.CompareTag("Axis_Y_PlayerSideTable"))
                {
                    PlayerSideBounce.Play();
                    velocity.y = -velocity.y;
                }
                else if (other.gameObject.CompareTag("Axis_Y_OtherSideTable"))
                {
                    AISideBounce.Play();
                    velocity.y = -velocity.y;
                }
                else if (other.gameObject.CompareTag("Axis_Z_PlayerBat"))
                {
                    BatHit.Play();
                    ruleManager.ChangeToBlue();
                    if (!addPoint)
                    {
                        ruleManager.AddRallyCount();
                        addPoint = true;
                    }
                    Vector3 destination = new Vector3(ruleManager.GetCurrentAim().x, ruleManager.GetCurrentAim().y, ruleManager.GetCurrentAim().z);
                    Vector3 heading     = (destination - transform.position);//.normalized;

                    velocity  = heading;
                    velocity  = new Vector3(velocity.x, velocity.y + Y_VelocityAddition, velocity.z - Z_VelocityAddition);
                    playerWin = 1;
                }
                else if (other.gameObject.CompareTag("Axis_Z_OtherBat"))
                {
                    addPoint = false;
                    BatHit.Play();
                    ruleManager.ChangeToRed();
                    Vector3 destination = new Vector3(Random.Range(-6.15f, -7.73f), PlayerSideZone.transform.position.y, PlayerSideZone.transform.position.z);
                    Vector3 heading     = (destination - transform.position);//.normalized;

                    velocity  = heading;
                    velocity  = new Vector3(velocity.x, velocity.y + Y_VelocityAddition, velocity.z + (Z_VelocityAddition + 0.3f));
                    playerWin = 2;
                }
                else if (other.gameObject.CompareTag("Axis_X"))
                {
                    velocity.x = -velocity.x;
                }

                Vector3 differenceVector = point - position;
                float   squareMag        = differenceVector.sqrMagnitude;
                float   dist             = Mathf.Sqrt(squareMag);
                Vector3 offset           = normal * (radius - dist);

                position += offset;
                velocity -= normal * Dot(velocity, differenceVector);
            }
        }
        return(hasCollided);
    }