官术网_书友最值得收藏!

Vector projection and rejection

Projecting vector A onto vector B yields a new vector that has the length of A in the direction of B. A good way to visualize vector projection is to imagine that vector A is casting a shadow onto vector B, as shown:

Figure 2.6: Vector A casting a shadow onto vector B

Figure 2.6: Vector A casting a shadow onto vector B

To calculate the projection of A onto B (projB A), vector A must be broken down into parallel and perpendicular components with respect to vector B. The parallel component is the length of A in the direction of B—this is the projection. The perpendicular component is the parallel component subtracted from A—this is the rejection:

Figure 2.7: Vector projection and rejection showing parallel and perpendicular vectors

Figure 2.7: Vector projection and rejection showing parallel and perpendicular vectors

If the vector that is being projected onto (in this example, vector B) is a normal vector, then finding the length of A in the direction of B is a simple dot product between A and B. However, if neither input vector is normalized, the dot product needs to be divided by the length of vector B (the vector being projected onto).

Now that the parallel component of A with respect to B is known, vector B can be scaled by this component. Again, if B wasn't of unit length, the result will need to be divided by the length of vector B.

Rejection is the opposite of projection. To find the rejection of A onto B, subtract the projection of A onto B from vector A:

  1. Implement the project function in vec3.cpp. Don't forget to add the function declaration to vec3.h:

    vec3 project(const vec3 &a, const vec3 &b) {

        float magBSq = len(b);

        if (magBSq < VEC3_EPSILON) {

            return vec3();

        }

        float scale = dot(a, b) / magBSq;

        return b * scale;

    }

  2. Implement the reject function in vec3.cpp. Don't forget to declare this function in vec3.h:

    vec3 reject(const vec3 &a, const vec3 &b) {

        vec3 projection = project(a, b);

        return a - projection;

    }

Vector projection and rejection are generally used for gameplay programming. It is important that they are implemented in a robust vector library.

主站蜘蛛池模板: 瓮安县| 山阳县| 江西省| 聊城市| 永春县| 尖扎县| 江北区| 惠安县| 调兵山市| 宿迁市| 出国| 富宁县| 平湖市| 贺兰县| 宾阳县| 北辰区| 乐至县| 阳曲县| 双桥区| 蓬莱市| 三台县| 白银市| 张北县| 呼伦贝尔市| 墨竹工卡县| 玉山县| 松潘县| 阿合奇县| 磴口县| 漳平市| 夏津县| 武平县| 双鸭山市| 赤壁市| 镇赉县| 讷河市| 晴隆县| 色达县| 增城市| 舞钢市| 安阳县|