本文介绍了ARKit - 在用户的手机方向上施加力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以创建一个 SCNBox 并将其拍摄到屏幕上.这是有效的,但只要我将手机转向任何其他方向,力脉冲就不会更新,并且它总是在相同的旧位置拍摄盒子.

I have the following code that creates a SCNBox and shoots it on the screen. This works but as soon as I turn the phone in any other direction then the force impulse does not get updated and it always shoots the box in the same old position.

代码如下:

@objc func tapped(recognizer :UIGestureRecognizer) {

        guard let currentFrame = self.sceneView.session.currentFrame else {
            return
        }

        /
        let box = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)

        let material = SCNMaterial()
        material.diffuse.contents = UIColor.red
        material.lightingModel = .constant

        var translation = matrix_identity_float4x4
        translation.columns.3.z = -0.01

        let node = SCNNode()
        node.geometry = box
        node.geometry?.materials = [material]

        print(currentFrame.camera.transform)

        node.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)

        node.simdTransform = matrix_multiply(currentFrame.camera.transform, translation)
        node.physicsBody?.applyForce(SCNVector3(0,2,-10), asImpulse: true)

        self.sceneView.scene.rootNode.addChildNode(node)


    }

第 26 行是我施加力的地方,但它没有考虑用户当前的手机方向.我该如何解决?

Line 26 is where I apply the force but it does not take into account the user's current phone orientation. How can I fix that?

推荐答案

在第 26 行,您将一个常量向量传递给 applyForce.该方法采用世界空间中的向量,因此传递常量向量意味着您始终在同一方向上施加力 - 如果您想要一个基于相机或其他物体指向的方向的方向,则需要根据该方向计算向量.

On line 26 you're passing a constant vector to applyForce. That method takes a vector in world space, so passing a constant vector means you're always applying a force in the same direction — if you want a direction that's based on the direction the camera or something else is pointing, you'll need to calculate a vector based on that direction.

(新)SCNNode 属性 worldFront 在这里可能会很有帮助——它为您提供节点指向的方向,自动转换为世界空间,因此它对物理方法很有用.(尽管您可能想对其进行缩放.)

The (new) SCNNode property worldFront might prove helpful here — it gives you the direction a node is pointing, automatically converted to world space, so it's useful with physics methods. (Though you might want to scale it.)

这篇关于ARKit - 在用户的手机方向上施加力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 22:31