根据其四元数在 3D 中移动 cannonJS 对象 [英] Moving cannonJS object in 3D according to its quaternion

查看:37
本文介绍了根据其四元数在 3D 中移动 cannonJS 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一艘太空船,就像在 cannonJS 身体上移动一样.(我真的是初学者)我找到了一些例子,但没有一个是我正在寻找的.

据我所知,移动物体的正确方法是改变它的速度.

这是我在这里所做的:http://codepen.io/Tomo0613/pen/xVjqqK 在此输入代码但我在理解四元数方面肯定有问题.

有没有办法在我旋转时根据其四元数更新身体的向量,或者总是与世界相关?结果:当身体仅在 Z 轴上加速时,它总是会朝着它所面对的方向移动.

解决方案

Body 上的速度始终处于世界坐标系中.对于这种情况,您可能希望跟踪变量中的局部速度(例如 vec(0,0,+1)),然后将其转换为 world速度,并在每次旋转改变时将其应用到 Body.

var localVelocity = new CANNON.Vec3(0, 0, 1);var worldVelocity = body.quaternion.vmult(localVelocity);body.velocity.copy(worldVelocity);

或者,相同代码的更快但更难阅读的版本:

var localVelocity = new CANNON.Vec3(0, 0, 1);body.quaternion.vmult(localVelocity, body.velocity);

I would like to achieve a space ship like move on a cannonJS body. (Im really a beginner) I found some examples but none of them are exactly what I'm looking for.

As i know the correct way to move an object, is to change its velocity.

Here's what I have done here: http://codepen.io/Tomo0613/pen/xVjqqK enter code here But i definitely have problems with understanding quaternions.

Is there a way to update, the vector of the body according to its quaternion as i rotate it, or that's always related to the world? Resulting: when the body is accelerating only on the Z axis, it would always move in the direction, where it's facing.

解决方案

The velocity on the Body is always in world coordinates. For this case, you probably want to keep track of the local velocity (e.g. vec(0,0,+1)) in a variable, and then convert that to world velocity and apply it to the Body every time rotation changes.

var localVelocity = new CANNON.Vec3(0, 0, 1);
var worldVelocity = body.quaternion.vmult(localVelocity);
body.velocity.copy(worldVelocity);

Or, a faster but harder-to-read version of the same code:

var localVelocity = new CANNON.Vec3(0, 0, 1);
body.quaternion.vmult(localVelocity, body.velocity);

这篇关于根据其四元数在 3D 中移动 cannonJS 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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