Thursday, December 28, 2017

Simulation #1: 2D Pool game (part 1 - theory)

At the beginning, I would like to deal with pool game simulation in 2 dimensions.

Simulation is based on right implementation of system of balls moving in area of rectangle that symbolizes table. What is most essential and hardest to do for this simulation is to calculate velocity vector $\vec{V}$ for each ball in each moment. When it is done, it is easy to calculate position vector $\vec{r}$
Rectangle that symbolizes table has dimentions $LxW$. In its area, there are $N$ balls with radius $R$. Balls should bounce with each other and from table borders. We assume that, all colissions are elastic. Therefore in this case, considering the fact also that system is close the total momentum is constant. We can apply law of Conservation of Energy here as well, where energy of the system is only kinetic energy of the balls. We assume that balls have the same mass. Other parameters for this simulation:

$\vec{r}_0=[x_0, y_0]$, $\vec{V}_0=[V_{x0}, V_{y0}]$ - respectively initial position and velocity of ball
$\vec{r}(t)=[x(t), y(t)]$, $\vec{V}(t)=[V_{x}(t), V_{y}(t)]$ - respectively actual position and velocity of ball

Let's define time step $\delta t$. Ball position at the moment of t $\vec{r}(t)$ is: $$\vec{r}(t)=\vec{r}(t-\delta t)+\vec{V}(t-\delta t)\cdot\delta t \tag{0}$$ We can easily see, that position at moment of $t$ depends on position in the previous time step, i.e. $t-\delta t$ and on velocity vector. Velocity vector depends on time as well. Because we omit friction, a single ball, if it does not encounter an obstacle, moves in a straight linear motion, i.e. its velocity vector is constant. Vector $\vec{V}$ changes when it encounters an obstacle, that is:
  1. ball collids with the edge
  2. ball collids with another ball

Let's analyze case number 1 - collision with edge:
Velocity vector of ball before collision we can present as pair of components: $V_x$ and $V_y$. Next, we can treat 2D case as two separate 1D cases along $x$ and $y$ axis respectively. Because motion along $y$ axis has direction parallel to the edge (when consider collision on left or right edge), so $V_y$ component remains the same after collision: $V_y'$=$V_y$. In case of $x$ axis, we have collision with the edge in this direction. It is an analog to collision with another ball with infinite big mass. According to fact that total momentum is constant, therefore velocity after collision in this direction is $V_x'$=$-V_x$, so it this the same as before collision but with minus sign. After collision with top or bottom edge we have opposite situation. Component $V_y$ is changed to $-V_y$ and $V_x$ remains same.

Case 2 - collision with another ball:
This case is more complex, because it requires entering the reflection angle to calculation. It depends on it, how velocity components of both balls are added. Only the components of the velocity vector change in the normal direction (denoted $V_n$) to the point of contact of the balls $P_s$ during the collision. Components in the tangent direction $V_s$ remain unchanged. This is shown in the figure below:

Fig.2


Knowing the values of normal components, the reflection boils down to one-dimensional reflection. We set initial velocities for balls (let's call them 1 and 2) as input parameters for the simulation, respectively: $\vec{V_1}=[V_{x1},V_{y1}]$ oraz $\vec{V_2}=[V_{x2},V_{y2}]$. In order to calculate normal and tangent components, we need to express these vectors in the new coordinate system denoted by Fig. 2 through $x'$, $y'$. We can see in the figure that it is rotated in trelation to the $x$, $y$ system by the angle $\alpha$
To transform from one system to another, we use general formula for coordinate transform: $$\begin{cases}x'=xcos(\alpha)-ysin(\alpha)\\ y'=xsin(\alpha)+ycos(\alpha)\end{cases} \tag{1}$$ The angle $\alpha$ can easily be calculated, knowing that $tg(90^{\circ}-\alpha)=ctg(\alpha)$ is the The angle $\alpha$ can easily be calculated to know that $ tg(90^{\circ} - \alpha) = ctg (\alpha) $ is the slope of the line passing through the centers of balls and whose coordinates we know because they mark the position of our balls on the table. Finally, $\alpha=acrctg(\frac{y_1-y_0}{x_1-x_0})$. General formula for normal and tangent component of velocity vector for the first ball is thus: $$\begin{cases} V_{1s}=V_{1x}cos(\alpha)-V_{1y}sin(\alpha) \\ V_{1n}=V_{1x}sin(\alpha)+V_{1y}cos(\alpha)\end{cases} \tag{1b}$$ Analogously for the sphere no. 2. As I mention above, after the collision tangent component remain unchanged. Normal components swap, i.e. $V_{1n}'$ denotes normal component of velocity vector for ball no. 1 after collision is equal to normal component for ball no. 2 before collision: $ V_ {1n}' = V_ {2n} $. Similarly $V_{2n}'$ for ball number 2. Finally, normal and tangent component for both balls after collision: $$ \begin{cases} V_{1s}'=V_{1x}cos(\alpha)-V_{1y}sin(\alpha) \\ V_{1n}'=V_{2x}sin(\alpha)+V_{2y}cos(\alpha)\\ V_{2s}'=V_{2x}cos(\alpha)-V_{2y}sin(\alpha)\\ V_{2n}'=V_{1x}sin(\alpha)+V_{1y}cos(\alpha) \end{cases}\tag{2}$$ Finally, it remains to go back to the original coordinate system to present the velocity vector components of the two balls after reflection. For this purpose, we will again use the formulas (1). Ultimately, the components of the velocity of the balls after reflection reflect the following formula: Finally, we need to get back to initial dimentional system to present the velocity vector components of the two balls after collision. For this purpose, we will use the formulas (1) again. Ultimately, velocity components of the two balls after collisions are determined by the folowing formulas: $$ \begin{cases} V_{1x}'=V_{1s}'cos(\alpha)+V_{1n}'sin(\alpha) \\ V_{1y}'=V_{1n}'cos(\alpha)-V_{1s}'sin(\alpha) \\ V_{2x}'=V_{2s}'cos(\alpha)+V_{2n}'sin(\alpha) \\ V_{2y}'=V_{2n}'cos(\alpha)-V_{2s}'sin(\alpha) \end{cases} \tag{3}$$ It is enough now to put the formulas (2) into equations (3) to obtain the final form of the velocity components after mutual ball collision. This is as an aside, because we use a programming language, we can assign values to variables, so we do not need to know the final analytical form.

No comments:

Post a Comment