for what can i use vector3.dot in games? and vector3.cross?
a lot
and that isnât trigonometry
trigonometry has to do with the relationships of sides of triangles and their angles, namely the functions that describe those relationships: cosine, sine, tangent, cotangent, secant, cosecant, and all of their inverses.
You may use trig in conjunction with dot products and cross products. But they arenât trig themselves. You also use addition and multiplication with trig, but addition and multiplication isnât trig.
some examples:
Dot Product:
The projection of one vector onto another.
It essentially is how much value/magnitude of one vector is displaced into the direction of the other vector. If the other vector is a unit vector, that value can have very significant meaning.
For example, say you have the tangent of a surface (thatâs trig), and you have the velocity of a mobile entity on that surface, you can determine the amount of velocity is occurring along the surface as opposed to velocity that may be in gravity/jumping/other directions.
The same goes for those other directions. The dot product along your gravitational vector will give you how much velocity is in the direction of gravity. Sure, if gravity is only in the âyâ, you can just take the âyâ component of your vector⌠but what if gravity is in an arbitrary direction? Note, the dot product of a vector V dot <0,1,0> is⌠the y component.
Dot product is also used as a sub-step in calculating many things about vectors. Such as the angle between vectors⌠though youâd just use the built in Vector3.Angle method instead, but dot product is what makes that method work (and also uses trig).
Cross Product:
The cross product returns a vector orthogonal (perpendicular in 3d, yeah 3d gets its own special word for perpendicularity) to the 2 input vectors.
Lets say you have an arbitrary velocity vector, and a current up vector, and youâre looking to know what is the nearest orientation to face in the direction of the velocity while maintaining an up closest to your current up vector (lets assume you donât always stand y-up, like a flight sim). Well if you cross those 2 vector you get the ârightâ vector to this relationship. Then if you cross that right vector with the velocity, you get a new up. This new up is the closest up to your previous up⌠essentially giving you the closest orientation to your current orientation but looking down the velocity.
âŚ
Cross product is used in any situation where you need to know about something being orthogonal/perpendicular to yourself. The âwhenâ thatâs useful can be so many places⌠same with dot product. Itâs such a basic arithmetic operation for vectors that it can be applied to numerous scenarios.
Asking âwhatâ youâd use it for is like asking âwhat do I use addition of floatsâ for. A LOT.
Vector3.Dot can be really useful to compare directions. It is related to trigonometry since it yields the cosine of the angle between two vectors.
Here are some useful practical facts about the dot product.
- If it yields 1, the directions are perfectly aligned.
- If it yields 0, the directions are perpendicular to each other.
- if it yields -1, the directions are perfectly opposite.
Of course, there are in-between values, which gives you some measurement about the alignment of two directions.
The cross product, is a bit more complicated. It does not yield a number, but a third vector. What you need to know, is that resulting vector is always perpendicular to both input vectors, and itâs length is the sine of the angle between the vectors. There are pratical usages of it, for instance:
- Figuring out rotation axis
- Figuring out whether a rotation is clockwise or counter clockwise
- Computing surface normals
- Compute a third direction (where âupâ is pointing given âforwardâ and ârightâ).
- and moreâŚ
If youâre wondering wether itâs worth learning theses mathematical tools for game development, the answer is: yes, definitely!