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

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.

主站蜘蛛池模板: 错那县| 仙居县| 昭平县| 芦溪县| 盈江县| 华容县| 青冈县| 荥经县| 金湖县| 汾西县| 武穴市| 朝阳区| 晋宁县| 澄城县| 方山县| 巫山县| 兴安盟| 平和县| 金阳县| 大悟县| 龙南县| 枝江市| 宁明县| 红河县| 柞水县| 突泉县| 黄骅市| 景谷| 青河县| 玛纳斯县| 黔江区| 武穴市| 浙江省| 佳木斯市| 子长县| 通城县| 清流县| 商都县| 轮台县| 沂源县| 六安市|