- Home /
Ball Aiming System
Hello,
I'm trying to make a ball aiming system so when a user hits a ball, they can see where the ball is going to go. Like what you see on many snooker games.
Right now, I have a raycast that fires forward into the aim direction which draws a Line Render when a ball is in the ray. But this isnt accurate, as for one, it doesn't take the ball radius into account.
I also want to so the deflection of the balls, very much like this:
So I need to take into account the balls entire radius, so maybe some kind of capsule raycaster, from there, I then need to calculate the deflection of the ball that was initially aimed and fired, and the direction of the ball that was hit by the initial. I have a little something:
if (Physics.Raycast (t1, direc, h1, Mathf.Infinity, l2)){
Debug.DrawLine (t1, h1.point, Color.red);
radiusDummy.localPosition.x = 0;
Debug.DrawLine (radiusDummy.position, h1.transform.position, Color.blue);
//var reflection = direc +
//Debug.DrawLine (radiusDummy.position, h1.transform.position, Color.green);
}
So the t1/h1 raycast is firing from the very most edge of the ball on the left side, and fired forward (where the user is aiming). RadiusDummy is basically a duplicate of the collider radius of the initial ball, but I have to move this manually at the moment. When I move this dummy on the Z axis until its barely touching another ball, the Blue DrawLine is positioned from the centre of the dummy, to the centre of the ball it is touching - this calculates the directional hit of this ball.
First of all, is using this dummy method good? How can I move it automatically? Secondly, I now need to calculate the deflection of the initial ball.
Would appreciate the feedback I can get. Thanks.
Hi! I found your question by searching on google. Do you already have figured out how to take into account the radius of the ball? I'm curious because i have the same problem. All the best! -Oscar
Answer by smallbit · Nov 26, 2013 at 12:35 PM
I have read once a tutorial how to make that aiming and deflection trick on pool game. The general Idea was to cast invisible ball in the direction of your shooting until it collides with something. Haven't thought on the Unity application but you could cast some invisible or alpha 0 sphere with trigger or something like that. I will just write here the theory they used for that (sadly I couldn't find the article)
Than there was so math geometry involved: If there was collision detected between balls G and 2, the where to go ball vector was starting from the ball that was hitting (in you case letter G) in the direction of ball that was hit (2) from center to center. Than you multiply it to have it a little bit longer
The deflection was based on angle between direction of shot and the above mentioned where to go vector in perfect world if would deflect at 90 degrees I think. Note that in your image the vector of deflecting going back from G does not appear to be correct, that vector is parallel to the whereTogo vector.
Than of course there were issues about the polycount for ball that the higher it was the smoother and more precisely looking the ball collisions were.
Here you have some theory on elastic colisions (including balls) and some formulas for force etc (http://en.wikipedia.org/wiki/Elastic_collision)
Your answer
Follow this Question
Related Questions
Simulating the graphics of a rolling 3D ball in a 2D game 2 Answers
Player-controlled rigidbody ball sometimes slowing down for no reason 0 Answers
How can I show trajectory of a bouncing ball OnCollisionEnter/OnCollisionExit? 0 Answers
Ball jump on collision problem 1 Answer
Simple Collision Deflection (Pong) 1 Answer