平滑的旋转与四元平滑

2023-09-08 00:58:04 作者:下辈子别做痴情种

四元数可以形容,不仅旋转,而且也是一个方向,即旋转从最初的(零)位置。

Quaternion can describe not only rotation, but also an orientation, i.e. rotation from initial (zero) position.

我是希望从一个模特平滑旋转到另一台。我计算的起始方向 startOrientation 和最终的方向 endOrientation ,并希望描述的中间方向为 startOrientation *(1-参数)+ endOrientation *参数,而参数 0 修改 1

I was wishing to model smooth rotation from one orientation to another. I calculated start orientation startOrientation and end orientation endOrientation and was wishing to describe intermediate orientations as startOrientation*(1-argument) + endOrientation*argument while argument changes from 0 to 1.

在code猴引擎更新功能如下:

The code for monkey engine update function is follows:

@Override
    public void simpleUpdate(float tpf) {

        if( endOrientation != null ) {

            if( !started ) {
                started = true;
            }
            else {

                fraction += tpf * speed;
                argument = (float) ((1 - Math.cos(fraction * Math.PI)) / 2);

                orientation = startOrientation.mult(1-argument).add(endOrientation.mult(argument));
                //orientation = startOrientation.mult(1-fraction).add(endOrientation.mult(fraction));
                log.debug("tpf = {}, fraction = {}, argument = {}", tpf, fraction, argument);
                //log.debug("orientation = {}", orientation);

                rootNode.setLocalRotation(orientation);

                if( fraction >= 1 ) {

                    rootNode.setLocalRotation(endOrientation);
                    log.debug("Stopped rotating");

                    startOrientation = endOrientation = null;
                    fraction = 0;
                    started = false;
                }
            }
        }


    }

余弦公式预期模型平滑加速在开始和减速末。

The cosine formula was expected to model smooth accelerating at the beginning and decelerating at the end.

在code ++工程,但并不如预期:在平稳的旋转开始之前分数参数值达到 1 ,我不明白,为什么。

The code works but not as expected: the smooth rotation starts and finishes long before fraction and argument values reach 1 and I don't understand, why.

为什么定位值达到 endOrientation 这么快?

推荐答案

您已经表示,在您的案件 startOrientation 已被修改。然而;下面仍是如此。

You have stated that in your case startOrientation was being modified. However; the following remains true

方法球面线性插值包含在四元数类用于此目的之内:的两个旋转之间进行插值。

The method slerp is included within the Quaternion class for this purpose: interpolating between two rotations.

假设我们有两个四元数 startOrientation endOrientation 和我们想要的点插入他们之间那么我们插值然后使用以下code之间:

Assuming we have two quaternions startOrientation and endOrientation and we want the point interpolation between them then we interpolate between then using the following code:

float interpolation=0.2f;
Quaternion result=new Quaternion();
result.slerp(startOrientation, endOrientation, interpolation);

为什么你的方法可能是危险的

四元数是有些复杂的内部,并按照有所不同的数学规则说的载体。你叫乘(浮标)上的四元数法。在内部,这看起来像这样

Why your approach may be dangerous

Quaternions are somewhat complex internally and follow somewhat different mathematical rules to say vectors. You have called the multiply(float scalar) method on the quaternion. Internally this looks like this

public QuaternionD mult(float scalar) {
        return new QuaternionD(scalar * x, scalar * y, scalar * z, scalar * w);
}

因此​​,它只是做所有元素的简单乘法。这明确不返回轮换是倍。其实这样的四元数不再重新presents有效的轮换,因为在所有的不再是一个四元数。如果你叫正常化这个quaterion将立即撤消缩放。我敢肯定,四元#乘法(浮点标量)有一定用途,但我还没有找到他们。

So it just does a simple multiplication of all the elements. This explicitly does not return a rotation that is scalar times the size. In fact such a quaternion no longer represents a valid rotation at all since its no longer a unit quaternion. If you called normalise on this quaterion it would immediately undo the scaling. I'm sure Quaternion#multiply(float scalar) has some use but I am yet to find them.

这也是一个添加四元数不将它们结合起来的情况。事实上,你将它们相乘。因此,结合一季度则Q2 Q3随后将实现如下:

It is also the case that "adding" quaternions does not combine them. In fact you multiply them. So combining q1 then q2 then q3 would be achieved as follows:

Quaternion q0 = q1.mult(q2).mult(q3);

借助小抄是这个非常有用

在你的情况公式进行插值几乎但不完全正确。这表明偏航对使用这两种方法之间的插值2四元数的图

In your case your formula for interpolation is nearly but not quite correct. This shows a graph of yaw for interpolation between 2 quaternions using both methods

 
精彩推荐