user3148575
user3148575

Reputation: 11

Sprite Kit setting a nodes angle (not rotate)

Okay so I have a game that the player(space ship) is trying to go around a planet, but if it gets to close the player's space ship is turned to the planet.

So I am trying to use:

float dx = [_player position].x - [planetNodes[i] position].x;
float dy = [_player position].y - [planetNodes[i] position].y;
double angleToTurn = (180.0 / M_PI)  * atan2(dy, dx);
NSLog(@"Turn to: %f", angleToTurn);
_player.zRotation = angleToTurn;

This code is put in the update method of the scene. Whenever the player gets too close to the planet it begins spinning because I am using the rotate method. Is there a method like setAngle? That way whenever the update is called it will reset the angle and actually look like the player is moving towards the planet.

Upvotes: 1

Views: 3182

Answers (2)

J. V.
J. V.

Reputation: 376

to make rotation to angle use

        [_player runAction:[SKAction rotateToAngle:atan2(dy, dx) duration:0.0 shortestUnitArc:YES]]

the sprite movement will be animated and look much nicer then just setting the zRotation.

note: the function takes radians, so no need to convert to degrees.

Upvotes: 1

WolfLink
WolfLink

Reputation: 3317

You are using the zRotation property correctly. Setting that property will have the desired effect, the problem is that you are giving _player.zRotation a value in degrees when you should be giving it radians. As the player approaches the planet, the angle changes, and the player rotates 180/pi times faster than it should because you are multiplying the angle by that.

What you want is:

angleToTurn = atan2(dy,dx);

Instead of

angleToTurn = (180.0 / M_PI) * atan2(dy,dx);

Upvotes: 2

Related Questions