接触时,物理形状与对象的形状不匹配

时间:2018-06-21 09:09:18

标签: swift scenekit arkit

我有两个对象,当它们都接触时,我希望发生一些事情。一个对象是SCNSphere,另一个是SCNCylinder。唯一的问题是,当我将球扔向圆柱时,即使有间隙,它们似乎仍在接触。如果我把它扔得很远,那么它将按预期工作。我该如何使联系准确无误,还有很多差距?看起来PhysicsShape与我的对象的形状不匹配。我希望它是准确的。有帮助吗?

我的气缸代码:

let scorer = SCNCylinder(radius: 0.02, height: 0.01)
let material = SCNMaterial()
material.diffuse.contents = UIImage(named: "basketballSkin.png")
scorer.materials = [material]

let scorerNode = SCNNode(geometry: scorer)

scorerNode.worldPosition = SCNVector3(x: 0, y: -1.35, z: -1.4)

let physicsShapesc = SCNPhysicsShape(node: scorerNode, options:[SCNPhysicsShape.Option.type: SCNPhysicsShape.ShapeType.concavePolyhedron])

let physicsBodysc = SCNPhysicsBody(type: .static, shape: physicsShapesc)

scorerNode.physicsBody = physicsBodysc

scorerNode.physicsBody?.categoryBitMask = BodyType.scorer.rawValue scorerNode.physicsBody?.collisionBitMask = BodyType.scorer.rawValue | BodyType.ball.rawValue scorerNode.physicsBody?.contactTestBitMask = BodyType.scorer.rawValue | BodyType.ball.rawValue

我的球代码:

let ball = SCNSphere(radius:0.04)
   // Bucketnode.scale = SCNVector3Make(0.2,0.2,0.2);
    let material = SCNMaterial()
    material.diffuse.contents = UIImage(named: "basketballSkin.png")
    ball.materials = [material]

    let ballNode = SCNNode(geometry: ball)

    ballNode.position = cameraPosition

    let physicsShape = SCNPhysicsShape(node: ballNode, options:nil)

    let physicsBody = SCNPhysicsBody(type: .dynamic, shape: physicsShape)

    ballNode.physicsBody = physicsBody


    let forceVector:Float = 2.7

    ballNode.physicsBody?.applyForce(SCNVector3Make(cameraPosition.x * forceVector, cameraPosition.y * forceVector, cameraPosition.z*forceVector), asImpulse: true)

    ballNode.physicsBody?.categoryBitMask = BodyType.ball.rawValue
    //ballNode.physicsBody?.collisionBitMask = BodyType.ball.rawValue | BodyType.scorer.rawValue
    ballNode.physicsBody?.contactTestBitMask = BodyType.ball.rawValue | BodyType.scorer.rawValue

    sceneView.scene.rootNode.addChildNode(ballNode)

  DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { // change 2 to desired number of seconds
 ballNode.removeFromParentNode()


   }

1 个答案:

答案 0 :(得分:0)

考虑到SCNPhysicsShape.ShapeType结构的定义,将几何视为“类型键的值,用于指定SceneKit在基于 geometry 创建物理形状时使用的详细程度”。该选项集可能无法按预期工作:

let physicsShapesc = SCNPhysicsShape(node: scorerNode, options:[SCNPhysicsShape.Option.type: SCNPhysicsShape.ShapeType.concavePolyhedron])

此外,球朝铲斗移动的速度有多快? “ concavePolyhedron”似乎是最苛刻的渲染类型