Given the position of a ball, the diameter, a direction/target position and the 4 edges of a rectangle(2:1 ratio) how can I find the end position (coordinates of a point) of a ball if it collides with one of the walls? Is there any formula or an easier method than mine? And how would the diameter change the outcome (hits wall at different position => ends up in a different position, but I don't know how to find it)?
Example of given Field, Ball and Target
Here is a solution I thought of (diameter = 0):
- Create the field lines
- Create line of ball travel
- Check if and where ball travel line and one of the field lines meet (find the exact point) (Using this formula)
- Find perpendicular line to the field line that crosses it at the wall hit point (3.step)
- Find parallel line to field wall crossing the ball position
- Find mirror point of ball position and the perpendicular line (4.step line)
- Create line from mirror point (6.step) and wall hit position (3.step)
- Calculate distance between ball pos and target
- Subtract distance between ball pos and wall hit (3.step) from ball pos and target (Using this formula to find the leftover
- Check if the leftover will hit another wall and repeat steps 4-9
- Using the same formula from 9.step by adding it to the wall hit position (3.step) find the point where the ball will end on.
Edit:Sorry if this is the wrong place to post this (maybe I should've posted it in https://physics.stackexchange.com/ ?)
Let's say then we have a point-like ball inside a rectangle (the field). The ball would travel from its starting point to some other point (the target $T$), but if its path crosses a side of the rectangle then it bounces back, and it goes on like that until the total length of its path is the same as the distance from start to target.
Suppose first of all the field is simply the square $[0,1]\times[0,1]$. If you reflect the target about the left or bottom wall, the target will simply change the sign of its $x$ or $y$ coordinate. If it is reflected about the right or top wall, its coordinates will switch to $2-x$ or $2-y$.
Hence there is a simple algorithm to find the end position of the ball: if coordinate $x$ or $y$ of the target is negative, substitute it with $-x$ or $-y$; if coordinate $x$ or $y$ of the target is greater than $1$, substitute it with $2-x$ or $2-y$; stop when both coordinates are in the range $0\le x,y\le1$.
Let's make an example. If the target is at $(3.3,-0.8)$ you have: $$ (3.3,-0.8)\to(-1.3,0.8)\to(1.3,0.8)\to(0.7,0.8) $$ the last point being the final position of the ball. Note that the actual path depends on the starting point (see figure below) but the ending point does not, and can be found with the above algorithm.
If the field is a generic rectangle $ABCD$, then we can remap it to the unit square as follows. Choose one of the vertices as origin (for instance $A$) and take as coordinate vectors $$ \mathbf{u}=B-A,\quad \mathbf{v}=D-A. $$ You can now write the relative position of the target as $T-A=\alpha \mathbf{u}+\beta \mathbf{v}$ and apply the above algorithm to coordinates $(\alpha,\beta)$. In fact, you can check that with these new coordinates $A=(0,0)$, $B=(1,0)$ and so on. Once the final position is found, we can revert from $(\alpha,\beta)$ coordinates to the real ones.
Example. If we take the situation described in your picture we have, for instance: $$ \mathbf{u}=(100,100),\quad \mathbf{v}=(-50,50) $$ and $T-A=(20,10)=0.15\mathbf{u}-0.1\mathbf{v}$. Hence the $(\alpha,\beta)$ coordinates of the target are $(0.15,-0.1)$ and the above algorithm stops after only one step: $$ (0.15,-0.1)\to(0.15,0.1). $$ The $(\alpha,\beta)$ coordinates of the final position $F$ are then $(0.15,0.1)$ and we can translate them to real coordinates as follows: $$F=0.15\mathbf{u}+0.1\mathbf{v}+A=(10,20)+A=(30,30).$$
EDIT.
If the ball has radius $r>0$, then we can recover the above case (where we assumed $r=0$, i.e. a point-like ball) by shrinking the original rectangle $ABCD$ to another rectangle $A'B'C'D'$, lying inside $ABCD$ and having its sides at a distance $r$ from the sides of $ABCD$. It is not difficult to find the coordinates of the vertices of such rectangle: $$ A'=A+r{\mathbf{u}\over|\mathbf{u}|}+r{\mathbf{v}\over|\mathbf{v}|}, \quad B'=B-r{\mathbf{u}\over|\mathbf{u}|}+r{\mathbf{v}\over|\mathbf{v}|}, \\ C'=C-r{\mathbf{u}\over|\mathbf{u}|}-r{\mathbf{v}\over|\mathbf{v}|}, \quad D'=D+r{\mathbf{u}\over|\mathbf{u}|}-r{\mathbf{v}\over|\mathbf{v}|}, $$ where $\mathbf{u}$, $\mathbf{v}$ are defined as above and $|\mathbf{u}|$, $|\mathbf{v}|$ are the lengths of the vectors.
Once the new rectangle has been obtained one can repeat the algorithm explained before.
Let's show how that works with the same example given before (your picture) but with a ball of radius $r=10$. Using the values computed above one readily finds the new vertices: $$ A'=\left(20,10+10 \sqrt{2}\right),\quad B'=\left(120-10 \sqrt{2},110\right),\\ C'=\left(70,160-10 \sqrt{2}\right),\quad D'=\left(10 \sqrt{2}-30,60\right), $$ and the new coordinate vectors: $$ \mathbf{u}'=\left(100-10 \sqrt{2},100-10 \sqrt{2}\right), \quad \mathbf{v}'=\left(10 \sqrt{2}-50,50-10 \sqrt{2}\right). $$ We then have: $$ T-A'=\frac{3}{98} \left(10+\sqrt{2}\right)\mathbf{u}'+ \frac{1}{\sqrt{2}-5}\mathbf{v}'. $$ The first coordinate is in the range $[0,1]$, while the second one is negative. The final position is thus: $$ \frac{3}{98} \left(10+\sqrt{2}\right)\mathbf{u}'- \frac{1}{\sqrt{2}-5}\mathbf{v}'+A'= \left(40,10 \left(5+\sqrt{2}\right)\right). $$