title: Rotating sprite with finger in Cocos2d
body: <p>I need help figuring out the rotation of a sprite with my finger. The sprite is rotating fine but on the initial touch of my finger it somehow rotates by a few degrees by itself. </p>
  
  <p>Also the rotations only work when the finger is rotated around the center of the sprite.</p>
  
  <p>I am trying to simulate the bicycle wheel and have a gear sprite and a pedal sprite as a child of the gear sprite. I want the bicycle wheel to rotate as I touch the pedal and rotate it. I haven't got that far yet. Right now I am trying to figure out how to get rid of the initial shift the gear makes.</p>
  
  <p>Here is the complete code</p>
  
  <pre><code>#import "BicycleScene.h"
  
  @implementation BicycleScene
  
  +(id) scene
  {
      // 'scene' is an autorelease object.
      CCScene *scene = [CCScene node];
  
      // 'layer' is an autorelease object.
      BicycleScene *layer = [BicycleScene node];
  
      // add layer as a child to scene
      [scene addChild: layer];
  
      // return the scene
      return scene;
  }
  
  -(id) init
  {
      if ((self=[super init])) {
  
          CGSize winSize = [[CCDirector sharedDirector] winSize];
  
          //place the bicycle sprite
          bicycleSprite = [CCSprite spriteWithFile:@"bike_gear.png"];
          bicycleSprite.position = ccp(bicycleSprite.contentSize.width/2 + 100, winSize.height/2);
          [self addChild:bicycleSprite z:1];
  
          //place the pedal sprite
          pedalSprite = [CCSprite spriteWithFile:@"bike_pedal.png"];
          [bicycleSprite addChild:pedalSprite z:1];
  
          pedalSprite.position = ccp(150, 15);
  
          //enable touch
          self.isTouchEnabled = YES;
  
          [self schedule:@selector(gameLoop:) interval:.1/100];
  
      }
  
      return self;
  }
  
  -(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  
      NSLog(@"Touch begun");
  
  }
  
  -(void)gameLoop:(ccTime) dt{
  
      bicycleSprite.rotation = cocosAngle;
  }
  
  -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
  
      NSLog(@"Touch moved");
  
      UITouch *touch = [touches anyObject];
      CGPoint location = [touch locationInView:[touch view]];
      CGPoint previousLocation = [touch previousLocationInView:[touch view]];
  
      CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
      CGPoint previousTouchingPoint = [[CCDirector sharedDirector] convertToGL:previousLocation];
  
      CGPoint vector = ccpSub(touchingPoint, bicycleSprite.position);
      CGFloat rotateAngle = -ccpToAngle(vector);
  
      previousAngle = cocosAngle;
      cocosAngle = CC_RADIANS_TO_DEGREES( rotateAngle);
  
      //bicycleSprite.rotation = cocosAngle;
  }
  
  @end
  </code></pre>
  
  <p>I am slightly confused if the line:</p>
  
  <pre><code>CGPoint vector = ccpSub(touchingPoint, bicycleSprite.position);
  </code></pre>
  
  <p>should actually be:</p>
  
  <pre><code>CGPoint vector = ccpSub(touchingPoint, previousTouchingPoint );
  </code></pre>
  
  <p>I tried that as well but it didn't work.</p>
  
  <p>I have also uploaded my complete xcodeproj to 4shared for anyone who wants to take a look here: <a href="http://www.4shared.com/file/5BaeW4oe/Healthy.html" rel="nofollow">http://www.4shared.com/file/5BaeW4oe/Healthy.html</a></p>