Resize SKPhysicsBody edges and keep children inside

130 Views Asked by At

I have a large rectangular SKPhysicsBody created with SKPhysicsBody(edgeLoopFrom:...). Inside that physics body are a bunch of balls that bounce around. When a notification appears, I show a view from the bottom of the screen. I want to resize the SKPhysicsBody so the balls bounce off this notification view while it's on screen.

What I do now is

func updateWalls() {
    let wallInsets = UIEdgeInsetsMake(bottomWallInset, 0, 0, 0)
    scene.physicsBody = SKPhysicsBody(edgeLoopFrom:UIEdgeInsetsInsetRect(bounds, wallInsets))
}

In other words I just replace the old physics body with a new one with the new dimensions. This works, except that if any balls are in the region between the old dimensions and the new dimensions they get trapped and bounce off the outside of the wall and fall out of the world.

Is there an elegant way to animate the transition between the old and new wall bounds, such that any balls that lie at the bottom are "swept" up into the new dimensions, and guaranteed to remain inside the wall? You can't scale an SKPhysicsBody directly.

2

There are 2 best solutions below

0
On

In this situation, instead of resizing the physics body, it is probably easier to create a node to represent the notification. That is, make an SKNode that does not render anything, attach a physics body to it the same size as the notification view, and just move the node along with the notification view. You can create an animation for the node movement at the same speed as the view is animated. This should give the effect you need with less possibility of confusing the physics simulation.

2
On

SpriteKit have always troubles about scaling SKPhysicsBody's, because the physicsBody don't remain the same, there isn't "continuity" between physicsBodies properties. So this is not a good way.

I want to focus your attention on distinguish between nodes and physicsBodies: you could build 2 walls with it's physicsBody and when you don't need one of them you could make ishidden = true to hide the node and set isDynamic = false to disable the physics interactions. So , one time you have the interior wall with it's body, after you can hide the interior , disable the physics interactions and enable the external wall with it's physics.

About the transition between the old and new wall bounds you can study an animation between the two nodes.