ios - 两个对象没有相互接触,但 contactTestBetween() 返回结果

标签 ios swift scenekit

我有一个 SceneKit 场景,我正在尝试检测两个对象何时相互接触。我有 2 个彼此靠近的物体,但它们没有接触。当我对这些对象进行接触测试时,函数 contactTestBetween() 返回一个接触点。为什么会这样?

首先,我将对象彼此放置得更远。如果我将对象彼此放置一定距离,contactTestBetween() 不会返回值。好像有一个神奇的数字。

其次,我对这两个对象之间的碰撞检测进行了相同的测试。同样,如果物体彼此之间的距离低于一定距离,它们就会开始移动。更有趣的是,碰撞检测的距离限制低于接触测试的距离限制。

scnView.debugOptions = .showPhysicsShapes

这是打开的,所以我可以看到物理形状有多大。

box!.physicsBody = SCNPhysicsBody(type: .dynamic,
                                 shape: SCNPhysicsShape(
                              geometry: box!.geometry!,
                               options: [SCNPhysicsShape.Option.type: 
                                            SCNPhysicsShape.ShapeType.boundingBox]))

box2!.physicsBody = SCNPhysicsBody(type: .dynamic,
                                  shape: SCNPhysicsShape(
                               geometry: box2!.geometry!,
                                options: [SCNPhysicsShape.Option.type: 
                                             SCNPhysicsShape.ShapeType.boundingBox]))

box!.physicsBody!.isAffectedByGravity = false
box2!.physicsBody!.isAffectedByGravity = false

box!.physicsBody!.categoryBitMask = 2
box2!.physicsBody!.categoryBitMask = 4

box!.physicsBody!.contactTestBitMask = 4
box2!.physicsBody!.contactTestBitMask = 2

上面是我用来为对象创建物理体的代码。

let contactResult = scnView.scene?.physicsWorld.contactTestBetween(box!.physicsBody!, box2!.physicsBody!)

这就是我如何进行对象之间的接触测试。

这是模拟器的截图:

enter image description here

我不明白为什么会这样。非常感谢您的帮助!

最佳答案

下面是关于 contactTestBetween(_:_:options:) 实例方法的文档:

SceneKit sends messages to the physics world’s contactDelegate object only when collisions occur between bodies whose collisionBitMask and categoryBitMask properties overlap, and only for collisions between certain types of bodies. (For details, see SCNPhysicsBodyType.) Use this method to directly test for contacts between any two bodies at a time of your choosing. For example, to implement a game where the player character can pick up an item, you might call this method when the player presses the “pick up” button to see if the player character is in contact with the item to be picked up.

但是您的 .categoryBitMask.contactTestBitMask 对于这两个对象是不同的

还有什么.collisionBitMask是吗?

还有一件事:

For nodes containing custom geometry, the physics shape SceneKit automatically creates is a rough approximation of the geometry. This approximation, or convex hull, provides a compromise between accuracy and performance in collision detection.

For the best collision detection performance, create an SCNPhysicsShape instance based on a basic geometry class (SCNBox, SCNSphere, SCNPyramid, SCNCone, SCNCylinder, or SCNCapsule).

希望这对您有所帮助。

关于ios - 两个对象没有相互接触,但 contactTestBetween() 返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55543546/

相关文章:

ios - 可设计构建失败: Could not attach to pid : "26000"

ios - 如何在 iOS UItest 中选择本地化的日期选择器项目

animation - SceneKit Swift3 编译器错误

ios - 为什么 NSSearchPathForDirectoriesInDomains 返回一个 NSArray?

ios - 如何从 Flutter 代码在 Swift 中打开相机 View 以访问自动对焦等相机功能

iphone - 通过子类类型数组和调用类方法进行客观迭代

ios - 将具有物理特性的 collada 场景加载到 SceneKit

swift - 将 SCNLight 添加到 SCNNode 会使 SCNNode 变黑

ios - 无法从 PFObject 获取解析文件

ios - 以编程方式 Swift Segue