gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:29:09 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com