Android系统。 LibGDX。如何移动和定向3D物体沿样条线 [英] Android. LibGDX. How to move and orient 3D-object along a spline

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

问题描述

我用LibGDX我的Andr​​oid应用程序。我需要移动和定向3D对象(ModelInstance)沿spline.In我的情况下,它是CatmullRomSpline。我得到了移动工作,但与定向ModelInstance沿花键有问题。

我的code:

 公共无效更新(){
    浮动T =速度* Gdx.graphics.getDeltaTime();
    elapsedTime = elapsedTime +速度* Gdx.graphics.getDeltaTime();

    //运动
    catmull.valueAt(值,elapsedTime);
    myObject3d.transform.setTranslation(值);

    //旋转
    衍生物= catmull.derivati​​veAt(衍生物,吨);
    衍生物= derivati​​ve.nor();

    //试图将对象转换成默认位置旋转的前
    //Vector.X  - 是我对象的默认方向 - 朝右
    //myObject3d.transform.rotate(Vector3.X,Vector3.X);

    myObject3d.transform.rotate(衍生物Vector3.X);
}
 

解决方案

的<一个href="http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Matrix4.html#rotate-com.badlogic.gdx.math.Vector3-com.badlogic.gdx.math.Vector3-"相对=nofollow> Matrix4#旋转的方法有两个参数,基础目标。你的情况基本载体应该是 Vector3.X ,而目标是衍生。您需要交换的参数这一点。另外,在<一href="http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Matrix4.html#rotate-com.badlogic.gdx.math.Vector3-com.badlogic.gdx.math.Vector3-"相对=nofollow> Matrix4#旋转方法后乘上顶部的现有旋转的旋转。或者换句话说:这会堆积在每次调用的绝对旋转。你可能想使用<一个href="http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Matrix4.html#setToRotation-com.badlogic.gdx.math.Vector3-com.badlogic.gdx.math.Vector3-"相对=nofollow> Matrix4#setToRotation 的方法来代替,该重置每次通话的旋转(清除矩阵)。

  myObject3d.transform.setToRotation(Vector3.X,衍生物);
myObject3d.transform.setTranslation(值);
 

虽然这可能会在大多数情况下,你可能会得到不同的道路上意想不到的效果。就像说,有这将导致在这个方向无限可能的旋转。为此,最好使用另外两个向量来指定向上(Y),右(Z)矢量。

  right.set(Vector3.Y).crs(衍生).nor();
up.set(右).crs(衍生).nor();
myObject3d.transform.set(导数,上,右,位置).rotate(Vector3.X,180);
 

下面的叉积来计算所述右和上载体,其中,假定 Vector3.Y 通常接近于向上矢量。那么<一href="http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Matrix4.html#set-com.badlogic.gdx.math.Vector3-com.badlogic.gdx.math.Vector3-com.badlogic.gdx.math.Vector3-com.badlogic.gdx.math.Vector3-"相对=nofollow> Matrix4#设定的方法被用于设置矩阵以反映这些值。第一个参数指定新的X轴,在第二新Y轴,第三新的Z轴和最后一个参数指定了翻译(位置)。

下面是一个工作的例子: https://gist.github.com/xoppa/7558c0c75e9534795e9f

虽然无关,请记住,<一个href="http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Path.html#valueAt-T-float-"相对=nofollow> path.valueAt 方法使用 0℃= T&LT; = 1 。或者换句话说, T elapsedTime 在code)项应不低于0或高于1。

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.

My Code:

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);             
}

解决方案

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);

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);

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).

Here's a working example: https://gist.github.com/xoppa/7558c0c75e9534795e9f

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.

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

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