Here's the situation, there is an object on screen the user should touch the object and be able to drag it around and when released it will move around the screen with a velocity based on the speed the user was dragging/swiping the object. My current implementation basically tracks the touchesMoved/touchesEnded callbacks and calculates stores the velocity based on the last/second to last touches.
Here I found it very difficult to control and sometimes would get unsatisfactorily low/high velocities, so I thought I might add in a minimum/maximum velocity to avoid these edge cases.
Which definitely helps and the velocities I get are "sane" velocities as I've arbitrarily defined desired velocities. However I still find it difficult to control where in this velocity range I am in. I'm thinking of trying to some how take an average velocity for a single movement, or maybe trying to compress a scale of human attainable velocities to my desired velocities, for example...
Reasonable velocities seem to be in the 1000-2000 pixels/s regime, where as I've been able to get velocities between 600-3000 pixels/s. So I was thinking of mapping 1000->600 and 3000->2000 with a linear scale between.
The other problem seems to be, that when trying to swipe quickly, I almost always end up curving my thumb and I don't get the angle I was intending (which could be due to the fact that I am chopping x/y components to a MAXIMUM_VELOCITY and not the magnitude, so I'll have to go back and check to see if that could fix the problem).
Before I go digging into this with trial and error I just wanted to see if anyone else has done this before and has any suggestions.
Code:
if(isTracking_) {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.superview];
CGPoint previousPoint = [touch previousLocationInView:self.superview];
NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
if(previousTouchTime_ != 0) {
CGFloat velocityX = (point.x-previousPoint.x)/(time-previousTouchTime_);
CGFloat velocityY = (point.y-previousPoint.y)/(time-previousTouchTime_);
self.velocity = CGPointMake(velocityX, velocityY);
}
previousTouchTime_ = time;
if([self.delegate ballView:self canMoveToPoint:point]) {
self.center = point;
} else {
[self endTracking];
}
}
Here I found it very difficult to control and sometimes would get unsatisfactorily low/high velocities, so I thought I might add in a minimum/maximum velocity to avoid these edge cases.
Code:
CGFloat speedX = fabs(velocityX);
CGFloat speedY = fabs(velocityY);
if(speedX < MINIMUM_VELOCITY)
if(velocityX < 0)
velocityX = -MINIMUM_VELOCITY;
else
velocityX = MINIMUM_VELOCITY;
if(speedX > MAXIMUM_VELOCITY)
if(velocityX < 0)
velocityX = -MAXIMUM_VELOCITY;
else
velocityX = MAXIMUM_VELOCITY;
if(speedY < MINIMUM_VELOCITY)
if(velocityY < 0)
velocityY = -MINIMUM_VELOCITY;
else
velocityY = MINIMUM_VELOCITY;
if(speedY > MAXIMUM_VELOCITY)
if(velocityY < 0)
velocityY = -MAXIMUM_VELOCITY;
else
velocityY = MAXIMUM_VELOCITY;
Which definitely helps and the velocities I get are "sane" velocities as I've arbitrarily defined desired velocities. However I still find it difficult to control where in this velocity range I am in. I'm thinking of trying to some how take an average velocity for a single movement, or maybe trying to compress a scale of human attainable velocities to my desired velocities, for example...
Reasonable velocities seem to be in the 1000-2000 pixels/s regime, where as I've been able to get velocities between 600-3000 pixels/s. So I was thinking of mapping 1000->600 and 3000->2000 with a linear scale between.
The other problem seems to be, that when trying to swipe quickly, I almost always end up curving my thumb and I don't get the angle I was intending (which could be due to the fact that I am chopping x/y components to a MAXIMUM_VELOCITY and not the magnitude, so I'll have to go back and check to see if that could fix the problem).
Before I go digging into this with trial and error I just wanted to see if anyone else has done this before and has any suggestions.