【问题标题】:How to position a SCNPhysicsBody at the center of a scnnode如何将 SCNPhysicsBody 定位在 scnnode 的中心
【发布时间】:2020-07-22 09:27:27
【问题描述】:

我有一个SCNNode 和一个完整的工作物理体,但物理体中心/静止点位于SCNNode 的底部。

这就是我想要做的:

  • 弄清楚如何将物理体定位在节点的中心。
  • 弄清楚如何以某种方式控制其定位。

到目前为止,Apple Developer 还没有帮助我。这是我所拥有的:

let nodePhysicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(geometry: SCNCapsule(capRadius: 2.0, height: 2.0), options: nil))

我找到了这个init方法,但我不确定它是否会起作用,或者如何实现它。

.init(circleOfRadius r: CGFloat, center: CGPoint()

【问题讨论】:

    标签: ios swift scenekit


    【解决方案1】:

    我建议尝试使用 SCNPhysicsShape init(shapes:transforms:) 初始化器——而不是从几个形状中构造一个物理形状(并使用 transforms 参数将它们相对于彼此定位),您也许可以通过一个单一的形状和一个从父坐标空间的中心偏移它的变换。

    【讨论】:

      【解决方案2】:

      举一个完整的例子(我的物理以 .dae 动画的基础为中心,我需要以 SCNNode 的质心为中心的物理体):

      // refNode is the SCNReferenceNode loaded from the .dae animation
       let kCharacterScale = SCNVector3(20.0,20.0,20.0)   //Set the node and physics scales
       let boundingBox = refNode.boundingBox
       let min = boundingBox.min
       let max = boundingBox.max
       let w = (max.x - min.x) 
       let h = (max.y - min.y) 
       let l = (max.z - min.z) 
       let boxShape = SCNBox (width: CGFloat(w) , height: CGFloat(h) , length: CGFloat(l), chamferRadius: 0.0)
       var shape = SCNPhysicsShape(geometry: boxShape, options: [SCNPhysicsShape.Option.scale : kCharacterScale, SCNPhysicsShape.Option.type : SCNPhysicsShape.ShapeType.boundingBox])
       let translate = SCNMatrix4MakeTranslation(0.0, (h*kCharacterScale.y/2.0), 0.0)
       let translateMatrix = NSValue.init(scnMatrix4: translate)
       shape = SCNPhysicsShape.init(shapes: [shape], transforms: [translateMatrix])
              refNode.physicsBody = SCNPhysicsBody(type: .kinematic, shape: shape)
      

      【讨论】:

        猜你喜欢
        • 2017-10-27
        • 2018-06-17
        • 2019-05-04
        • 1970-01-01
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        • 2018-01-30
        • 2019-11-14
        相关资源
        最近更新 更多