【问题标题】:Collision Detections SceneKit Swift碰撞检测 SceneKit Swift
【发布时间】:2018-12-18 07:45:19
【问题描述】:

我使用委托 SCNPhysicsContactDelegate 构建了一个具有碰撞检测功能的应用程序。我尝试检测与 SCNPhysicsContactDelegate 委托的冲突,但它不起作用。

什么是穿的!?

这是我的代码

let CollisionCategorySpaceMan = 1
let CollisionCategoryEnemy = 2

func physicsWorld(world: SCNPhysicsWorld, didBeginContact contact: SCNPhysicsContact) {
    if contact.nodeB.physicsBody!.categoryBitMask == CollisionCategoryEnemy {
        print("Enemy HIT!--------")
    }
}




  override func viewDidLoad() {
        super.viewDidLoad()

let mainScene = createMainScene()

        mainScene.physicsWorld.contactDelegate = self

        mainScene.physicsWorld.gravity = SCNVector3Make(0, 0, 0)



        sceneView = self.view as! GameView
        sceneView.scene = mainScene
        sceneView.delegate = self

    }

这是敌人

func setupEnemy(){
        space = space - 300
        let football2 = SCNScene(named: "art.scnassets/k.scn")
        let football21 = football2!.rootNode.childNodeWithName("football2", recursively: false)
        football21!.name = "Enemy"
        football21!.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: nil)
        football21!.physicsBody!.categoryBitMask = CollisionCategoryEnemy
        football21!.physicsBody!.collisionBitMask = CollisionCategorySpaceMan
        football21!.position = SCNVector3(x: 0, y: 0, z: Float(space))
        sceneView.scene!.rootNode.addChildNode(football21!)
    }

这就是英雄

func createMainScene() -> SCNScene {
        let scene = SCNScene(named: "art.scnassets/football.scn")
        spaceManNode = scene!.rootNode.childNodeWithName("football", recursively: false)

        spaceManNode!.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: nil)
        spaceManNode!.physicsBody!.categoryBitMask = CollisionCategorySpaceMan
        spaceManNode!.physicsBody!.contactTestBitMask = CollisionCategoryEnemy
        spaceManNode.name = "SpaceMan"

        setupLighting(scene!)
        setupCameras(scene!)

        return scene!
    }

【问题讨论】:

  • 你检查过didBeginContact 是否至少被调用了?谁保证nodeB 属于CollisionCategoryEnemy,而不是nodeA
  • 我都试了都没有用:'(
  • 您的spaceManNode 上似乎没有设置collisionBitMask

标签: ios swift scenekit


【解决方案1】:

从 iOS 9 开始,您必须显式设置physicsBody 的“contactTestBitMask”才能获取联系人通知。看起来 setupEnemy() 中缺少这些。我的游戏中的碰撞在 iOS 8 中运行良好,但在 iOS 9 上停止运行,直到我发现这篇文章:Why Contact Delegate isn't called in SceneKit?

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    在这种情况下,我发现了 2 个缺失的行:

    你的敌人目标或任何应该看起来像这样的东西......

    physicsBody?.categoryBitMask = PhysicsCategory.enemy // your enemy category (lets say 1)
    physicsBody?.collisionBitMask = PhysicsCategory.player (with what can report collision lets say 2
    

    你的英雄:

    physicsBody?.categoryBitMask = PhysicsCategory.player  // the category is 2
    physicsBody?.collisionBitMask = PhysicsCategory.enemy | PhysicsCategory.ball // can report collision with 1 and 3        
    physicsBody?.contactTestBitMask = PhysicsCategory.enemy | PhysicsCategory.ball // when hitTest performed detect collision with 1 and 3
    

    希望这会有所帮助, 问候

    【讨论】:

      猜你喜欢
      • 2017-01-07
      • 2023-04-09
      • 2019-07-16
      • 2021-02-01
      • 1970-01-01
      • 2015-02-06
      • 2018-06-09
      • 2016-07-09
      • 1970-01-01
      相关资源
      最近更新 更多