- Home /
Need to normalize vectors?
When I pass vectors into methods such as Vector3.Angle() or Transform.RotateAround(), do I need to normalize them first or do those routines normalize them internally?
I'd like to avoid normalizing if I don't have to, since it's a (relatively) expensive operation and there's no point in doing it twice.
Answer by jonas-echterhoff · Jul 22, 2010 at 05:00 PM
The correct answer is: no, you don't need to normalize your vectors. Both mentioned functions will internally normalize vectors, so doing it yourself will just double the effort.
Thanks, jonas -- that's what I needed to know.
Petroz, I agree with your approach in general (which is why I've been normalizing the vectors myself up until now). However, knowing that I don't have to do that will simplify my code and improve my performance.
As to changing to a different library... I'm not planning on doing that, since Unity is a really good engine for what I'm doing. If I do move to a different engine in the future, much of my code will have to be rewritten anyway since the APIs will be different.
Thanks to both of you for your answers -- much appreciated!
Answer by _Petroz · Jul 22, 2010 at 07:11 AM
No I don't think you need to normalize them but I think you should. If a function will perform an operation requiring normalized data, then you should be passing it normalized data regardless of whether that function has be set up to accept non-normalized data and normalize it internally.
The overhead of normalizing data will be incurred inside the function if you don't do it beforehand, so there is no win there. The cost of determining if a vector is normalized is quite small: (x*x + y*y + z*z) == 1.0 and that will be executed either way so it's a zero sum game performance wise. The reason you should do this is it makes your code correct, it will prepare you for the future when you may use a library which expects normalized data. Most importantly if you encounter a function which does require normalized data, you wont give yourself and your team a headache trying to track down obscure bugs.
Your answer
Follow this Question
Related Questions
Math Optimization: Normalize 2 Answers
Problem Solved [JS] 1 Answer
Find intersecting points between vectors 1 Answer
Unity ML-Agents Game,Unity ML-Agents Space Shooter Game 0 Answers
Maping cube to sphere. Help. 1 Answer