Exemple #1
0
    //Since the number of objects in play are low in number and all are spheres, we're gonna check each against the other in every fixed update

    void FixedUpdate()
    {
        if (balls != null && balls.Length > 0)
        {
            for (int i = 0; i < balls.Length; i++)
            {
                //Check for Cushion collision in here as well
                for (int k = 0; k < cushions.Length; k++)
                {
                    Ball    ball    = balls[i];
                    Cushion cushion = cushions[k];
                    //Vector3 possibleContactPoint = cushion.center - ball.getCenter();
                    //An early escape test, for if the ball isn't moving towards the cushion, there is no point in doing further collision checks between them
                    //if(Vector3.Dot(possibleContactPoint,ball.getVelocity())>0){
                    bool collisionHappened = CollisionResolutionBallCushion(ball, cushion);

                    if (collisionHappened)
                    {
                        //Calculate The Impulse between the cushion and the ball
                        Vector3 impulseVector = sheet.CalculateImpulseVectorBallCushion(ball, cushion);

                        //Apply the force to the ball
                        ball.AddForce(impulseVector);
                    }
                    // }
                }


                for (int j = i + 1; j < balls.Length; j++)
                {
                    Ball A = balls[i];
                    Ball B = balls[j];
                    checkForCollisionBetweenBalls(A, B);
                }
                //Check for whether the ball is in on the plane or has fell in a pocket
                if (hasBallFallenIntoAPocketOrGoneOutOfBounds(balls[i]))
                {
                    balls[i].putOutOfAction();
                }
            }
        }
    }