如果一个模型不在场景的中心点,这时候使用 osg::Matrix::rotate在这里插入代码片旋转的话,这个对象会围绕场景的中心点进行旋转,会转一个大圈,那么怎么做才能让他在任何位置的时候,围绕自己的轴心进行旋转?解决思路如下:
- 先保存物体在世界坐标系下的坐标,即物体在世界坐标系下的中心点坐标
- 再将物体移动到世界坐标系的原点。
- 在世界坐标系的原点旋转好后,再移动回原来的位置,即步骤1中的提到的坐标。
const osg::BoundingSphere& loaded_bs = m_spTrans0->getBound();
osg::Vec3d center = m_spTrans0->getBound().center(); // 先保存物体中心点坐标
float fX = m_spTrans0->getBound().center().x();
float fY = m_spTrans0->getBound().center().y();
float fZ = m_spTrans0->getBound().center().z();
osg::Matrix curMatrix = m_spTrans0->getMatrix();
curMatrix *= osg::Matrix::translate(-center); // 再将物体移动到世界坐标系原点
curMatrix *= osg::Matrix::rotate(osg::inDegrees(1.0), osg::Vec3d(0, 0, 1)); // 旋转
curMatrix *= osg::Matrix::translate(center); // 再移回物体原来的位置
m_spTrans0->setMatrix(curMatrix);