简体   繁体   中英

Rotating a vector

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM