0

Im trying to get my player sprite to rotate towards my mouse clicks. My player barely even moves and it prints out numbers in the decimals, such as "0.000991", What am i doing wrong?

    CGPoint endPoint = touchLocation;
    CGPoint startPoint = _plane.position;
    CGFloat angleVal =  atan2(endPoint.x -startPoint.x ,  endPoint.y- startPoint.y );
    CGFloat angle = angleVal* M_PI / 180;
    SKAction *planeAction = [SKAction rotateToAngle: angle duration:1];
    [_plane runAction:[SKAction sequence:@[planeAction]]];
    NSLog(@"%f",angle);
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281

1 Answers1

0

atan2 returns an angle in radians. You then try to convert it to radians, which ruins the value. rotateToAngle:duration: expects radians.

So, remove the * M_PI / 180 calculation and use angleVal directly.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Yeah. When you see small values like that you are usually looking at radians. – 0xFADE Apr 03 '14 at 18:22
  • okay sweet, i got rid of that and I'm using angleVal directly, i also needed to multiply angleVal by -1 because it was going to the opposite side that i clicked. it is still extremely inaccurate and only works within the top 180 degrees. – user3483259 Apr 03 '14 at 18:24
  • @user3483259: Note that `atan2(y, x)` takes the y-component as the first argument. – Martin R Apr 03 '14 at 18:26
  • You might want to take a look over some other answers for that, like http://stackoverflow.com/questions/6064630/get-angle-from-2-positions – Wain Apr 03 '14 at 18:29
  • okay i switched around the X and Y for my atan2 argument and now it only works on the right side of my object and is still extremely inaccurate. – user3483259 Apr 03 '14 at 18:32
  • @wain i have looked at that post before, it uses methods from Cocos2D where i am using sprite kit here. – user3483259 Apr 03 '14 at 18:33
  • I didn't see any Cocos2d specific methods / functions when I scanned through it. There are other questions on the same topic too. – Wain Apr 03 '14 at 18:36