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