8 ball pool ball path prediction

Hello, i am developing 8 ball pool template. All i am trying to achieve here is predict the other ball path even before collision with the white ball.
I did all calculations as suggested by many blogs/people, but the thing is the result is not accurate.

What is the solution for this? Some say due to rotations the path prediction is not accurate. Some say we need to tweet physics values. If so, what should i tweek?

My Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameplayController : MonoBehaviour
{
[SerializeField] GameObject _cue;
[SerializeField] GameObject _whiteBall;

int _layerMask = 1 << 8;

RaycastHit2D hit;
// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    CueRotation();
    ShootWhiteBall();

    hit = Physics2D.CircleCast(_whiteBall.transform.position, _whiteBall.GetComponent<CircleCollider2D>().radius, _cue.transform.right, Mathf.Infinity, _layerMask);
    if(hit.collider != null)
    {
        Debug.DrawLine(_cue.transform.position, hit.centroid, Color.white, 0, false);

        if (hit.transform.gameObject.CompareTag("Ball"))
        {
            Vector3 targetDir = (hit.transform.position - new Vector3(hit.centroid.x, hit.centroid.y, 0)).normalized * 10;
            Debug.DrawRay(hit.centroid, -hit.normal * 5.0f, Color.red, 0);
        }
    }
}

void CueRotation()
{
    _cue.transform.Rotate(0, 0, -Time.deltaTime * Input.GetAxis("Mouse Y") * 500.0f);
}

void ShootWhiteBall()
{
    if (Input.GetMouseButtonDown(0))
    {
        _whiteBall.GetComponent<Rigidbody2D>().AddForce(_cue.transform.right * 10.0f,ForceMode2D.Impulse);
    }
}

}

do you have the rotations on all balls frozen? if not you can not predict this like this as the balls will start to spin on impact.
Other then that what you do is basically the analytical solution with is correct in itself. However unity does a numerical solution when it comes down to actually simulating physics. So as long as you rely on Unitys builtin physics it will never be completly 100% accurate.

Nothing more to say here unless you provide more details. “The prediction is not accurate” can mean anything.

you could use the direction and speed of the calculation u’ve done to apply a force to the red ball when entering its trigger with the white ball. u would want to disable the normal collisions tho.

You should just forget about trying to predict the outcome manually by essentially recreating the entirety of PhysX or Box2D. Most of such games simply simulate the actual game ahead of time X amount of seconds into the future. Keep in mind that you can do this simulation completely invisible. Maybe even on a seperate layer. Unity now gives us the ability to manually advance the physics simulation. This can be done in a for loop to simulat a certain time into the future, all in one frame. Though depending on the performance you may want to limit the simulations so they are not carried out every frame but only in certain intervals.

For the simulation you usually use a seperate set of game objects that only have the colliders and rigidbodies attached. Keep in mind that you should use the same time step in the Simulate method that is used normally in order to get the same result. By default FixedUpdate runs at a timestep of 0.02 so we get exactly 50 frames. So when using a for loop with 50 cycles we can simulate 1 second into the future.

Since we have to essentially stop the “normal” objects from simulating during our prediction, we could simply detach the actual visuals from the physics representation. We could even “record” the actual movement of all balls during the simulation every step and once the shot is actually made you just “replay” the previous simulation by moving the visual gameobject along the recorded paths.

you may also want to look into the “relatively” new PhysicsScene which allows you to decouple and seperate different physics scenes. I would love to be able to create physics scenes manually, but as for now it seems we can only create them for seperate scenes. Having completely independent physics scenes would also allow many other kind of simulations (like a server for multiple games at the same time).