Pool Game (Billiards) - Reflection

Hello,

I’m trying to implement an aiming system into my pool game like this:

I’ve got it working, it fires a raycast from the ball, to the direction, and then a reflection is fired from that. The issue I’m having is that my ball doesn’t actually follow the raycast. It’s not very accurate.

I think this is because the balls are actually being moved by the Unity Physics and Rigidbody Force. How can I take this into account for my aiming system?

``````function Update () {

//Initial Direction
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
if (Physics.Raycast (transform.position, direction, hit, Mathf.Infinity)){
Debug.DrawLine (transform.position, hit.point, Color.red);

//Reflection
var reflectDir = Vector3.Reflect(direction, hit.normal);
reflectDir.x = reflectDir.x * offset;
var hitReflec : RaycastHit;

if(Physics.Raycast (hit.point, reflectDir, hitReflec, Mathf.Infinity)){
Debug.DrawLine(hit.point, hitReflec.point, Color.green);
}
}

if(Input.GetButtonDown("Fire1")){

}
}
``````

One thing to consider heavily when creating systems like this is Unity’s realistic ability to account for friction.

For example:
Executing that shot on a real pool table, it would be near impossible to replicate the path shown in your diagram.

Here’s why ( Given: cue ball is hit dead center no additional SIDE OR TOP spin is introduced ):

Pool table rails (very bouncy rubber covered with cloth) will impart friction on a ball. ALSO, the rubber is somewhat soft which means the ball will PUSH or sink into the rubber in an amount directly proportional to the velocity of the ball. Which means MORE FRICTION is imparted. This is why the harder you hit a ball into a rail on a pool table, the tighter the rebound angle.

Those two factors influence the friction imparted (especially to a multi rail shot) In pool, this is called ROLLING ENGLISH. This means that every rail collision adds side spin (on Y Axis). So when that ball hits another rail that spin will influence the rebound angle - effectively widening the angle.

SO… If I were a betting man, based on the diagram supplied, I would wager that your ball hit the third rail CLOSER to the upper right corner pocket, and very likely scratched in the lower left corner pocket with enough speed.

Hope that helps!

I had this issue until I changed the physics material in default physics settings to an actual material that had no friction.

@oliver-jones
This is because of the bounciness of the physics materials of the side rails and the ball. Set both to one to follow line exactly.
To slow down ball, use some sort of deceleration function. Hope this helps.

@oliver-jones I know that the anwser is pretty old but… Maybe the innacurace comes from the raycast and not the ball itself. Ball has a radius, but raycast not, is just a single point searching for collision. Different volume objects will get different results. To get an accurate bounce with Raycast use Spherecast instead, and provide the ball rigidbody radius to get expected results.

i was having the same problem and what a did was create a slow down every time that the object collide simulating the bounciness velocity reduction.

`void OnCollisionEnter(Collision col) {

``````	if (col.gameObject.name == "GolfTableWall") {
rb.velocity = rb.velocity * 0.5f;
``````

}`

but you have to keep in 1 the bounciness of the matirial so the angle after thebounciness will be the same as the raycast.

be carefull with the Bounce Combine mode.