swift - ARKit – 球没有穿过 "SCNTorus"孔

标签 swift ios11 augmented-reality xcode9 arkit

我在 ARKit 上遇到了一个问题,我需要帮助。 我正在做一个小演示,我在场景中放置了一个简单的 SCNTorus 几何体,我试图将一个小球 (SCNSphere) 扔进环面孔中。问题是球在中间弹跳而不是通过。 有环面的代码:

let ring = SCNTorus(ringRadius: 0.4, pipeRadius: 0.1)
ring.ringSegmentCount = 100

let ringMaterial = SCNMaterial()
ringMaterial.diffuse.contents = UIImage(named: "art.scnassets/Ball/BeachBallColor.jpg")

ring.materials = [ringMaterial]

let ringNode = SCNNode()
ringNode.position = SCNVector3(x: location.worldTransform.columns.3.x,
                               y: location.worldTransform.columns.3.y + 0.8,
                               z: location.worldTransform.columns.3.z)

ringNode.geometry = ring

let body = SCNPhysicsBody(type: SCNPhysicsBodyType.kinematic,
                               shape: nil)
body.categoryBitMask = CollisionTypes.wall.rawValue
body.collisionBitMask = CollisionTypes.beachball.rawValue
// body.contactTestBitMask = CollisionTypes.beachball.rawValue
body.isAffectedByGravity = false
body.mass = 0.5

ringNode.physicsBody = body

sceneView.scene.rootNode.addChildNode(ringNode)

对于球:

let node = SCNNode(geometry: sphere!)
node.renderingOrder = 10

let body = SCNPhysicsBody(type: SCNPhysicsBodyType.dynamic,shape: nil)
body.categoryBitMask = CollisionTypes.beachball.rawValue
body.collisionBitMask = CollisionTypes.solid.rawValue|CollisionTypes.wall.rawValue|CollisionTypes.beachball.rawValue
// body.contactTestBitMask = CollisionTypes.fireball.rawValue|CollisionTypes.wall.rawValue
body.isAffectedByGravity = true
body.mass = 0.5
body.restitution = 0.75
body.damping = 0.1
body.friction = 0.8

node.physicsBody = body

最佳答案

用于创建物理体的代码( body 类型运动学和形状为 nil)导致几何体的简化“凸包”表示。简单地说,您看到的几何体是环面,但用于碰撞检测的几何体不是。

这行 (obj c) 代码实际上来自 Apple 示例代码项目之一:

_torus.physicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeStatic shape:[SCNPhysicsShape shapeWithGeometry:_torus.geometry options: @{SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConcavePolyhedron} ] ];

换句话说,您需要使用 SCNPhysicsShapeTypeConcavePolyhedron 键值(仅适用于静态物体)基于几何体本身创建静态类型物体和形状,以更准确地表示环面几何体作为物理体。

有关详细信息,请参阅:https://developer.apple.com/documentation/scenekit/scnphysicsshape

关于swift - ARKit – 球没有穿过 "SCNTorus"孔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46800601/

相关文章:

ios - 使用 Podfile 时,为什么 Xcode 将所有 Swift 库(多次)安装到我的设备上?

swift - 仅在满足特定条件时添加单元格

以编程方式创建的 Swift UIBarButtonItem 位置

swift - 创建一个类似于 Apple 的焦点方 block 的对象放置指示器

swift - 如何使用 ARKit 和 RealityKit 检测二维图像

ios - Facebook SDK 错误与 Branch(深层链接)相结合 - iOS

ios - Xcode 10 错误。 PhaseScriptExecution 失败,退出代码为非零 : errSecInternalComponent

ios - NSBundle.allFrameworks 不返回一些框架

xcode - 在 swift4 中制作圆形按钮

augmented-reality - 如何使用 Reality 套件在屏幕前设置实体?