Rotating a vector

╄→尐↘猪︶ㄣ 提交于 2019-12-13 21:02:21

问题


I want to do a simple vector rotation.

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.

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

I am performing the actual movement.

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.

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


回答1:


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

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.

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

hope this helps.



来源:https://stackoverflow.com/questions/12796701/rotating-a-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!