Game Development Stack Exchange is a question and answer site for professional and independent game developers. 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

So I am trying to port over a simple game that I originally wrote to iphone into cocos2d-x. I've hit a minor bump however in implementing simple 'fling' logic I had in the iphone version that is difficult to port over to the c++. In iOS I could get the velocity of a pan gesture very easily:

CGPoint velocity = [recognizer velocityInView:recognizer.view]; 

However now I basically only know where the touch began, where the touch ended, and all the touches that are logged in between. For now I logged all the pts onto a stack then pulled the last point and the 6th to last point (seemed to work the best), find the difference between those pts multiply by a constant and use that as the velocity. It works relatively well, but I'm wondering if anyone else has any better algorithms, when given a bunch of touch pts, to figure out a new speed upon releasing an object that feels natural (Note speed in my game is just a constant x and y, there's no drag or spin or anything tricky like that).

Bonus points if anyone has figured out how to get pan gestures into the newest version (3.0 alpha) of cocos2d-x without losing ability to build cross platform.

share|improve this question

I think using just the last points is a little risky. For example, if the user do a lot of harsh movements but the last 6 points are generated from a very calm movement your system will behave wrongly by discarding all the other points.

If you have performance limitations you could indeed fix the number of points to be seen but use a list instead of a stack so you can interpolate the indices of the points to be checked based on the size of the list and the number of values you want to consider. The main idea is:

const int MAX_POINTS_TO_CONSIDER = /*your constant value here*/;
const int finalPointsToConsider = min(pointVector.size(), MAX_POINTS_TO_CONSIDER);
const int step = pointVector.size(); / finalPointsToConsider;

for(int i = 0, i < finalPointsToConsider; ++i) {
    Point p = pointVector[i*step];

    // Do your computation here


If you don't have performance problems, just use all points to be accurate.

share|improve this answer
I use the last points not so much for performance, but because if the user is holding something for a long time, those early points aren't really important to the 'fling'. Like if you hold something steady for a very long time, but then slide your finger up and release, that shouldn't be any slower than grabbing and flinging immediately, it's usually the points very close to the release of the finger that are important, also the Do your computation here is also part of the question. – Kevin DiTraglia Nov 8 '13 at 19:50

the best is to look at the implementation of the recognizer. You can have a look at the open source project Chamelon that ports uikit to mac os.

in your case your can find the implementation of your recognizer here :

it seems that it just takes the last two points.

share|improve this answer

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.