- Home /
Working out a reversed magnitude of a vector without affecting the angle
Ok, this may be basic vector maths, so apologies. Then again, this may also be impossible. I hope to be informed.
Basically, I'm trying to add together a number of vectors (they represent the positions of enemies), to create one big vector (a threat vector I'm going to use in the AI) ... that bit would be simple, something like:
foreach (GameObject enemy in enemies) {
threatVector += (transform.position - enemy.transform.position)
}
Apart from the issue that I want the smaller the magnitude (the closer the enemy) to create a larger vector.
As such I want to create some form of reversed magnitude, i.e. 50 - magnitude, but keep the overall angle the same.
Confused. Thanks for any pointers.
Answer by efergan · Jul 29, 2012 at 11:05 PM
I know I may be answering my own question here, but could I reduce the vector using something like threat.Normalize() and then apply that to a new magnitude, 50 - orignalMagnitude?
Yes you can ;)
I've converted it into an actual answer ;)
The normalized vector will represent your direction information and the inversed magnitudes your new magnitude.
Thank you all as always. (Not sure if I should stick this here, may be classed as another question but...) How do I actually set a magnitude? Is it a matter of multiplying all the components, or a separate function of some type?
Vector3.magnitude ...can be found in the script reference.
Read only though, right? Anyhow... I should test before I type, of course a normalised vector multiplies up..
When the vector is normalized it has a length of 1.0f. You only need to multiply the vector with your desired length ;)
threat.Normalize(); // now it has a lenght (magnitude) of 1.0f
threat *= 5.0f; // still the same direction but a magnitude of 5.0f;
Answer by Piflik · Jul 29, 2012 at 11:21 PM
You could multiply each vector by 1/length². This would give you a hyperbolic dependence of the distance, the closer, the higher (infinity at zero, which means error, so you would have to test if the distance is zero ;)).
Ehm, the multiplicative inverse doesn't really represent a linear relationship. Also length² makes it even worse ;)
It of course depends on what you need, but usually a linear relation is more predictable.
Yeah, I meant hyperbolic...and while 1/length² is inverse square, length/length² equals 1/length, which is just inverse ;)