For a little programming project that should animate a billiards ball (without fraction 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)..

  • $\begingroup$ Are you assuming there's no walls to bounce off of? $\endgroup$ – Silynn May 23 '14 at 20:44
  • $\begingroup$ @Silynn, correct. No walls. Just a ball that rolls to its destination (a pocket on a pool billiard table) $\endgroup$ – mawimawi May 23 '14 at 20:45

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.

  • $\begingroup$ Thank you for thinking of "similar triangles". Now I only have one square root function at the beginning of the whole animation to find out the d_final. The rest ist just simple division and multiplication derived from your last equation. $\endgroup$ – mawimawi May 23 '14 at 22:27

The simplest way I see is to parameterize x and y in terms of t. Which you can do using the following.


Where $x_{0}$ and $y_{0}$ are the starting positions. So now we just need to solve for $v_{x}$ and $v_{y}$.

$$v_{x}=v\cos{\theta}=v\cos{(\tan^{-1}\frac{\Delta y}{\Delta x})}=v\frac{\Delta x}{\sqrt{(\Delta x)^{2}+(\Delta y)^{2}}}\\v_{x}=v\sin{\theta}=v\sin{(\tan^{-1}\frac{\Delta y}{\Delta x})}=v\frac{\Delta y}{\sqrt{(\Delta x)^{2}+(\Delta y)^{2}}}$$

Where $v$ is the speed, $\Delta x$ is $x_{final}-x_{initial}$, and $\Delta y$ is $y_{final}-y_{initial}$.

I hope this is simple enough. I can try to write some pseudocode if you're having trouble understanding.

  • $\begingroup$ I like your solution a lot, since I do not need to use square roots all the time, but only multiplication, after initially finding out the deltas. I think I'm able to write the code myself. (btw. it's just an exercise in JavaScript and SVG for learning how they can work together in detail) $\endgroup$ – mawimawi May 23 '14 at 21:17
  • $\begingroup$ This is indeed how one should probably code all those questions where speed is a constant and a point should be placed at specific times : find the x and y components of the speed. $\endgroup$ – Jedai May 23 '14 at 21:34

$x^2 + (0.3868 \times x)^2 = 3.5$ is an equality between a distance (squared) and a time... It doesn't work, at a very fundamental dimensional level.

Using units (or at least dimension) is a must to reason properly about a physical question. Here you have 3.5s, so you want to convert this into a distance. Since you have a speed, it behoove you to use $v = \frac{d}{t}$ or more exactly here $d = v \cdot t$ so in 3.5s you'll go $7 \times 3.5$ cm. That's only a distance (the hypotenuse) so you should square it in your precedent equation : $$x^2 + (0.3868 \times x)^2 = (7 \times 3.5)^2$$

Let's reformulate with letters (names are overly long and obscure the computation, we're not programming yet) : $$x + (f \cdot x)^2 = (v \cdot t)^2$$ $$(1 + f^2)x^2 = (v \cdot t)^2$$ $$x^2 = \frac{(v \cdot t)^2}{1 + f^2} $$ $$x = \sqrt{ \frac{(v \cdot t)^2}{1 + f^2} } $$ $$x = \frac{v \cdot t}{\sqrt{1 + f^2}}$$

So the problem was that you took the square root of $v \cdot t$.


The point is that the formula for current x is given by the distance (not the square root of the distance) divided by the quantity (y_factor^2+1).

In fact, starting from the Pythagorean equation x^2 + (0.3868*x)^2 = distance^2, which expresses the relation between the distance that the ball rolls and the change in the x-coordinate, we get:

x^2 *(0.3868^2 + 1) = distance^2

x^2 = distance^2/(0.3868^2 + 1)

x = distance/sqr(0.3868^2 + 1)

The animation based on this formula would probaby be realistic and no longer slow.


$\left({ {10.1+7t\cos\theta} \mid {10.5+7t\sin\theta} }\right)$,

where $t=$ time in seconds and


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.