如何避免使用设备方向避免使用kimbal锁定,以将相机位置移动三j?

发布于 2025-02-10 08:48:14 字数 1092 浏览 4 评论 0原文

我正在尝试使用浏览器的设备方向在三js场景中移动相机,但是当beta命中90时,我无法避免摄影锁。

我在这里创建了一个演示站点:

“ https://mattparrilla.github.io/gimbal-lock” rel =“ nofollow noreferrer”> https://mattparrilla.github.io/gimbal-lock 要肖像(因此电话沿重力矢量直立),然后摇摆到周围,看到立方体疯狂地移动。

这是我的代码的肉:

let initialOrientation;
function onDeviceOrientation (e) {
  // use initial angle as our "origin"
  if (!initialOrientation) {
    initialOrientation = {
      alpha: e.alpha,
      beta: e.beta,
      gamma: e.gamma
    }
  }
  
  // move camera X and Y positions between -5 and 5 depending on
  // angles of beta and gamma, keeping Z position fixed
  // the calc[Gamma|Beta]Offset functions maps my angle between -1 and 1 for use with lerp
  camera.position.set(
    THREE.MathUtils.lerp(0, 5, calcGammaOffset(e.gamma, initialOrientation.gamma)),
    THREE.MathUtils.lerp(0, 5, calcBetaOffset(e.beta, initialOrientation.beta)),
    5
  )
  camera.lookAt(0, 0, 0);
}

我知道我需要使用四元组或旋转矩阵,并且我可以计算出两种方法(三js具有方便的方法),我只是不够理解它们,以将它们从现有示例转换为我自己的用例。

可以在此处查看整个应用>

I'm trying to use the browser's deviceorientation to move a camera in a ThreeJS scene, but am unable to avoid gimbal lock when beta hits 90.

I've created a demo site here: https://mattparrilla.github.io/gimbal-lock

Load the site on a mobile device and move the phone to portrait (so phone is upright along the gravity vector) and wiggle it around to see the cube move wildly.

Here is the meat of my code:

let initialOrientation;
function onDeviceOrientation (e) {
  // use initial angle as our "origin"
  if (!initialOrientation) {
    initialOrientation = {
      alpha: e.alpha,
      beta: e.beta,
      gamma: e.gamma
    }
  }
  
  // move camera X and Y positions between -5 and 5 depending on
  // angles of beta and gamma, keeping Z position fixed
  // the calc[Gamma|Beta]Offset functions maps my angle between -1 and 1 for use with lerp
  camera.position.set(
    THREE.MathUtils.lerp(0, 5, calcGammaOffset(e.gamma, initialOrientation.gamma)),
    THREE.MathUtils.lerp(0, 5, calcBetaOffset(e.beta, initialOrientation.beta)),
    5
  )
  camera.lookAt(0, 0, 0);
}

I know I need to use either quaternions or rotation matrices, and I can calculate either (ThreeJS has convenient methods) I just don't understand them well enough to translate from existing examples to my own use case.

The entire app can be viewed here: https://github.com/mattparrilla/gimbal-lock

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文