1
\$\begingroup\$

I am making a game similar to Lunar Lander, when you land with your ship on different objects, etc... The issue I am having is that I have a Player SKSpriteNode (my Ship) with a slightly complex collision system (bottom of the ship has different CategoryBitMask from other sides, etc.) and when I apply PhysicsBody(bodies: [physicsBodiesArray]) I cannot control CategoryBitMask for each element of the [physicsBodiesArray]... Because main PhysicsBody is applied to the Player it overrides all other Categories and such.

Here is my "createPlayer" function:

//Creating the player

    player = SKSpriteNode(imageNamed: "Bottle_02")
    player.size = CGSize(width: 130, height: 200)
    player.position = CGPoint(x: 0, y: 0 + player.frame.height / 2)

//Creating 2 collision objects and placing them

    var playerColTop = SKSpriteNode()
    var playerColBottom = SKSpriteNode()

    playerColTop = SKSpriteNode(imageNamed: "test_top")
    playerColBottom = SKSpriteNode(imageNamed: "test_bottom")

    playerColTop.position = CGPoint(x: 0.5, y: 0)
    playerColBottom.position = CGPoint(x: 0.5, y: 0 - playerColTop.frame.height / 2 - playerColBottom.frame.height / 2)

//Creating PhysicsBodies for the Array and assigning CategoryBitMasks

    let playerColTopPhys = SKPhysicsBody(texture: playerColTop.texture!, size: playerColTop.size)
    let playerColBottomPhys = SKPhysicsBody(texture: playerColBottom.texture!, size: playerColBottom.size)

    playerColTopPhys.categoryBitMask = PhysicsParams.Player
    playerColTopPhys.collisionBitMask = PhysicsParams.Ground | PhysicsParams.Walls
    playerColTopPhys.contactTestBitMask = PhysicsParams.Ground | PhysicsParams.Walls

    playerColBottomPhys.categoryBitMask = PhysicsParams.PlayerScore
    playerColBottomPhys.collisionBitMask = PhysicsParams.Ground | PhysicsParams.Walls
    playerColBottomPhys.contactTestBitMask = PhysicsParams.Ground | PhysicsParams.Walls

//Adding my PhysicsBodies and setting up the Player

    player.physicsBody = SKPhysicsBody.init(bodies: [playerColTopPhys, playerColBottomPhys])
    player.physicsBody?.categoryBitMask = PhysicsParams.Player
    player.physicsBody?.collisionBitMask = PhysicsParams.Ground | PhysicsParams.Walls
    player.physicsBody?.contactTestBitMask = PhysicsParams.Ground | PhysicsParams.Walls
    player.physicsBody?.restitution = 0
    player.physicsBody?.friction = 1
    player.physicsBody?.mass = 5
    player.physicsBody?.affectedByGravity = false
    player.physicsBody?.isDynamic = false

    self.addChild(player)

Thanks in advance!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.