未检测到SceneKit碰撞检测

时间:2016-08-31 02:36:43

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

我无法弄清楚为什么我的碰撞没有被发现。我希望你能弄明白为什么,因为它给我带来了很多问题,我需要学习如何正确地做到这一点。 Swift,SceneKit。

好吧,我一如既往地从这句话开始:

E/Value inserting ==: second
E/SQLiteLog: (1) near "null": syntax error
E/SQLiteDatabase: Error inserting 
              android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO Mylist(null) VALUES (NULL)
                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                  at todo.do2.com.todo.dbHandler.addListItem(dbHandler.java:56)
                  at todo.do2.com.todo.MainActivity.onCreate(MainActivity.java:70)
                  at android.app.Activity.performCreate(Activity.java:6251)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5461)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                  at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
E/count:  0

我想在球和盒子节点之间发生碰撞。我的盒子节点是mainBox,我的球是......球。

    override func viewDidLoad() {
    super.viewDidLoad()


    createScene()



   scene.physicsWorld.contactDelegate = self

   // This Statement.^

    motionManager = CMMotionManager()
    motionManager.startAccelerometerUpdates()


}

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

 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)

哦,这就是bodyNames的来源。

 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.")

    }

}

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

1 个答案:

答案 0 :(得分:0)

您是否检查了didBeginContact是否已访问过?

如果访问,请检查if条件

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

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