Rotating wheel with touch (adding momentum and slowing down the initial rate it can be moved

Posted by Lewis on Game Development See other posts from Game Development or by Lewis
Published on 2012-07-01T00:54:14Z Indexed on 2012/07/01 3:24 UTC
Read the original article Hit count: 236

Filed under:
|
|

I have a wheel control in a game which is setup like so:

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    if (CGRectContainsPoint(wheel.boundingBox, location))
    {
        CGPoint firstLocation = [touch previousLocationInView:[touch view]];
        CGPoint location = [touch locationInView:[touch view]];

        CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
        CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];

        CGPoint firstVector = ccpSub(firstTouchingPoint, wheel.position);
        CGFloat firstRotateAngle = -ccpToAngle(firstVector);
        CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);

        CGPoint vector = ccpSub(touchingPoint, wheel.position);
        CGFloat rotateAngle = -ccpToAngle(vector);
        CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);

        wheelRotation += (currentTouch - previousTouch) * 0.6; //limit speed 0.6
    }
}

Now once the user lets go of the wheel I want it to rotate back to where it was before but not without taking into account the momentum of the swipe the user has done. This is the bit I really can't get my head around. So if the swipe generates a lot of momentum then the wheel will carry on moving slightly in that direction until the overall force which pulls the wheel back to the starting position kicks in.

Any ideas/code snippets?

© Game Development or respective owner

Related posts about cocos2d-iphone

Related posts about cocos2d