For a little programming project that should animate a billiards ball (without friction and in a vacuum), I need to find out the x and y coordinates of the ball at any point in time given its start and end coordinates and its speed.
Let's say, the ball starts from coordinate x = 10.1 cm, y = 10.5 cm and will hit the hole at coordinate x = 500 cm, y = 200 cm. The speed of the ball is 7.0 cm per second.
I want to show the ball at any point in time, where time starts at 0 seconds.
So far I thought of using Pythagoras somehow:
x^2 + y^2 = z^2
where x and y are the current coordinates and z being the distance that the ball rolled from its start location up to the current point in time.
Since I know that the total x distance is 489.9 cm and the total y distance is 189.5 cm, I assume that I can write y as a factor of x, i.e. 189.5 / 489.9 which results to y_factor = 0.3868....
Now I need to figure out the x and y coordinates at some point in time, say 3.5 seconds.
According to my Pythagoras thought: x^2 + (0.3868*x)^2 = 3.5
I think that leaves me the following:
distance = speed * time_passed
current_x = squareroot(distance) / squareroot(y_factor^2 + 1)
current_y = current_x * y_factor;
To my astonishment, the animation gets slower and slower over time and I have no idea why.
Where am I wrong here?
As you can clearly see, my understanding of mathematics is more than dusty, so please try to write in laymans terms (or programming jargon)..
Here, $x$ and $y$ are the distances traveled horizontally and vertically rather than the actual coordinates. These are an offset you should apply to the initial coordinates.
You establish that $x^2+y^2=d^2$. You have $y=fx$, where $f$ is your factor between $x$ and $y$. This gives $$x^2+(fx)^2=d^2$$ $$x^2+f^2x^2=d^2$$ $$x^2(1+f^2)=d^2$$ $$x^2=\frac{d^2}{1+f^2}$$ $$x=\left(\frac{d^2}{1+f^2}\right)^{0.5}=\frac{d}{(1+f^2)^{0.5}}$$
You shouldn't be taking the square root of your distance in your second line of code.
A consideration you could make is that at any point this is essentially one expanding triangle with the same angles. So $$\frac{d_{final}}{d_{current}}=\frac{x_{final}}{x_{current}}$$ as per similar triangles, and you know $3$ of these. Replace $x$ with $y$ to get that instead. Saves you from square roots, which are more computationally expensive.