# detect which ball is going to be hit

Discussion in 'Physics' started by dccoo, Feb 7, 2018.

1. ### dccoo

Joined:
Oct 3, 2015
Posts:
113
Suppose the white sphere is gonna be thrown in direction of the others like in the image.

How can I detect which sphere is going to be touched (by the white) first?

Joined:
Jan 21, 2014
Posts:
747

Joined:
Oct 3, 2015
Posts:
113
4. ### PGJ

Joined:
Jan 21, 2014
Posts:
747
It works almost exactly like RayCast, with the exception that you supply a radius as well. It will return the closest object that the ball will hit. So, it shouldn't be too heavy to use.

5. ### dccoo

Joined:
Oct 3, 2015
Posts:
113
SphereCast creates a fixed sphere somewhere in the space and if I want to get the nearest ball, I would have to move this sphere forwards (in the white ball direction) until it touches something.

Detail: I want to know where it is going to hit before the white moves, not during its movement, that's why I'm saying the SphereCast may have to move.

6. ### dgoyette

Joined:
Jul 1, 2016
Posts:
630
The white ball doesn't need to be moving for the spherecast to work, but you do need to know which direction it will move. That's the direction you send the spherecast, and see what gets hit.

However, I'm not convinced that a spherecast is the right choice here, and I believe you'll need to do something a little more complicated. Consider a "real" pool game, where you can hit the cue ball off-center to generate spin. In those cases, the ball would move along an arc, not in a straight line, so a spherecast wouldn't work.

But if you're not putting spin on the ball, then you can use a spherecast in the direction you're currently "aiming" the cue stick, and that should tell you the first thing the white ball will hit.

dccoo likes this.
7. ### dccoo

Joined:
Oct 3, 2015
Posts:
113
That's the point, how should I move the spherecast every frame efficiently?

8. ### dgoyette

Joined:
Jul 1, 2016
Posts:
630
Well, just to be clear, you don't "move" a spherecast. A spherecast is an instantaneous check you perform in a single frame. You just say how far you want it to try to go. It's the equivalent of saying, "If I were to move this sphere forward up to a certain distance, what would it hit?" or, "What things would I hit if I moved this sphere from A to B?" You don't actually move the sphere, though.

However, you can perform a new spherecast on each frame, using whatever direction your cue stick is currently aiming. Just put the spherecast call in the Update() method, setting the proper direction each frame. Spherecasting on each frame should be fine from an efficiency standpoint.