ARKit - 在用户的电话方向上施加力量 [英] ARKit - Applying Force in User's Phone Direction

查看:88
本文介绍了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 - 在用户的电话方向上施加力量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆