Billiard power and direction algorithm

324 Views Asked by At

Board clear Image Board with 2 sample shot

Important: White ball and black ball are exactly one(1) pixle not bigger

Hello. I want to write algorithm that computer play billiard and hit specific ball in the current table for example

dimensions [3, 2]
White ball position: [1, 1]
Black ball position: [2, 1]
Hit power(distance): 4

White ball can shot in 7 direction vector

[1, 0], [1, 2], [1, -2], [3, 2], [3, -2], [-3, 2], and [-3, -2]

As specific examples, the shot at bearing [1, 0] is the straight line horizontal shot of distance 1, the shot at bearing [-3, -2] bounces off the left wall and then the bottom wall before hitting the black ball with a total shot distance of sqrt(13), and the shot at bearing [1, 2] bounces off just the top wall before hitting the black ball with a total shot distance of sqrt(5)

Is there any algorithm to find hitting direction and give final position?

1

There are 1 best solutions below

3
On

I would investigate a mirroring solution.

If [x,y] is the coordinate of the black ball, then [-x, y], [x, -y], [-x, -y] are virtual black ball positions that would cause a hit with bounces.

Depending on 'distance', and table 'height' h, 'width' w, you may need to extend the virtual ball positions with [2*h $\pm$ x, 2*w $\pm$ y] like virtual ball positions, to allow bouncing multiple times on long/short edges.

Check if the distance to the white ball and angle from white to black (virtual) ball is allowed.

In your second example, a virtual black ball is positioned at [2, -1], and the direction is [2, -1] - [1, 1] = [1, -2], is in your list.