Jonathan
Jonathan

Reputation: 2383

Limit Sprite Rotation - Cocos2d

I have two hands on either side of the screen that do not move at all and two thumbs that rotate 360 degrees. I want to limit the thumbs rotation, meaning I only need them to be able to rotate like normal thumbs. I am new to cocos2d so any help will be greatly appreciated. Here is what I have so far

    #import "cocos2d.h"


    @interface GameScene : CCLayer {

        CGFloat lthumbRotation, rthumbRotation;
        CCSprite *lthumb, *rthumb;
    }

    +(CCScene *) scene;


    @end
------------------------------------
#import "GameScene.h"

@implementation GameScene

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    GameScene *layer = [GameScene node];

    [scene addChild: layer];
    return scene;
}

-(id) init
{
    if ((self = [super init]))
    {
        lthumb = [CCSprite spriteWithFile:@"lthumb.png" rect:CGRectMake(0,0, 145, 59)];
        lthumb.position = ccp(100, 140);
        lthumb.anchorPoint = ccp(0.3, 0.8);

        [self addChild:lthumb z:0];


        rthumb = [CCSprite spriteWithFile:@"rthumb.png" rect:CGRectMake(0,0, 145, 59)];
        rthumb.position = ccp(380, 140);
        rthumb.anchorPoint = ccp(0.7, 0.8);

        [self addChild:rthumb z:0];

        [self scheduleUpdate];

    }
    return self;
}
-(void)update:(ccTime)delta
{
    lthumb.rotation = lthumbRotation;
    rthumb.rotation = rthumbRotation;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    //acquire the previous touch location
    CGPoint firstLocation = [touch previousLocationInView:[touch view]];
    CGPoint location = [touch locationInView:[touch view]];

    //preform all the same basic rig on both the current touch and previous touch
    CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
    CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];

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

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

    //keep adding the difference of the two angles to the dial rotation
    lthumbRotation += currentTouch - previousTouch;
    rthumbRotation -= currentTouch - previousTouch;
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void) dealloc
{
    CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
    [super dealloc];
}

@end

This lets both of the thumbs move at the same time in upward/downward angles with the anchor point at the bottom of the thumbs(acting like a joint).

I also need the thumbs rotation to reset back to 0 once the touches have ended.

Thanks in advance

Upvotes: 1

Views: 877

Answers (1)

Reed Olsen
Reed Olsen

Reputation: 9169

In your ccTouchesMoved:withEvent: method, simply check to see if the new rotation is within a given threshold before applying it to your sprites.

Finally, in ccTouchesEnded:withEvent:, you can simply set the rotation values back to 0:

lthumbRotation = 0;
rthumbRotation = 0;

Upvotes: 1

Related Questions