Android系统。如何(使用的Vector3和四元),其面对的方向移动对象 [英] Android. How to move object in the direction it is facing (using Vector3 and Quaternion)

查看:400
本文介绍了Android系统。如何(使用的Vector3和四元),其面对的方向移动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用libGDX(很新的这实际上)和Android。我想在它面对(使用一些速度)的方向移动3D对象。我认为这是一个基本的问题,但无法找到一个直nswer它。我有一个四元数再presenting对象的旋转(方向)和我有一个重新的Vector3 presenting对象的位置。现在的问题是如何才能使用信息,从四元到四元数由psented方向重新$ P $移动对象更新位置的Vector3。 (这另一种方法是从四元提取辊距和偏航并通过应用三角计算得到新的COORDS。但我认为必须有办法用这个的Vector3和季铵盐来实现的。)

I'm using libGDX (quite new to it in fact) and Android. And I want to move 3d object in the direction it is facing (using some speed). I thinks it's a basic question but can't find a straight nswer for it. I have a Quaternion representing object rotation(direction) and I have a Vector3 representing the object position. The question is how to update the position Vector3 using info from Quaternion in order to move object in the direction represented by Quaternion. (An alternative to this is extract roll pitch and yaw from Quaternion and get the new coords by applying trigonometric calculations. But i think there must be a way to achieve this using Vector3 and Quat.)

推荐答案

四元数用于指定的旋转。首先,您需要在不应用旋转指定方向。例如,如果你想在不加旋转沿X轴移动:

Quaternion is used to specify a rotation. You first need to specify the direction when no rotation is applied. For example if you want to move along the X axis when no rotation is applied:

Vector3 baseDirection = new Vector3(1,0,0);

确认的基本方向是标准化(长度= 1),你可以使用,也没有()方法是安全的:

Make sure the base direction is normalized (length = 1),you can use the nor() method to be safe:

Vector3 baseDirection = new Vector3(1,0,0).nor();

接下来,您需要使用四元数旋转方向:

Next you'll need to rotate the direction using the Quaternion:

Vector3 direction = new Vector3();
Quaternion rotation = your_quaternion;
direction.set(baseDirection);
direction.mul(rotation);

现在,你有方向,你可以与你要移动它的量缩放。你可能会想这样做的每一帧,并根据自上一帧经过的时间。

Now that you have the direction, you can scale it with the amount you want to move it. You'll probably want to do this every frame and depending on the time elapsed since the last frame.

final float speed = 5f; // 5 units per second
Vector3 translation = new Vector3();
translation.set(direction);
translation.scl(speed * Gdx.graphics.getDeltaTime());

最后,您需要翻译添加到位置

Finally you need to add the translation to the position

position.add(translation);

当然,这取决于你的实际执行可以分批多次操作,例如:

Of course, depending on your actual implementation you can batch multiple operations, e.g.:

translation.set(baseDirection).mul(rotation).scl(speed * Gdx.graphics.getDeltaTime());
position.add(translation);

更新:
从Xoppa的评论添加工作code:

Update: Adding working code from Xoppa's comment:

translation.set(baseDirection).rot(modelInstance.transform).nor().scl(speed * delta)

这篇关于Android系统。如何(使用的Vector3和四元),其面对的方向移动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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