如何避免使用设备方向避免使用kimbal锁定,以将相机位置移动三j?
我正在尝试使用浏览器的设备方向
在三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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论