- Home /
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")){
rigidbody.AddForce(transform.TransformDirection(Vector3.forward) * initialForce, ForceMode.Impulse);
}
}
How bad is it not following? Compared to the picture you show, do you get a totally different path?
Fact is, the real path will depend on where you exactly hit the white ball with the stick. The given path is the one you get if tapping exactly in the middle of the sphere with no friction.
Then there is the torque due to rotation, this one will make it harder I guess to predict the exact path.
So in order to get a more accurate but not 100%, you would have to get the hit point of the stick to the ball and define the raycast from there taking consideration of the torque. Since I do not know where to start with this, I 'm sorry I'm oot (Dragon's den style).
The cue doesn't actually hit the pool ball, it just applies force to the general forward direction of the ball. The aim system isn't completely inaccurate, but just enough not to rely on it.
Thanks
can you just move the ball to each point yourself and gradually slow it down ins$$anonymous$$d of using the physics?
Answer by PaulzCreative · Jun 16, 2015 at 10:51 AM
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!
Answer by tellemstevedave · May 23, 2014 at 06:54 PM
I had this issue until I changed the physics material in default physics settings to an actual material that had no friction.
Answer by shashank_gargeshwari · Apr 29, 2016 at 07:38 PM
@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. :D
Answer by Fran-Matsusaka · Feb 22, 2017 at 10:25 AM
@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.
Answer by Moligon · Apr 27, 2021 at 12:35 AM
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.