应用转换后如何获得真正的方向/旋转值?

发布于 2025-02-08 09:45:08 字数 1188 浏览 1 评论 0 原文

在我的代码中,我当前使用 .loadAsync(nund string)方法加载实体,然后添加到我的现有 aNchorentity 。作为测试,我然后将我的实体旋转90°,并想确定如何获得当前的旋转角度。

长期的意图是,我将允许用户旋转模型,但要限制一定程度的旋转(即,用户可以将模型的音调旋转到90°或-90°,但没有不仅如此)。在不知道实体的当前旋转角度的情况下,我不确定可以使用什么逻辑来限制这一点。

Entity.loadAsync(named: "myModel.usdz")
    .receive(on: RunLoop.main)
    .sink { completion in
        // ...
    } receiveValue: { [weak self] entity in

        guard let self = self else { return }
        self.objectAnchor.addChild(entity)
        scene.addAnchor(objectAnchor)
        
        let transform = Transform(pitch: .pi / 2,
                                    yaw: .zero,
                                   roll: .zero)
        
        entity.setOrientation(transform.rotation,
                                  relativeTo: nil)
        
        print(entity.orientation)

        // Sample Output: simd_quatf(real: 0.7071069, 
        //                           imag: SIMD3<Float>(0.7071067, 0.0, 0.0))
    }
    .store(in: &subscriptions)

我本来希望 entity.corientation 给我 90.0 1.57 .pi/2 ),但是不确定如何以与预期角度对齐的形式获得实体的电流

In my code, I am currently loading an Entity using the .loadAsync(named: String) method, then adding to my existing AnchorEntity. As a test, I am then rotating my Entity 90°, and would like to determine how to then get the current angle of rotation.

The long-term intent is that I am going to allow users to rotate a model, but want to limit the rotation to a certain degree (I.E., the user can rotate the pitch of the model to 90° or -90°, but no further than that). Without being able to know the current angle of rotation for the Entity, I am unsure what logic I could use to limit this.

Entity.loadAsync(named: "myModel.usdz")
    .receive(on: RunLoop.main)
    .sink { completion in
        // ...
    } receiveValue: { [weak self] entity in

        guard let self = self else { return }
        self.objectAnchor.addChild(entity)
        scene.addAnchor(objectAnchor)
        
        let transform = Transform(pitch: .pi / 2,
                                    yaw: .zero,
                                   roll: .zero)
        
        entity.setOrientation(transform.rotation,
                                  relativeTo: nil)
        
        print(entity.orientation)

        // Sample Output: simd_quatf(real: 0.7071069, 
        //                           imag: SIMD3<Float>(0.7071067, 0.0, 0.0))
    }
    .store(in: &subscriptions)

I would have expected entity.orientation to give me something like 90.0 or 1.57 (.pi / 2), but unsure how I can get the current rotation of the Entity in a form that would align with expected angles.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油饼 2025-02-15 09:45:08

simd_quatf

在REALEKIT 2.0中,检索 SIMD_QUATF 中的结构值。 (标量)和 imaginary (vector) quaternion

public init(real: Float, imag: SIMD3<Float>)

汉密尔顿的季节表达式如下:

”“在此处输入图像说明”

在您的情况下,值 0.707 对应于45度的旋转角度。如果您同时将设置为Real a )和 imag.x.x bi )代码>,对于X轴,您将获得90度的总旋转角度。

易于检查:

let quaternion = simd_quatf(real: 0.707, imag: [0.707, 0, 0])   // 90 degrees
entity.orientation = quaternion

要检查哪些“可读”模型的方向是,请使用另一个初始化器中的常规参数:

public init(angle: Float, axis: SIMD3<Float>)

print(entity.orientation.angle)         // 1.5707964
print(entity.orientation.axis)          // SIMD3<Float>(0.99999994, 0.0, 0.0)

simd_quatf

In RealityKit 2.0, when retrieving simd_quatf structure's values from .orientation and .transform.rotation instance properties, the default initializer brings real (scalar) and imaginary (vector) parts of a Quaternion:

public init(real: Float, imag: SIMD3<Float>)

Hamilton's quaternion expression looks like this:

enter image description here

In your case, the value 0.707 corresponds to a rotation angle of 45 degrees. If you set both real (a) and imag.x (bi) to 0.707, you'll get a total rotation angle of 90 degrees for X axis.

enter image description here

It's easy to check:

let quaternion = simd_quatf(real: 0.707, imag: [0.707, 0, 0])   // 90 degrees
entity.orientation = quaternion

To check what "readable" model's orientation is, use regular parameters from another initializer:

public init(angle: Float, axis: SIMD3<Float>)

print(entity.orientation.angle)         // 1.5707964
print(entity.orientation.axis)          // SIMD3<Float>(0.99999994, 0.0, 0.0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文