旋转矢量 [英] Rotating a vector

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

问题描述

我想做一个简单的向量旋转。

I want to do a simple vector rotation.

目标是使我的第一人称照相机当前指向目标t与方向d到具有新方向d1的新目标t1。

The goal is to head my first-person camera which is currently pointing to to target t with direction d to a new target t1 with a new direction d1.

d和d1之间的过渡应该是平滑运动。

The transition between d and d1 should be a smooth movement.

With

public void FlyLookTo(Vector3 target) {

        _flyTargetDirection = target - _cameraPosition;
        _flyTargetDirection.Normalize();

        _rotation = new Matrix();

        _rotationAxis = Vector3.Cross(Direction, _flyTargetDirection);

         // This bool tells the Update()-method to trigger the changeDirection() method.
        _isLooking = true;
    }

我使用新参数启动方向更改,

I am initiating the direction change with its new parameter and with

// this method gets executed by the Update()-method if the isLooking flag is up.
private void _changeDirection() {

        dist = Vector3.Distance(Direction, _flyTargetDirection);

        // check whether we have reached the desired direction
        if (dist >= 0.00001f) {

            _rotationAxis = Vector3.Cross(Direction, _flyTargetDirection);
            _rotation = Matrix.CreateFromAxisAngle(_rotationAxis, MathHelper.ToRadians(_flyViewingSpeed - Math.ToRadians(rotationSpeed)));


            // update the cameras direction.
            Direction = Vector3.TransformNormal(Direction, _rotation);
        } else {

            _onDirectionReached();
            _isLooking = false;
        }
    }

我正在执行实际动作。

我的问题:实际移动工作正常,但移动速度减慢越多,当前方向越接近所需的方向,这使它非常

My Problem: The actual movement works fine but the speed of the movement slows down the more the current direction gets closer to the desired direction which makes it a very unpleasant movement if executes several times in a row.

如何使相机以相同的速度从d方向移动到d1方向?

How can I make the camera move from direction d to direction d1 with an equal speed over its movement ?

推荐答案

您的代码看起来很实用。 _flyViewingSpeed或rotationSpeed变化吗?

your code looks pretty solid. does _flyViewingSpeed or rotationSpeed change at all?

另一种方法是使用Vector3.Lerp(),这将完全按照你想做的。注意,您需要使用初始的开始和目标方向,而不是当前的方向 - 或者您将得到不同的速度变化。

Another approach would be to use Vector3.Lerp() which will do exactly what you're trying to do. note however, you need to use the initial start and goal directions - not the current directions - or you'll get varying speed changes.

此外,它通常用于点),我会使用Vector3.Dot()这是一种像距离的方向。它也应该比Distance()快。

Also, instead of using distance (which is usually used for points), i would use Vector3.Dot() which is kind of like distance for directions. it should also be faster than Distance().

希望这有帮助。

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

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