This video? The Vector Cross Product - YouTube Damn what a great intro tune!
Anyway, your understanding isnât quite right. Take a look here:
This is taken from the video. It is an example of how to calculate a Ă b
, where a
and b
are vectors in 3D. As you can see after the equals sign, the result is also a 3D vector. This doesnât fit with your description that I quoted, because a magnitude is just a simple number, not a vector.
Anyway, if I were you I wouldnât worry so much about how to compute the cross product. To program games with it, itâs much more important to understand the properties, geometric interpretations and uses of it. The computer is there to compute for us, after all!
You wanted to know how the cross product can be related to angles, and hereâs the key to understanding that:
Here, θ
is the angle between the vectors a
and b
. This equation can be rearranged however you want, e.g. if you already know the vectors and want to determine their angle, you might use this form of the equation instead:
θ=asin( |a à b| / (|a||b|) )
As for how the cross product is used in the code you posted, thatâs hard to figure out just by looking at it. The original programmer didnât do a very good job of making it readable - or maybe they were just so used to thinking about 3D geometry that they thought it looked perfectly clear. Oh well, letâs break it down. Each version will be the same, unless I note otherwise
--Putting it onto more lines
Neck.C0 = Neck.C0:lerp(
NeckOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1, 0),
0.25 --Simplifying last parameter to lerp()
)
--Simplifying last parameter to lerp
Neck.C0 = Neck.C0:lerp(
NeckOriginC0 *
CFrame.Angles( --Putting it onto more lines
-(math.atan(Difference / Distance) * 0.5), --Pitch
(((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1, --Yaw
0 --Roll
),
0.25
)
Okay so now that itâs split onto more lines, we can see that theyâre using the version of CFrame.Angles that takes 3 numbers, where each is the X, Y and Z rotation (or pitch yaw roll). The roll one is easiest because itâs just 0. That means the neck joint never ends up rolling - which in this case would be like tilting your head left/right.
The pitch (looking up/down) calculation looks a lot like the basic tan formula but rearranged:
tan(θ) = o/a â θ=atan(o/a)
If we rearranged the pitch calculation we can see that itâs that but multiplied - or scaled - by a constant:
-0.5 * math.atan(Difference / Distance)
Canât really tell you which angle it is without knowing what Difference
and Distance
are. But it seems to be finding half of the angle from the neck joint to some point in 3D space, which makes sense when constructing an angle with the CFrame.Angles call.
The yaw (twisting your neck to look left or right) has the actual cross product. Letâs simplify it:
((HeadPosition - Point).Unit:Cross(TorsoLookVector)).Y --Yaw
and then see what two vectors itâs operating on:
a = (HeadPosition - Point).Unit
b = TorsoLookVector
A LookVector is always a unit vector (magnitude = 1), and a
is calculated to always be a unit vector as well. This means that |a|=1
and |b|= 1
, so if you look at the first picture formula in this post, it simplifies to
|a à b|=sin(θ)
The LookVector of a characterâs torso is usually going to be horizontal, and (HeadPosition - Point)
will usually be somewhat horizontal if Point
is mouse.Hit.p
- again impossible to know without seeing the rest of the code. But if both vectors are horizontal (and not pointing the same direction), then their cross product will always be vertical. That means the X and Z components will always be 0, and all of the magnitude will be âstored inâ the Y component of the cross product. So all in all itâs like setting the yaw of the neck C0 joint to the sine of the angle that the head should be yawed at to look towards the mouse. Doesnât make as much sense as taking the asin of that whole thing to get the actual angle, but this is my best guess as to what the code is supposed to do.
Anyway, hope this helps in showing how code like that can be broken down into an understanding of what it does in 3D space. Itâs not what you asked for originally (doesnât even touch the arms at all), but if you learned something then I guess itâs ok