- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 403字
- 2021-06-30 14:45:52
Cross product
When given two input vectors, the cross product returns a third vector that is perpendicular to both input vectors. The length of the cross product equals the area of the parallelogram formed by the two vectors.
The following figure demonstrates what the cross product looks like visually. The input vectors don't have to be 90 degrees apart, but it's easier to visualize them this way:

Figure 2.10: Visualizing the cross product
Finding the cross product involves some matrix math, which will be covered in more depth in the next chapter. For now, you need to create a 3x3 matrix, with the top row being the result vector. The second and third rows should be filled in with the input vectors. The value of each component of the result vector is the minor of that element in the matrix.
What exactly is the minor of an element in a 3x3 matrix? It's the determinant of a smaller, 2x2 sub-matrix. Assuming you want to find the value of the first component, ignore the first row and column, which yields a smaller 2x2 sub-matrix. The following figure shows the smaller sub-matrix for each component:

Figure 2.11: The submatrix for each component
To find the determinant of a 2x2 matrix, you need to cross multiply. Multiply the top-left and bottom-right elements, then subtract the product of the top-right and bottom-left elements. The following figure shows this for each element of the resulting vector:

Figure 2.12: The determinant of each component in the result vector
Implement the cross product in vec3.cpp. Don't forget to add the function declaration to vec3.h:
vec3 cross(const vec3 &l, const vec3 &r) {
return vec3(
l.y * r.z - l.z * r.y,
l.z * r.x - l.x * r.z,
l.x * r.y - l.y * r.x
);
}
The dot product has a relationship to the cosine of the angle between two vectors and the cross product has a relationship to the sine of the angle between the two vectors. The length of the cross product between the two vectors is the product of both vectors, lengths, scaled by the sine of the angle between them:

In the next section, you will learn how to interpolate between vectors using three different techniques.
- ThinkPHP 5實(shí)戰(zhàn)
- 圖解Java數(shù)據(jù)結(jié)構(gòu)與算法(微課視頻版)
- RTC程序設(shè)計(jì):實(shí)時(shí)音視頻權(quán)威指南
- 高級(jí)C/C++編譯技術(shù)(典藏版)
- C語(yǔ)言程序設(shè)計(jì)
- Responsive Web Design by Example
- Linux操作系統(tǒng)基礎(chǔ)案例教程
- Windows內(nèi)核編程
- Node.js:來(lái)一打 C++ 擴(kuò)展
- Statistical Application Development with R and Python(Second Edition)
- 小型編譯器設(shè)計(jì)實(shí)踐
- Domain-Driven Design in PHP
- Mastering Apache Storm
- Scratch從入門到精通
- 絕密原型檔案:看看專業(yè)產(chǎn)品經(jīng)理的原型是什么樣