Is Vector3.Dot faster than Vector3.Angle?
Hi, I'm currently reading stuff about the purpose of dot product, and from all I can tell, its main use is to determine wether two vectors are parallel or perpendicular, which is nice, but then on the other hand you also have Vector3.Angle returning the angle between two vectors, and it would be quite easy to determine from there if they are perpendicular too.
Also looking at the doc, to calculate the dot product, Unity uses the cosine of the angle, which feels a bit redundant.
So I'm wondering, if people actually use dot product for that purpose at all, is it for performance reasons? Or is there something more clever that you can do with dot products, that apparently no one seems keen to share in any tutorial video on dot products.
Don't know about the performance. But what you're describing about the dot product is just the case for two normalized vectors. If, however you match a vector against a normalized one, you get the amount of the first vector in the direction of the normalized one.
Like the amount of drift when using the dot product with the velocity and any side direction vector of the car.
The dot product is a standard mathematical thing. Formulas use it, people who know 3D trig use it. $$anonymous$$ath textbooks have the best explanations. But it seems like you don't have a strong trigonometry background (many very good game developers don't) and are confusing yourself.
If you really want to learn more math, you could review how cosine and arc-cosine work. Or just do what everyone else who hates trig does and use Angle.
Dot products or "scalar vector products" are used for a myriad of things: vector projections, interceptions, intersections, fast motion or proximity detectors, slope detectors, fast calculations of all sorts you'd otherwise do with trig. So yes, there is something more clever that you can do with dot products, but the topic is so vast I couldn't cover it in this comment alone. Cross products, or true vector products, are as much as important, and the two (along with the addition and subtraction) form the basis for the vector math.
If it helps, one could divide general geometry in three areas, and I'm talking exclusively from the game engineering perspective: 1) trigonometry, 2) vectors, 3) analytic geometry. Trigonometry, even really basic one, is usually quite enough when working with 2D, but mastering all of the fields can turn you into a beast in 3D.
Vectors are essentially very fast, computation-wise, and elegant enough if you know what you're doing. In my experience, they are also the easiest to visualize, however you have to keep in $$anonymous$$d that a single Vector3 can represent quite a few different things, a position, a direction, or a difference (or unpacked distance). I also use barycentric coordinates, which belong to affine geometry and you absolutely need dot products to even begin thinking stepping outside of the XYZ coordinate system.
For example, this is how you might compute an angle (in radians) between two given radial vectors (the assumption is that they share the same origin)
float calcAngle(Vector3 a, Vector3 b) { return $$anonymous$$athf.Atan2(Vector3.Cross(a, b).magnitude, Vector3.Dot(a, b)); }
Because arc length = radius * angle (s = r x theta) which is a definition of a radian. The method above returns the unit arc length when the vectors are normalized. For the general arc length, multiply the result by radius.
Answer by balfire · Apr 25, 2018 at 07:32 PM
No. Unity does not use the cosine to calculate dot product. If you look around the internet you can find decompiled unity source files. There you can see that dot product is calculated as follows: dot(a,b) = a.x*b.x + a.y*b.y + a.z*b.z.
I myself use Dot when i need to find the cosine between two vectors. In fact, Angle is defined through the use of Dot.
The problem, if you want the angle, is there are two more steps. You have to normalize the vectors or know they already are. Then you have to think in reverse cosin. Ex: dot>0.707 ins$$anonymous$$d of angle<45.
When you ask if one thing is faster than the other, they have to be doing the same thing, and these aren't.