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

Angle axis

Quaternions are often created using an axis of rotation and an angle. A rotation about an axis by θ can be represented on a sphere as any directed arc whose length is on the plane perpendicular to the rotation axis. Positive angles yield a counterclockwise rotation around the axis.

Create a new file, quat.cpp. Implement the angleAxis function in quat.cpp. Don't forget to add the function declaration to quat.h:

#include "quat.h"

#include <cmath>

quat angleAxis(float angle, const vec3& axis) {

    vec3 norm = normalized(axis);

    float s = sinf(angle * 0.5f);

    return quat(norm.x * s,

                norm.y * s,

                norm.z * s,

                cosf(angle * 0.5f)

    );

}

Why ? A quaternion can track two full rotations, which is 720 degrees. This makes the period of a quaternion 720 degrees. The period of sin/cos is 360 degrees. Dividing θ by 2 maps the range of a quaternion to the range of sin/cos.

In this section, you learned how the angle and axis of a rotation are encoded in

a quaternion. In the next section, you will learn how to build an angle and an axis

for the rotation between two vectors and encode that into a quaternion.

主站蜘蛛池模板: 阿图什市| 江阴市| 读书| 翁源县| 隆子县| 桂林市| 盘山县| 乐山市| 密云县| 牙克石市| 宁波市| 澄迈县| 贵州省| 白水县| 漾濞| 邵武市| 兴山县| 仁化县| 从化市| 贵港市| 陇西县| 昌吉市| 达孜县| 平乐县| 诸城市| 镇赉县| 阜城县| 加查县| 始兴县| 卢湾区| 游戏| 亚东县| 焦作市| 长丰县| 洛浦县| 陆川县| 大石桥市| 洛隆县| 屏南县| 临漳县| 沙湾县|