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

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.

主站蜘蛛池模板: 襄樊市| 海丰县| 芦溪县| 双城市| 台州市| 靖江市| 沈阳市| 司法| 娱乐| 五莲县| 翁源县| 周至县| 永福县| 古田县| 哈尔滨市| 翁源县| 石泉县| 社会| 黄山市| 五台县| 会东县| 阳曲县| 安溪县| 西吉县| 麻江县| 海宁市| 柳河县| 惠东县| 察隅县| 沧州市| 扶余县| 乌拉特后旗| 台南县| 贺兰县| 防城港市| 临海市| 榆中县| 四会市| 斗六市| 綦江县| 横峰县|