Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a game which relies on "flinging" an object. That is, click and hold on the object, and then drag and release it, and it continues on the path you were dragging it. Of course, the most well-known example of flinging is with iPhone and Android devices, where you can quickly scroll down a list by quickly swiping your finger upward, giving the illusion of "flinging" the list.

I'm tracking mouse positions (x,y) and timestamps. But I'm drawing a blank as to how I can take a list of positions and times and get out of it a velocity or curve that an object should follow.

What are my options? Right now I am looking only for a straight-line fling action, but if it's easy to implement some sort of curve that better fits the fling, that would be good information that I might be able to integrate into the design of the game.

share|cite|improve this question
I would recommend , another stack exchange site, if you haven't looked there already One could say that the site was designed exactly for questions like this. – Justin L. Aug 22 '10 at 7:22
up vote 6 down vote accepted

In the physical world, once an object is released from all external forces, it will travel in a straight line. UI design strongly suggests that interfaces work better when the user is using previous knowledge of motor control, eye movement, physics, etc. Unless you have a legitimate reason to curve after the object is released, I would personally go with linear only.

In that case, take the last few coordinates and timestamps, and average the velocity components together. Then run a quadratic or exponential function of time on each component until the change of position is negligibly small. The latter functions relates to throwing an object upward and watching it being slowed by gravity, and the former relates to shoving an object on the ground and seeing it come to a stop by friction. Since the exponential function seems like the best for the job, here's an example.

Find the last few velocities for the $x$ and $y$ components, and average them:

$v = \frac{\Delta x}{\Delta t}; y = \cdots$

$x = \frac{\sum v_x}{n}; y = \cdots$

Set time from release for $t$, $\beta$ as some friction constant, $v_x$ as the average velocity for each component, and $x_0$ as the initial release point.

$x = v_x (1 - e^{-\beta t})$

The purpose of the 1 is to align the function to start at zero when time is zero.

So I hope this helps. I'm interested in what you're doing, so you'll have to show me when you're done. ;)

share|cite|improve this answer
Wow, all I really needed was the first part; somehow I didn't make the connection that with position and time, I can find velocity! Despite several years of physics and math now. :-\ I am making the game for Ludum Dare, a 48-hour game programming competition, and I am updating this page almost hourly: (note that the current, as of right now, game does have flinging but it's not a very good/consistent/accurate algorithm; I'll come back and comment again when it's "fixed") - it's a Java applet so you can play it in the browser (in most cases). – Ricket Aug 22 '10 at 2:34
I see you've implemented gravity in your applet. Could you further describe the behavior you're looking for? – Vortico Aug 22 '10 at 3:51
I was looking for the equation of a fling. Sorry I forgot to come back here last night, but the game is using the first part of your answer. I don't need the x=vx... equation you posed, I just needed that connection of turning the points into velocities, and I'm pleased with how it turned out! – Ricket Aug 22 '10 at 21:42
Great! For added effect, you could rotate the held bird depending on the mouse's velocity. See Pendulums on Wolfram's Science World. Maybe a little difficult to implement, but the game is begging for it. – Vortico Aug 24 '10 at 2:35

Why not compute a cubic spline from the 5 most recent samples equidistant in time? This will give you a smooth curve and you can use the parametric derivative of the spline as a velocity.

share|cite|improve this answer
Because in game programming, you can't update at equidistant times. With the numerous other things running "simultaneously" in the computer, there's no way to enforce a truly consistent loop. Everything has to be based on time. – Ricket Aug 22 '10 at 2:31
Then I think you can still use the spline, but scale the parametric derivative between consecutive points by the time difference. – Dan Brumleve Aug 22 '10 at 5:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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