Search Unity

Help with code for Collision Detection and Reaction..

Discussion in 'Physics' started by EliJNemer, Jun 10, 2020.

  1. EliJNemer

    EliJNemer

    Joined:
    Aug 3, 2019
    Posts:
    123
    hello,

    in my billiard game i have written all the physics, at the moment i am working on the collision detection.

    in some cases where balls have ample space between them the collision works as i want. but when i apply the break shot unity freezes a little it may crash or it might resolve the collsiion but the balls will jump into their final positions..

    can someone help me with resolving this issue?

    here is footage of the issue:



    here is the code for the Collision Detection:

    Code (CSharp):
    1. private void CheakForCollision()
    2.     {
    3.         for(int i = 0; i <= 15; i++)
    4.         {
    5.             ballRB[i].transform.position = originalPosition[i];
    6.         }
    7.  
    8.         for(int i = 0; i <= 5; i++)
    9.         {
    10.             for(int j = 0; j <= 15; j++)
    11.             {
    12.                 ballRB[j].transform.position += velocity[j] * Time.fixedDeltaTime * 0.2f;
    13.             }
    14.  
    15.             for(int j = 0; j <= 15; j++)
    16.             {
    17.                 Collider[] hitColliders;
    18.                 int hitCollidersLength;
    19.                 int layerMask = 1 << 17;
    20.                 int n = 0;
    21.                 int r = 0;
    22.  
    23.                 hitCollidersLength = Physics.OverlapSphere(ballRB[j].transform.position, 0.05715f, layerMask).Length;
    24.  
    25.                 for (int k = 0; k < hitCollidersLength; k++)
    26.                 {
    27.                     hitColliders = Physics.OverlapSphere(ballRB[j].transform.position, 0.05715f, layerMask);
    28.                     if(hitColliders[k] != null)
    29.                     {
    30.                         if (hitColliders[k].gameObject.tag != ballRB[j].gameObject.tag)
    31.                         {
    32.                             for (int m = 0; m <= 15; m++)
    33.                             {
    34.                                 if (ballRB[m].gameObject.name == hitColliders[k].gameObject.name)
    35.                                 {
    36.                                     if (velocity[j].magnitude < velocity[m].magnitude)
    37.                                     {
    38.                                         n = j;
    39.                                         r = m;
    40.                                     }
    41.                                     else if (velocity[j].magnitude > velocity[m].magnitude)
    42.                                     {
    43.                                         r = j;
    44.                                         n = m;
    45.                                     }
    46.                                 }
    47.                             }
    48.  
    49.                             while (Vector3.Distance(ballRB[n].transform.position, ballRB[r].transform.position) <= ballDiameter)
    50.                             {
    51.                                 ballRB[r].transform.position -= velocity[r] * (Time.fixedDeltaTime * 0.02f);
    52.                                 ballRB[n].transform.position -= velocity[n] * (Time.fixedDeltaTime * 0.02f);
    53.  
    54.                                 for (int p = 0; p <= 15; p++)
    55.                                 {
    56.                                     if (ballRB[p].gameObject.name != ballRB[r].gameObject.name && ballRB[p].gameObject.name != ballRB[n].gameObject.name)
    57.                                     {
    58.                                         ballRB[p].transform.position -= velocity[p] * (Time.fixedDeltaTime * 0.02f);
    59.                                     }
    60.                                 }
    61.                             }
    62.  
    63.                             Vector3 normal = -new Vector3(ballRB[r].transform.position.x - ballRB[n].transform.position.x, ballRB[r].transform.position.y - ballRB[n].transform.position.y, ballRB[r].transform.position.z - ballRB[n].transform.position.z);
    64.                             float angleBetween = Vector3.Angle(velocity[r], normal);
    65.                             print("Normal: " + normal);
    66.                             print("angleBetween: " + angleBetween);
    67.  
    68.                             Vector3 cross = Vector3.Cross(velocity[r], normal);
    69.  
    70.                             print("velocity[j]: " + velocity[r] + ", velocity[n]: " + velocity[n]);
    71.  
    72.                             float velLost1 = (velocity[r].magnitude * Mathf.Cos(angleBetween * Mathf.Deg2Rad));
    73.                             float velLost2 = (velocity[n].magnitude * Mathf.Cos(angleBetween * Mathf.Deg2Rad));
    74.  
    75.                             print("VelLost1: " + velLost1);
    76.                             print("velLost2: " + velLost2);
    77.  
    78.                             float newVel1Mag = 0;
    79.                             float newVel2Mag = 0;
    80.  
    81.                             ballRB[r].angularVelocity = Vector3.zero;
    82.                             ballRB[n].angularVelocity = Vector3.zero;
    83.  
    84.                             newVel1Mag = velocity[r].magnitude - velLost1 + (velLost2 * 0.92f);
    85.                             newVel2Mag = velocity[n].magnitude - velLost2 + (velLost1 * 0.92f);
    86.  
    87.                             print("newVel1: " + newVel1Mag + "newVel2: " + newVel2Mag);
    88.  
    89.                             ballDirections[r] = Vector3.Cross(normal, Vector3.up) / Vector3.Cross(normal, Vector3.up).magnitude;
    90.                             ballDirections[n] = normal / normal.magnitude;
    91.  
    92.                             print("Direction r: " + ballDirections[r]);
    93.  
    94.                             if (cross.y > 0)
    95.                             {
    96.                                 velocity[r] = newVel1Mag * ballDirections[r];
    97.                             }
    98.                             if (cross.y < 0)
    99.                             {
    100.                                 velocity[r] = newVel1Mag * ballDirections[r] * (-1);
    101.                             }
    102.                             if (cross.y == 0)
    103.                             {
    104.                                 velocity[r] = Vector3.zero;
    105.                             }
    106.  
    107.                             velocity[n] = newVel2Mag * ballDirections[n];
    108.  
    109.                         }
    110.                     }
    111.                  
    112.                 }
    113.             }
    114.         }
    115.     }
     
    Last edited: Jun 10, 2020