转载:OSG::Quat(四元数)用法

转自:http://blog.163.com/dj_zone/blog/static/4908931320098294047820/

osg::Quat

To set the attitude of a node or group of nodes, we use a new object, the osg::Quat. An osg::Quat is used to instantiate a quaternion, which we use to rotate objects. The two functions that we will use to apply rotation transformations to a PAT node are:

void setAttitude(const Quat& quat);
const Quat& getAttitude() const;

We simply construct a new osg::Quat whenever we want to rotate a PAT. The two declarations for an osg::Quat are:

osg::Quat(value_type angle, const Vec3d &axis)
osg::Quat(value_type angle1, const Vec3d& axis1, value_type angle2, const Vec3d& axis2, value_type angle3, const Vec3d& axis3)

The first constructor creates a osg::Quat with a facing of angle in radians around one or more axes defined within the osg::Vec3d. For example:

osg::Quat(1.2, osg::Vec3d(1.0, 0.0, 1.0));

This will create a quaternion that has the properties of rotating 1.2 radians around the x and z axis. For the axis, a value of 1.0 will represent a rotation around that axis, with a value of 0.0 producing no rotation. Also note that because there is but a single angle of rotation, all the axes will be rotated by the same amount if they are flagged for rotation. That is, with this constructor, I cannot rotate 3.0 radians around the x axis and 4.0 radians around the y axis. That requires a more complicated system where there is an individual angle for each axis. For example:

osg::Quat(
    1.0, osg::Vec3d(1.0, 0.0, 0.0),
    2.0, osg::Vec3d(0.0, 1.0, 0.0),
    0.0, osg::Vec3d(0.0, 0.0, 1.0)
);

This constructor is used when the angle of rotation around more than one axis should not be equal. In the above example, we rotate 1.0 radians around the x axis, 2.0 around the y, and 0.0 around the z.

I find it easier to use degrees rather than radians, and OSG helps with degree usage by offering the following function to convert degrees into radians:

osg::DegreesToRadians(<some_degree_value>);

The osg::DegreesToRadians function returns a double. Note that, like translations, rotation angles are offsets from initial values, so unless we keep increasing or changing the rotational angle, the object will not rotate. Setting the attitude of a PAT with the Quat of (5.0, (1.0, 0.0, 0.0)) will NOT rotate the object 5 units per iteration around the x axis. Instead it will rotate the object 5 radians around the x axis on first call, and it will stay that way until the angle is changed.

posted @ 2015-07-05 21:46  cLockey  阅读(1184)  评论(0编辑  收藏  举报