Normalize Vector3 loosing decimal values

Normalize seem to only be a value of 0 or 1… It looses everything in between.

I am trying to normalize input so Vector.length (magnitude) is not greater then one
when capturing diagonal input for movement.

How do deal with diagonal input values and normalizing that for speed control when going diagonal

            this.playerInputX = BABYLON.SceneManager.GetUserInput(BABYLON.UserInputAxis.Horizontal, this.playerNumber);
            this.playerInputZ = BABYLON.SceneManager.GetUserInput(BABYLON.UserInputAxis.Vertical, this.playerNumber);
            this.playerMouseX = BABYLON.SceneManager.GetUserInput(BABYLON.UserInputAxis.MouseX, this.playerNumber);
            this.playerMouseY = BABYLON.SceneManager.GetUserInput(BABYLON.UserInputAxis.MouseY, this.playerNumber);
            //..
            // Update Player Movement Input
            // ..
            this.inputMovementVector.set(this.playerInputX, 0, this.playerInputZ);
            this.inputMovementVector.normalize();
            this.inputMagnitude = this.inputMovementVector.length();

Hi @MackeyK24
Works fine here, vector3 values are normalized, length is reduced to 1

am i missing something? maybe the fault is somewhere else? :slight_smile:

The issue for was i only get 0 or 1 (or 0.999999999999)…

I never get like .2 or .3 etc… all the gradient from 0 to 1 are gone … so normalize seems to only go 0 to 1

I ended up CLAMPING the magnitude instead of normalizing

this.inputMovementVector.set(this.playerInputX, 0, this.playerInputZ);
this.inputMagnitude = this.inputMovementVector.length();
this.inputMagnitude = BABYLON.Scalar.Clamp(this.inputMagnitude);

But normalize will always scale vector3 length to 1.
(0 if vector values are 0)
This is what the fuction does.

So say you have directional input to move,

x 1 y 0 z 1
Normalized values will move diagonal by 1 length,
So in world space it will move by same distance as
x 1 y 0 z 0

Any vector is made up of two components, a direction and a magnitude (length). . A unit vector is one with a magnitude of 1. A zero vector has a magnitude of 0. A vector of the form (2, 3, 1) contains information about it direction and its magnitude = sqrt(22 + 32 + 12) = 3.74165…

When U = (2, 3, 1).normalize(), U will be in the same direction as (2, 3, 1) however the magnitude of U is 1.

U = (2 / 3.74165…+ 3 / 3.74165… + 1/3.74165…)

(2, 3, 1) = U.scale(3.74165…)

I got it… I always thought the normalize would not really effect the vector unless the values where greater than 1… So I thought if you had a vector with values (for example) 0.2 x 0.2 x 0.2
after normalizing they would still be the same. Obviously that was wrong.

In Unity they have ClampMagnitude for this situation :slight_smile:

FYI… I create these Clamp Magnitude Function Helper Functions… Just in case anyone also need to do this

        /**  Clamps a vector2 magnitude to a max length. */
        public static ClampMagnitudeVector2(vector:BABYLON.Vector2, length:number):BABYLON.Vector2 {
            let result:BABYLON.Vector2 = new BABYLON.Vector2(0,0);
            BABYLON.Utilities.ClampMagnitudeVector2ToRef(vector, length, result);
            return result;
        }
        /**  Clamps a vector2 magnitude to a max length. */
        public static ClampMagnitudeVector2ToRef(vector:BABYLON.Vector2, length:number, result:BABYLON.Vector2):void {
            const sqrMagnitude:number = vector.lengthSquared();
            if (sqrMagnitude > (length * length))
            {
                const mag:number = Math.sqrt(sqrMagnitude);
                // Note: These intermediate variables force the intermediate result to be
                // of float precision. without this, the intermediate result can be of higher
                // precision, which changes behavior.
                const normalized_x:number = vector.x / mag;
                const normalized_y:number = vector.y / mag;
                result.set((normalized_x * length), (normalized_y * length));
            }
        }
        /**  Clamps a vector3 magnitude to a max length. */
        public static ClampMagnitudeVector3(vector:BABYLON.Vector3, length:number):BABYLON.Vector3 {
            let result:BABYLON.Vector3 = new BABYLON.Vector3(0,0,0);
            BABYLON.Utilities.ClampMagnitudeVector3ToRef(vector, length, result);
            return result;
        }
        /**  Clamps a vector3 magnitude to a max length. */
        public static ClampMagnitudeVector3ToRef(vector:BABYLON.Vector3, length:number, result:BABYLON.Vector3):void {
            const sqrMagnitude:number = vector.lengthSquared();
            if (sqrMagnitude > (length * length))
            {
                const mag:number = Math.sqrt(sqrMagnitude);
                // Note: These intermediate variables force the intermediate result to be
                // of float precision. without this, the intermediate result can be of higher
                // precision, which changes behavior.
                const normalized_x:number = vector.x / mag;
                const normalized_y:number = vector.y / mag;
                const normalized_z:number = vector.z / mag;
                result.set((normalized_x * length), (normalized_y * length), (normalized_z * length));
            }
        }