# Normalize Vector3 loosing decimal values

**URL:** <https://forum.babylonjs.com/t/normalize-vector3-loosing-decimal-values/13781>\
**Category:** Questions\
**Created:** [August 30, 2020, 1:38am UTC](https://forum.babylonjs.com/t/normalize-vector3-loosing-decimal-values/13781 "2020-08-30T01:38:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [August 30, 2020, 1:38am UTC](https://forum.babylonjs.com/t/normalize-vector3-loosing-decimal-values/13781/1 "2020-08-30T01:38:22Z")

</div>

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

```auto
            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();

```

---

<div class="post-metadata">

**Author:** ![aWeirdo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/aweirdo/32/52_2.png) [@aWeirdo](https://forum.babylonjs.com/u/aWeirdo)\
**Post date:** [August 30, 2020, 7:03am UTC](https://forum.babylonjs.com/t/normalize-vector3-loosing-decimal-values/13781/2 "2020-08-30T07:03:16Z")

</div>

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

 ![vector3normalize](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/3/31adb766770e71f0252c9bf3ffdeac238aebe851.png)

am i missing something? maybe the fault is somewhere else? 🙂

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [August 30, 2020, 7:41am UTC](https://forum.babylonjs.com/t/normalize-vector3-loosing-decimal-values/13781/3 "2020-08-30T07:41:32Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![aWeirdo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/aweirdo/32/52_2.png) [@aWeirdo](https://forum.babylonjs.com/u/aWeirdo)\
**Post date:** [August 30, 2020, 7:59am UTC](https://forum.babylonjs.com/t/normalize-vector3-loosing-decimal-values/13781/4 "2020-08-30T07:59:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [August 30, 2020, 8:47am UTC](https://forum.babylonjs.com/t/normalize-vector3-loosing-decimal-values/13781/5 "2020-08-30T08:47:53Z")

</div>

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…)

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [August 30, 2020, 6:50pm UTC](https://forum.babylonjs.com/t/normalize-vector3-loosing-decimal-values/13781/6 "2020-08-30T18:50:24Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [August 30, 2020, 7:07pm UTC](https://forum.babylonjs.com/t/normalize-vector3-loosing-decimal-values/13781/7 "2020-08-30T19:07:22Z")

</div>

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

```auto
        /** 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));
            }
        }

```
