user2763173
user2763173

Reputation:

In the SpriteKit framework, how can I make a sprite point in the direction a sprites PhysicsBody is going?

I have a sprite moving around the scene, that has physics. I want to create a motion blur effect, but I am stuck on getting it so that the SKSPriteNode with the blurred image is pointing in the right way.

Upvotes: 0

Views: 327

Answers (3)

fawsha1
fawsha1

Reputation: 790

If you use an SKAction there is a parameter called OrientToPath. This will align your object with the path it is on. All you have to do initially is ensure the orientation starts in the right position.

SKAction.followPath(cgpath, asOffset: false, orientToPath: true, duration: 5)

Upvotes: 0

Epic Byte
Epic Byte

Reputation: 33968

You could do something like this.

-(void)didSimulatePhysics {
    if (node.physicsBody.velocity.dx!=0 || node.physicsBody.velocity.dy!=0)
        node.zRotation = atan2(node.physicsBody.velocity.dy, node.physicsBody.velocity.dx);
}

Upvotes: 0

prototypical
prototypical

Reputation: 6751

The SKPhysicsBody has a velocity and angularVelocity property. Those properties can be used to determine your direction for this purpose.

You can use the zRotation property of any SKNode to adjust it's rotation to get the proper facing.

Upvotes: 0

Related Questions