我在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
答案 0 :(得分:1)
用于创建物理实体的代码(体型kinametic和shape 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
答案 1 :(得分:0)
谢谢!这行得通! 有代码:
let ring = SCNTorus(ringRadius: 0.4, pipeRadius: 0.1)
ringNode.geometry = ring
let shapeOptions = [ SCNPhysicsShape.Option.type : SCNPhysicsShape.ShapeType.concavePolyhedron]
let physicShape = SCNPhysicsShape(geometry: ring, options: shapeOptions)
let body = SCNPhysicsBody(type: SCNPhysicsBodyType.kinematic,
shape: physicShape)
body.categoryBitMask = CollisionTypes.wall.rawValue
body.collisionBitMask = CollisionTypes.beachball.rawValue
body.isAffectedByGravity = false
body.mass = 0.5
ringNode.physicsBody = body