Jason Jason - 1 year ago 55
Swift Question

Why is my dynamic physics body able to push my static physics body? In Swift

I have a player and a wall, whenever the player walks into the wall it pushes it away. The wall is static and the player is dynamic anyone know how to fix this?

Player code:

player.texture = SKTexture(imageNamed: "player")
player.position = CGPoint(x: 0 + player.frame.width / 2,y: self.frame.height + player.frame.height / 2)
player.zPosition = 2
player.physicsBody?.affectedByGravity = true
player.physicsBody?.dynamic = true
player.physicsBody?.allowsRotation = false
player.physicsBody?.pinned = false
player.physicsBody?.mass = 0.18
player.physicsBody?.restitution = 0

Code to move the player:

let moveForward = SKAction.moveByX(1, y: 0, duration: 0.01)

Wall code:

backWall = SKSpriteNode(imageNamed: "up")
backWall.size = CGSize(width: 70, height: 70)
backWall.position = CGPoint(x: 0 + 35, y: ground.frame.height)
backWall.zPosition = 3
backWall.physicsBody?.categoryBitMask = PhysicsCategory.backWall
backWall.physicsBody?.contactTestBitMask = PhysicsCategory.backWall
backWall.physicsBody?.collisionBitMask = PhysicsCategory.backWall
backWall.physicsBody?.dynamic = false
backWall.physicsBody?.affectedByGravity = false
backWall.physicsBody?.allowsRotation = false
backWall.physicsBody?.friction = 1.0
backWall.physicsBody = SKPhysicsBody(rectangleOfSize: backWall.size)

Answer Source

Even though your wall is not dynamic, it is still a physics body with volume and mass. You want to use SKPhysicsBody(edgeLoopFromRect:) if you want bodies that can't move and not have to worry about advanced physics being applied to it

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download