3D libgdx旋转 [英] 3D libgdx rotation

查看:540
本文介绍了3D libgdx旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题是不是重复,或者如果是这样,我不明白如何使这项工作。

I think this question is not a duplicate, or if it is, I couldn't understand how to make this work.

(对不起,我伟大的绘图功能...)

(sorry for my great drawing capabilities...)

我有一个球,半径0.5(所以,周长为3.1415926)。
在每帧中,我有它在 oldPosition ,我想将它放在位置,所以,在蓝招箭头方向,再由图纸在v矢量psented $ p $。这很容易。

I have a ball, radius 0.5 (so, perimeter is 3.1415926). In each frame, I have it in oldPosition and I want to place it in position, so, move in the blue arrow direction, represented by the v vector in the drawing. This is easy.

关键是我想要的球在目标位置旋转。旋转轴线应垂直于所述方向(绿色虚线),和旋转应该在相对于目标中心(旋转将是2倍的翻译的长度)来进行。

The trick is that I want the ball to be rotated in the target position. The rotation axis should be perpendicular to the direction (green dashed line), and rotation should be performed in relation to the object center (rotation will be 2x the length of the translation).

在此刻我libgdx code如下:

My libgdx code at the moment looks like:

// During object initialization I have a Matrix4 with identify
Matrix4 rotation = new Matrix4();


// Now, this code during render()
Vector3 movement = position.sub(oldPosition);
float length = movement.len();

// drawing this on paper, it seems a correct way to compute orthogonal vector
rotation.rotateRad(new Vector3(-movement.z, 0f, movement.x), 2*length);

iSphere.transform = rotation.cpy().trn(position);

所以,基本上,我开始与识别Matrix4。在我申请一个旋转,它每一帧,应用旋转球体,并进行翻译。

So, basically, I start with the identify Matrix4. In each frame I apply a rotation to it, apply that rotation in the sphere, and translate it.

此似乎在第一工作(?),但很多转后,球开始在错误的方向转动。

This seems to work at first (?), but after a lot of rotations, ball starts rotating in the wrong direction.

我试着用四元数的实现,但没有运气,以及。它应该是简单的东西,我忘了。

I tried an implementation with Quaternions but did not have luck as well. It should be something simple that I am forgetting.

推荐答案

就像@Springrbua说,你可以采取跨产品,以获得轴绕。例如(未经测试code):

Like @Springrbua said you could take the cross product to get the axis to rotate around. For example (untested code):

public void update(float delta) {
    velocity.add(tmpV.set(acceleration).scl(delta));
    position.add(tmpV.set(velocity).scl(delta));
    final float speed = velocity.len();
    final float angle = speed*delta*MathUtils.radiansToDegrees;
    Vector3 axis = tmpV.set(velocity).scl(-1f/speed).crs(Vector3.Y);
    tmpQ.set(axis, angle);
    rotation.mulLeft(tmpQ);
    transform.set(position, rotation);
}

下面是一个工作实例的全部源代码: https://gist.github.com/xoppa/3b841fb52f46e8cdec24

Here's the full source of a working example: https://gist.github.com/xoppa/3b841fb52f46e8cdec24

这篇关于3D libgdx旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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