安卓.LibGDX.如何沿样条线移动和定向 3D 对象 [英] Android. LibGDX. How to move and orient 3D-object along a spline

查看:31
本文介绍了安卓.LibGDX.如何沿样条线移动和定向 3D 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 LibGDX 用于我的 Android 应用程序.我需要沿着样条移动和定向 3D 对象(ModelInstance).在我的例子中它是 CatmullRomSpline.我让运动正常工作,但在沿样条线定向 ModelInstance 时遇到问题.

I'm using LibGDX for my Android app. I need to move and orient 3D object(ModelInstance) along a spline.In my case it's CatmullRomSpline. I got the movement working but having problem with orienting the ModelInstance along the spline.

我的代码:

public void update() {
    float t = SPEED * Gdx.graphics.getDeltaTime();
    elapsedTime = elapsedTime + SPEED *  Gdx.graphics.getDeltaTime();       

    //Movement
    catmull.valueAt(value, elapsedTime);
    myObject3d.transform.setTranslation(value);

    //Rotation
    derivative = catmull.derivativeAt(derivative, t);
    derivative = derivative.nor();

    //Tried to bring object into default position before rotating
    //Vector.X - is the default direction of my Object - facing right
    //myObject3d.transform.rotate(Vector3.X, Vector3.X);

    myObject3d.transform.rotate(derivative, Vector3.X);             
}

推荐答案

Matrix4#rotate 方法有两个参数,basetarget.在您的情况下,基本向量应该是 Vector3.X,而目标是 derivative.您需要为此交换参数.此外,Matrix4#rotate 方法将旋转后乘以现有旋转.或者换句话说:这将在每次调用时累积绝对旋转.您可能想要使用 Matrix4#setToRotation 方法,它在每次调用时重置旋转(清除矩阵).

The Matrix4#rotate method has two arguments, base and target. In your case the base vector should be Vector3.X, while the target is derivative. You need to swap the arguments for this. Also, the Matrix4#rotate method post-multiplies the rotation on top the existing rotation. Or in other words: this will accumulate the absolute rotation on every call. You probably want to use the Matrix4#setToRotation method instead, which resets the rotation on every call (clears the matrix).

myObject3d.transform.setToRotation(Vector3.X, derivative);
myObject3d.transform.setTranslation(value);

虽然这在大多数情况下可能会奏效,但根据路径的不同,您可能会得到意想不到的结果.如上所述,有无限可能的旋转会导致这个方向.因此最好使用两个额外的向量来指定向上 (Y) 和向右 (Z) 向量.

While this probably will work in most cases, you might get unexpected results depending on the path. Like said, there are infinite possible rotations that will result in this direction. Therefor it's better to use two additional vectors to specify the up (Y) and right (Z) vector.

right.set(Vector3.Y).crs(derivative).nor();
up.set(right).crs(derivative).nor();
myObject3d.transform.set(derivative, up, right, position).rotate(Vector3.X, 180);

这里的叉积用于计算右向量和上向量,这里假设Vector3.Y通常接近上向量.然后 Matrix4#set 方法用于设置要反映的矩阵这些值.第一个参数指定新"X 轴,第二个参数指定新"Y 轴,第三个参数指定新"Z 轴,最后一个参数指定平移(位置).

Here the cross product is used to calculate the right and up vectors, where it is assumed that Vector3.Y is usually close to the up vector. Then the Matrix4#set method is used to set the matrix to reflect these values. The first argument specifies the "new" X-axis, the second the "new" Y-axis, the third the "new" Z-axis and the last argument specifies the translation (location).

这是一个工作示例:https://gist.github.com/xoppa/7558c0c75e9534795e9f

尽管无关,但请记住 path.valueAt 方法使用 0 <= t <= 1.或者换句话说,t(代码中的elapsedTime)不应低于 0 或高于 1.

Although unrelated, keep in mind that path.valueAt method uses 0 <= t <= 1. Or in other words, t (elapsedTime in your code) should not be below 0 or above 1.

这篇关于安卓.LibGDX.如何沿样条线移动和定向 3D 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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