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

Vector reflection

Vector reflection can mean one of two things: a mirror-like reflection or a bounce-like reflection. The following figure shows the different types of reflections:

Figure 2.8: A comparison of the mirror and bounce reflections

Figure 2.8: A comparison of the mirror and bounce reflections

The bounce reflection is more useful and intuitive than the mirror reflection. To make a bounce projection work, project vector A onto vector B. This will yield a vector that points in the opposite direction to the reflection. Negate this projection and subtract it twice from vector A. The following figure demonstrates this:

Figure 2.9: Visualizing a bounce reflection

Figure 2.9: Visualizing a bounce reflection

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

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

    float magBSq = len(b);

    if (magBSq < VEC3_EPSILON) {

        return vec3();

    }

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

    vec3 proj2 = b * (scale * 2);

    return a - proj2;

}

Vector reflection is useful for physics and AI. We won't need to use reflection for animation, but it's good to have the function implemented in case it is needed.

主站蜘蛛池模板: 儋州市| 雷山县| 新宁县| 敖汉旗| 潮州市| 华亭县| 拜泉县| 郁南县| 德令哈市| 兴宁市| 综艺| 固安县| 壶关县| 长垣县| 当阳市| 桑日县| 遵化市| 西峡县| 兴国县| 监利县| 大姚县| 奎屯市| 崇礼县| 太仓市| 安吉县| 阿尔山市| 临漳县| 昭通市| 蛟河市| 乌什县| 庆云县| 曲麻莱县| 华坪县| 蛟河市| 社会| 广水市| 佛冈县| 伊春市| 藁城市| 临武县| 潜山县|