简体   繁体   English

Swift SpriteKit物理碰撞问题

[英]Swift SpriteKit Physics Collision Issue

I am making a simple physics based game. 我正在做一个简单的基于物理的游戏。 Everything is working normally with exception to collision detection. 除冲突检测外,一切正常。 It feels like the didBeginContact method is being ignored. 感觉didBeginContact方法被忽略了。

I have tried several ways of configuring the "PhysicsCategory" struct (even using enum) and several formations of the bodyA/bodyB contact statements. 我尝试了几种配置“ PhysicsCategory”结构的方法(甚至使用枚举)以及bodyA / bodyB联系语句的几种形式。

I am all out of ideas. 我全都没主意了。 I can get the 2 objects to collide but they just bounce off each other. 我可以使2个对象发生碰撞,但它们只是相互反弹。 There are no errors and nothing logged to the console. 没有错误,也没有任何记录到控制台。 I hope that I have made a trivial mistake that I am overlooking. 我希望我犯了一个我忽略的小错误。

Below is all the pertinent code. 以下是所有相关代码。 In case it matters... setupPhysics() is being called in didMoveToView 如果很重要...则在didMoveToView中调用setupPhysics()

PhysicsCategory Struct 物理类别结构

struct PhysicsCategory {
static let None: UInt32 = 0
static let Fish: UInt32 = 0b1
static let Bird: UInt32 = 0b10
static let BottomEdge: UInt32 = 0b100}    

Physics Setup Method 物理设置方法

//MARK: - Physics Methods
func setupPhysics() {
    /* Physics World */
    physicsWorld.gravity = CGVectorMake(0, -9.8)
    physicsWorld.contactDelegate = self
    physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)

/* Bottom Collision Rect */
    let bEdge = CGRect(x: CGPointZero.x, y: CGPointZero.y, width: size.width, height: size.height * 0.005)
    let bottomEdge = SKShapeNode(rect: bEdge)
    bottomEdge.physicsBody = SKPhysicsBody(edgeLoopFromRect: bEdge)
    bottomEdge.physicsBody!.categoryBitMask = PhysicsCategory.BottomEdge
    bottomEdge.physicsBody!.collisionBitMask  = PhysicsCategory.Fish
    bottomEdge.physicsBody!.dynamic = false
    gameLayer.addChild(bottomEdge)

    /* Fish */
    fish.physicsBody = SKPhysicsBody(circleOfRadius: blowfish.size.height / 2.1)
    fish.physicsBody!.allowsRotation = false
    fish.physicsBody!.categoryBitMask = PhysicsCategory.Fish
    fish.physicsBody!.collisionBitMask = PhysicsCategory.Bird | PhysicsCategory.BottomEdge

    /* Left Random Bird */
    randomLeftBird.physicsBody = SKPhysicsBody(rectangleOfSize: randomLeftBird.size)
    randomLeftBird.physicsBody!.affectedByGravity = false
    randomLeftBird.physicsBody!.allowsRotation = false
    randomLeftBird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
    randomLeftBird.physicsBody!.collisionBitMask = PhysicsCategory.Fish

    /* Random Right Bird */
    randomRightBird.physicsBody = SKPhysicsBody(rectangleOfSize: randomRightBird.size)
    randomRightBird.physicsBody!.affectedByGravity = false
    randomRightBird.physicsBody!.allowsRotation = false
    randomRightBird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
    randomRightBird.physicsBody!.collisionBitMask = PhysicsCategory.Fish
}

didBeginContact Setup didBeginContact设置

func didBeginContact(contact: SKPhysicsContact!) {
    let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    if collision == PhysicsCategory.Fish | PhysicsCategory.Bird {
        println("COLLISON WITH BIRD!")
        updateLives()
    } else if collision == PhysicsCategory.Fish | PhysicsCategory.BottomEdge {
        println("EPIC FAIL!")
    }
}

I don't see any use of contactTestBitMask in your code. 我在您的代码contactTestBitMask不到contactTestBitMask任何使用。 That one controls whether you get contact delegate messages — collisionBitMask just controls whether they collide (bounce off). 该控件控制您是否获取联系人委托消息— collisionBitMask仅控制它们是否发生碰撞(反弹)。

These are separate so that you can get contact delegate messages even for categories that don't bounce off each other, but it means you also can have collisions that don't send messages. 这些是分开的,因此即使对于彼此不会反弹的类别,您也可以获得联系人委托消息,但这意味着您也可能遇到不发送消息的冲突。 (That can be a good thing if you don't want game logic for every kind of collision.) Any that you do want contact messages for you need to explicitly request. (如果您不希望每种碰撞都有游戏逻辑,那将是一件好事。)任何您想要的联系信息都需要明确请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM