So I have created a player who moves tile based and a wall surrounding the map to keep the player on the playground. Both have a physicsBody in them. My guess is, that my movement of the player is not properly and so the player bugs into the walls. Let me just show you my code:
So this is the physicsBody of the Player:
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.categoryBitMask = PhysicsCategory.Player
self.physicsBody!.contactTestBitMask = PhysicsCategory.House
self.physicsBody!.collisionBitMask = PhysicsCategory.Wall
And this is the physicsBody of the Wall:
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.categoryBitMask = PhysicsCategory.Wall
self.physicsBody!.contactTestBitMask = 0
self.physicsBody!.collisionBitMask = PhysicsCategory.Player
self.physicsBody!.isDynamic = false
They both inherit from the class Objects:
self.physicsBody = SKPhysicsBody(rectangleOf: size)
self.physicsBody!.affectedByGravity = false
self.name = name
So and just let me give you my code for the movement of the player if thats the cause:
func move(direction: String, width: CGFloat){
//Choosing the direction based of the users touch
switch direction{
case "South":
//Decreasing the position of the player by 1 Tile
pos.y -= width
//Flipping the picture the right way to make the player look in the right direction
yScale = 1.0
//Rotates the picture
let rotateAction = SKAction.rotate(toAngle: -1.57, duration: 0.0)
run(rotateAction)
//All the other directions just change the x and y value of pos corresponding to the direction
}
//Moves the player to the calculated position
let moveAction = SKAction.move(to: pos, duration: 0.0)
run(moveAction){
//Sound
}
Edit:
The values of the PhysicsCategory.Player
and PhysicsCategory.Wall
struct PhysicsCategory{
static let Wall:UInt32 = 0x1 << 0
static let Player:UInt32 = 0x1 << 1
static let House:UInt32 = 0x1 << 2
}
Edit 2:
The main problem is to get collision with SKAction properly working
You haven't actually described your problem other than "Sprites are moving through each other..." and "so the player bugs into the walls"
To validate all of your contact and collsions, have a look at my answer here : iOS SpriteKit - collisions and contacts not working as expected and try implementing the checkPhysics() function and calling it once you think you have set up all of your physics bodies and their interactions.
Are you certain that:
SKPhysicsContactDelegate
physicsWorld.contactDelegate = self
SKPhysicsContactDelegate
:Is this correct:
It is not listed in the question here, but the reason why collision contact is not happening is because the method
didBeginContact
got changed todidBegin