Confused
Confused

Reputation: 6278

Scale a SKPhysicsBody, how?

I'm reasonably sure I've previously seen code somewhere that scales a SKPhysicsBody, but google and the docs aren't helping, making me more confused than normal.

Is there a way to scale a physic's body in SpriteKit?

The docs, here, don't mention anything about it: https://developer.apple.com/reference/spritekit/skphysicsbody

Upvotes: 3

Views: 653

Answers (3)

revati.at
revati.at

Reputation: 55

just updating this in 2023: if you create you physics body from the sprites texture, you can use the SKPhysicsBody initialiser with texture and size property like this:

let scale: CGFloat = 0.9
self.physicsBody = SKPhysicsBody(
     texture: self.texture!,
     size: CGSize(
        width: self.size.width * scale, 
        height:self.size.height * scale
    )
)

Upvotes: 1

claassenApps
claassenApps

Reputation: 1147

If you assign a physics body to a sprite and scale that sprite, the physics body will scale with it.

Upvotes: 3

Fluidity
Fluidity

Reputation: 3995

So the answer I think to your question is a solid "no"; at least not in anything I've seen before.

If you care to dabble in a workaround:

  1. Create a new physics body that is larger than what you currently have. Make a method or two to accomplish this.

node.physicsBody = scaledPhysicsBody(bodyToScale: xxxx)

2.

I would just make a blank SKNode and child it to whatever it is that you want to be hit-detectable. Then get rid of the parent's physics body and just use the child's. Now, you can scale the child node (and thus the PB) without affecting the graphics of the main sprite.

Upvotes: 1

Related Questions