1
votes

I have a 40x40 rectangle node, and I want to detect when the bottom touches a platform.

I tried this

let feet = SKPhysicsBody(rectangleOfSize: CGSize(width: hero.frame.size.width, height: 1), center: CGPoint(x: 0, y: -(hero.frame.size.height/2 - 0.5)))

then set the categoryBitMask, collisionBitMask, contactTestBitMaskand added it to the hero

hero.physicsBody = SKPhysicsBody(bodies: [feet])

But in didBeginContact the println() doesn't print.

I need a body for the bottom of the rectangle and one for the top, because if hero hits a platform from below the collision should push him down.

Update

Here is how I set the bit masks

let heroFeetCategory: UInt32 = 1 << 0
let edgeCategory: UInt32 = 1 << 1
let groundCategory: UInt32 = 1 << 2

let feet = SKPhysicsBody(rectangleOfSize: CGSize(width: hero.frame.size.width, height: 10), center: CGPoint(x: 0, y: -(hero.frame.size.height/2 - 5)))
feet.collisionBitMask = edgeCategory | groundCategory
feet.contactTestBitMask = groundCategory

feet.categoryBitMask = heroFeetCategory


hero.physicsBody = SKPhysicsBody(bodies: [feet])
hero.physicsBody?.usesPreciseCollisionDetection = true
hero.physicsBody?.velocity = CGVectorMake(0, 0)

hero.physicsBody?.restitution = 0.0
hero.physicsBody?.friction = 0.0
hero.physicsBody?.angularDamping = 0.0
hero.physicsBody?.linearDamping = 1.0

hero.physicsBody?.allowsRotation = false
hero.physicsBody?.mass = 0.0641777738928795

world.addChild(hero)

and for the ground

let ground = SKSpriteNode(color: UIColor.whiteColor(), size: CGSizeMake(38, 38))
ground.name = "groundName"
ground.position = CGPoint(x: 0, y: -(self.frame.size.height/2 - ground.frame.size.height/2))

ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size)
ground.physicsBody?.collisionBitMask = edgeCategory | heroFeetCategory
ground.physicsBody?.contactTestBitMask = heroFeetCategory
ground.physicsBody?.categoryBitMask = groundCategory
world.addChild(ground)

And how I detect if they touch

func didBeginContact(contact: SKPhysicsContact) {

    var notTheHero: SKPhysicsBody!;

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
        notTheHero = contact.bodyB;
    } else {
        notTheHero = contact.bodyA;
    }

    println(notTheHero.node) // print "heroName"
    if (notTheHero.categoryBitMask == groundCategory) {
        println("touch began?"); // is never called
    }
}
1
show us the code you used to set your bit masks for the hero and the platformMaxKargin
I set bitmasks for the feet, since i want to know only when the hero is above the platform.grape1
ok then the code for the bitmasks for the feet and platform.MaxKargin

1 Answers

-1
votes

collision is fine, but when you print the node's name through the physics body with notTheHero.node you only access the SKNode, and not the NSString property of its name. Instead, try something like println(notTheHero.node.name);