Calculate position of bouncing ball based on arbitrary time value

124 Views Asked by At

Usually, when you make a bouncing ball in any programming language, you have an X and Y value that updates over time, as well as an x velocity and y velocity, whose signs flip when the ball hits the left/right borders and top/bottom borders, respectively.

But, I was wondering. Given this same scenario of a bouncing ball, with a fixed speed, fixed starting position, and 4 equal length walls that it can bounce against, would it be possible to determine the position of the ball given an arbitrary time value, t?

image diagram

Take this example. Each edge of the screen is 128in long. The ball travels at 3 inches per second along the x axis, and 2 in per second along the y axis. The same rules for bouncing apply here as when first described above. We can also assume the ball is just a point with no width. What formula would calculate the ball's position after, say 6.5 seconds of the program running?

1

There are 1 best solutions below

0
On BEST ANSWER

Assuming no external force, we can decouple the $x$ and $y$ direction. I'll just discuss the $x$ direction.

The only force applied to the ball/particle would be at the boundary, and would always flip the direction of the traveling particle. Therefore, the particle would always travel at constant speed, then flip its direction at the boundary and continue to travel at constant speed (but at an opposite direction. The $x$ coordinate would just be a triangular function with periodicity of $2L/v$, where $L$ is the length from one side to the other side, $v$ is the initial velocity in the $x$ direction. I assume left side of the box is at the coordinate $x=-L/2$, and right side is at $x = L/2$. Not very elegant...but here you go...

\begin{aligned} x &= \begin{cases} &x_0 + vt, \quad 0\le t < t_f,\\ &\text{sign}(v)L/2 - v(t - t_f), \quad t_f \le t < t_f + |L/v|,\\ &-\text{sign}(v)L/2 + v(t - t_f - |L/v|), \quad t_f + |L/v| \le t <2L/v \end{cases}\\ t_f &= |\frac{\text{sign}(v)L/2 - x_0}{v}| \end{aligned}

The plot of the coordinate would look something like this, assuming the particle starts to the right of the box, and traveling to the right direction: enter image description here

The boundary is at $x=\pm 5$, as you can see from the plot, the particle changes direction when it hits the boundary.