J.Treutlein
J.Treutlein

Reputation: 983

SpriteKit: Resetting SKPhysicsBody also resets its Gravity

I have found an 'issue'/(weird interaction) with an SKSpriteNode's SKPhysicsBody. After setting the SKPhysicsBody, like this:

node.physicsBody = SKPhysicsBody(bodies: bodies)

I wanted to add more physicsBodies to the node so I added it to bodies list and called the same line again. It turns out if you want to change the node's SKPhysicsBody again after initialising it, the gravity setting of the node is also reset.

What I mean by this is if the node's affectedByGravity variable is originally false and then you reset the node's physicsBody, it becomes true.

Even weirder, if the node's affectedByGravity variable is originally true and then you reset the node's physicsBody, the gravity acceleration of the node is reset. Therefore, the node appears to be 'stuttering' down the screen unevenly and not smoothly accelerating by the gravity.

I don't want this to occur when I reset the physicsBody of the node, I just want to change its physicsBody without it affecting its gravity at all. ANY ideas would be GREATLY appreciated in Swift.

Thank you!

Upvotes: 0

Views: 163

Answers (1)

J.Treutlein
J.Treutlein

Reputation: 983

The issue is actually that when u reset an SKSpritenode's physicsBody, it automatically sets the velocity of that physicsBody to 0. From what I have tried, you can not stop this.

However, to get around this issue save the velocity of the physicsBody before resetting the physicsBody and then set the old velocity as the new one:

let vel = node.physicsBody?.velocity
node.physicsBody = SKPhysicsBody(bodies: bodies)
node.physicsBody?.velocity = vel!

Upvotes: 0

Related Questions