0

I have a player. He has to jump over some blocks.

However, it seems like the SKPhysicsBody(rectangleOfSize: ... ) isn't aligned properly. When the character hits the upper half of the block it just passes through and he can't pass below it because it collides with an invisible SKPhysicsBody.

Depending on how I put the anchor point it sometimes even happens that the physics body is to the left or right of it's SKSpriteNode.

My code for the blocks:

    self.smallBlock.anchorPoint = CGPointMake(0.5, 0.5)
    self.smallBlock.position = CGPointMake(CGRectGetMaxX(self.frame) + self.smallBlock.size.width, CGRectGetMinY(self.frame) + self.runningBar.size.height * 2)

    self.smallBlock.physicsBody = SKPhysicsBody(rectangleOfSize: self.smallBlock.size)
    self.smallBlock.physicsBody?.dynamic = false
    self.smallBlock.physicsBody?.categoryBitMask = colliderType.Block.rawValue
    self.smallBlock.physicsBody?.contactTestBitMask = colliderType.Character.rawValue
    self.smallBlock.physicsBody?.collisionBitMask = colliderType.Character.rawValue
    self.smallBlock.physicsBody?.usesPreciseCollisionDetection = true

    self.addChild(self.smallBlock)

    self.character.anchorPoint = CGPointMake(0, 0.25)
    self.character.position = CGPointMake(CGRectGetMinX(self.frame) - self.character.size.width, CGRectGetMinY(self.frame) + self.character.size.height)
    self.initPosX = CGRectGetMinX(self.frame) + (self.character.size.width * 1.25)

Code for the character

    self.character.anchorPoint = CGPointMake(0, 0.25)
    self.character.position = CGPointMake(CGRectGetMinX(self.frame) - self.character.size.width, CGRectGetMinY(self.frame) + self.character.size.height)
    self.initPosX = CGRectGetMinX(self.frame) + (self.character.size.width * 1.25)

    self.character.physicsBody = SKPhysicsBody(rectangleOfSize: self.character.size)
    self.character.physicsBody?.affectedByGravity = true
    self.character.physicsBody?.categoryBitMask = colliderType.Character.rawValue
    self.character.physicsBody?.contactTestBitMask = colliderType.Block.rawValue
    self.character.physicsBody?.collisionBitMask = colliderType.Block.rawValue
    self.character.physicsBody?.usesPreciseCollisionDetection = true
    self.character.physicsBody?.affectedByGravity = true
    self.character.physicsBody?.allowsRotation = false

    self.addChild(self.character)
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
Nick Groeneveld
  • 895
  • 6
  • 18

1 Answers1

1

Solution:

I've added skView.showsPhysics = true to my View Controller. When you run the app in the simulator it will outline every object like shown here.

It turns out setting custom anchor points messes up the rectangleOfSize-positions.

Nick Groeneveld
  • 895
  • 6
  • 18