Reputation: 2499
I am looking at this code and do not know what is this magnitude
and normalized
doing and how is this guy using them. In documentation there is only few thing but it doesn't explain much.
Code i am looking at is:
public float minDistance = 1.0f;
public float maxDistance = 4.0f;
public float smooth = 10.0f;
Vector3 dollyDir;
public Vector3 dollyDirAdjusted;
public float distance;
void Awake()
{
dollyDir = transform.localPosition.normalized;
//What has he got with this? Documentation says Returns this vector with
//a magnitude of 1 so if i get it it return's it's length as 1. So what is the point?
distance = transform.localPosition.magnitude;
//Have no idea what he got with this
}
void Update()
{
Vector3 desiredPos = transform.parent.TransformPoint(dollyDir * maxDistance);
//I know what TransfromPoint does but what is he getting with this dollyDir * maxDistance
//Some other code which i understand
}
And while i am here if someone could explain me Mathf.Clamp
Documentation for clamp
is so unclear and how i got it is that i give top and bottom value 1 and 5
and if i set value = 3
it will return 3, but if i set value > 5 it will return 5 and if i set value < 1 it will return 1?
Thanks.
Upvotes: 5
Views: 18759
Reputation: 21
A magnitude is the length of a vector from the origin. It is like using a tape measure from (0,0,0) to wherever your point is. A normalized vector is that same vector, but its length is exactly 1. An important note is that the normalized vector is calculated is by dividing by its magnitude.
Upvotes: 1
Reputation: 5035
Vector3.magnitude returns a float, its a single dimensional value expressing vector length (so it looses directional information)
Vector3.normalized is a bit of an opposite operation - it returns vector direction, guaranteeing that the magnitude of the resulting vector is one, (it preserves the direction information but looses the magnitude information).
The two are often useful for when you want to seperate those two ways of looking at a vector, for example if you need to have an influcence between two bodies where the influence is inversly proportional to the distance between them, you can do
float mag=(targetpos-mypos).magnitude;
if (mag<maxRange)
{
Vector3 dir=(targetpos-mypos).normalized;
Vector3 newVector=dir*(maxRange-mag);
}
this is the most typical use case for me, I am not sure what the author of the original code meant by his as it does seem he could just use a vector difference without using the two extra calls
Mathf.Clamp returns value of value lies between min and max, returns min if its lower and max if is greater.
Another interesting feature of Vector3 is sqrMagnitude which returns a^2+b^2+c^c without computhing the square root. While it adds a bit to the complexity to the code (you need to compare it with squared distance), it saves a relatively expensive root computation; The slightly optimised but a tiny bit harder to read version would look like this
Vectior3 delta=targetpos-mypos;
if (delta.sqrMagnitude<maxRange*maxRange)
{
Vector3 dir=delta.normalized;
Vector3 newVector=dir*(maxRange-delta.magnitude);
}
Upvotes: 9
Reputation: 1062620
A vector can be thought of as a direction and distance (although it is normally expressed as an amount in each axis; for example, +3 on x and -4 on y would have a magnitude of 5). A normalized vector has magnitude 1, so it is just the direction (+0.6,-0.8 for the +3,-4 example); the magnitude tells you the original distance part. You can then multiply a normalized vector (the direction) by any magnitude to express "go this far in that direction". In this case, they seem to be moving 4 units (maxDistance
) in the same direction as the original vector.
Upvotes: 1