【问题标题】:SceneKit collision detection is not being detected未检测到 SceneKit 碰撞检测
【发布时间】:2017-01-07 11:18:23
【问题描述】:

我无法弄清楚为什么我的碰撞没有被检测到。我希望你能找出原因,因为它给我带来了很多问题,我需要学习如何正确地做到这一点。斯威夫特,SceneKit。

好吧,我一如既往地从这个声明开始:

    override func viewDidLoad() {
    super.viewDidLoad()


    createScene()



   scene.physicsWorld.contactDelegate = self

   // This Statement.^

    motionManager = CMMotionManager()
    motionManager.startAccelerometerUpdates()


}

我希望球和盒子节点之间发生碰撞。我的盒子节点是mainBox,我的球是……球。

 ball.position = SCNVector3Make(0, 1.75, 3)
    ball.geometry = ballGeometry
    ballMaterial.diffuse.contents = UIColor.greenColor()
    ballGeometry.materials = [ballMaterial]
    scene.rootNode.addChildNode(ball)
    ball.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: SCNPhysicsShape(geometry: ballGeometry, options: nil))
    ball.physicsBody?.angularVelocityFactor = SCNVector3Make(0, 0, 0)
    ball.physicsBody?.angularVelocity = SCNVector4Make(0, 0, 0, 0)
    ball.name = "sphere"
    ball.physicsBody?.categoryBitMask = bodyNames.Person
    ball.physicsBody?.contactTestBitMask = bodyNames.Floor
    ball.physicsBody?.collisionBitMask = bodyNames.Floor
    ball.physicsBody?.affectedByGravity = true
    ball.addChildNode(cameraNode)


    mainBox.position = SCNVector3Make(0, -0.75, 2)
    mainBox.geometry = mainBoxGeometry
    mainBoxMaterial.diffuse.contents = UIColor.whiteColor()
    mainBox.physicsBody?.categoryBitMask = bodyNames.Floor
    mainBox.physicsBody?.contactTestBitMask = bodyNames.Person
    mainBox.physicsBody?.collisionBitMask = bodyNames.Person
    mainBox.physicsBody = SCNPhysicsBody.staticBody()
    mainBoxGeometry.materials = [mainBoxMaterial]
    mainBox.name = "floor"
    scene.rootNode.addChildNode(mainBox)

现在魔法发生的地方,还有魔法没有发生的地方......

 func physicsWorld(world: SCNPhysicsWorld, didBeginContact contact: SCNPhysicsContact) {

    let nodeA = contact.nodeA
    let nodeB = contact.nodeB

    if nodeA.physicsBody?.categoryBitMask == bodyNames.Person && nodeB.physicsBody?.categoryBitMask == bodyNames.Floor || nodeA.physicsBody?.categoryBitMask == bodyNames.Floor && nodeB.physicsBody?.categoryBitMask == bodyNames.Person {


        print("I collided with the damn box.")

    }

}

哦,这就是 bodyNames 的来源。

struct bodyNames {

static let Person = 0x1 << 1
static let Floor = 0x1 << 2

}

如果我需要添加更多代码来回答,请告诉我,我很乐意这样做。

【问题讨论】:

  • 在设置physicsBody 本身之前,您要在mainBoxphysicsBody 上设置所有位掩码。
  • 非常简单的修复,但现在可以正常工作。谢谢。 @JamesP

标签: ios swift collision-detection game-physics scenekit


【解决方案1】:

你检查过didBeginContact 是否访问过吗?

如果访问过,检查if条件

nodeA.physicsBody?.categoryBitMask == bodyNames.Person && nodeB.physicsBody?.categoryBitMask == bodyNames.Floor || nodeA.physicsBody?.categoryBitMask == bodyNames.Floor && nodeB.physicsBody?.categoryBitMask == bodyNames.Person

如果根本没有访问过,你必须检查physicsBody的世界、盒子和球的配置是否正确

【讨论】:

  • didBeginContact 由于某种原因没有被调用。
猜你喜欢
  • 2019-07-16
  • 1970-01-01
  • 2018-02-15
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 2021-02-01
  • 2015-02-06
相关资源
最近更新 更多