Math 基于四元数的旋转和轴心位置
考虑到OpenGL中的轴位置,我不知道如何使用四元数执行矩阵旋转。我目前得到的是对象围绕空间中某个点的旋转,而不是我想要的局部轴。 下面是代码[使用Java] 四元数旋转方法:Math 基于四元数的旋转和轴心位置,math,opengl,quaternions,Math,Opengl,Quaternions,考虑到OpenGL中的轴位置,我不知道如何使用四元数执行矩阵旋转。我目前得到的是对象围绕空间中某个点的旋转,而不是我想要的局部轴。 下面是代码[使用Java] 四元数旋转方法: public void rotateTo3(float xr, float yr, float zr) { _rotation.x = xr; _rotation.y = yr; _rotation.z = zr; Quaternion xrotQ = Glm.angleAxis(
public void rotateTo3(float xr, float yr, float zr) {
_rotation.x = xr;
_rotation.y = yr;
_rotation.z = zr;
Quaternion xrotQ = Glm.angleAxis((xr), Vec3.X_AXIS);
Quaternion yrotQ = Glm.angleAxis((yr), Vec3.Y_AXIS);
Quaternion zrotQ = Glm.angleAxis((zr), Vec3.Z_AXIS);
xrotQ = Glm.normalize(xrotQ);
yrotQ = Glm.normalize(yrotQ);
zrotQ = Glm.normalize(zrotQ);
Quaternion acumQuat;
acumQuat = Quaternion.mul(xrotQ, yrotQ);
acumQuat = Quaternion.mul(acumQuat, zrotQ);
Mat4 rotMat = Glm.matCast(acumQuat);
_model = new Mat4(1);
scaleTo(_scaleX, _scaleY, _scaleZ);
_model = Glm.translate(_model, new Vec3(_pivot.x, _pivot.y, 0));
_model =rotMat.mul(_model);//_model.mul(rotMat); //rotMat.mul(_model);
_model = Glm.translate(_model, new Vec3(-_pivot.x, -_pivot.y, 0));
translateTo(_x, _y, _z);
notifyTranformChange();
}
模型矩阵比例法:
公共无效比例TO(浮动x、浮动y、浮动z){
翻译方法:
公共无效转换为(浮动x、浮动y、浮动z){
但我不使用四元数的这种方法效果很好:
public void rotate(Vec3 axis, float angleDegr) {
_rotation.add(axis.scale(angleDegr));
// change to GLM:
Mat4 backTr = new Mat4(1.0f);
backTr = Glm.translate(backTr, new Vec3(_pivot.x, _pivot.y, 0));
backTr = Glm.rotate(backTr, angleDegr, axis);
backTr = Glm.translate(backTr, new Vec3(-_pivot.x, -_pivot.y, 0));
_model =_model.mul(backTr);///backTr.mul(_model);
notifyTranformChange();
}
四元数仅描述旋转。因此,您希望如何仅使用四元数围绕轴心点旋转某个对象 你需要的最小值是一个R3向量和一个四元数。只需一级变换,你就可以先旋转对象,然后将它移动到那里 如果要创建一个矩阵,首先创建定额矩阵,然后添加未更改的换算 如果您只想调用glTranslate和glRotate(或glMultMatrix),那么首先要调用glTranslate,然后再调用glRotate 编辑: 如果不渲染,只想知道每个顶点的位置,请执行以下操作:
Vector3 newVertex = quat.transform(oldVertex) + translation;
在我看来,你已经考虑到了旋转前后的来回平移。为什么最后要调用translateTo 此外,当您旋转时,纯旋转总是指围绕原点旋转。因此,如果您希望围绕轴心点旋转。我建议将轴心点平移到原点,然后旋转,然后再平移回轴心点。因此,我希望您的代码如下所示:
_model = Glm.translate(_model, new Vec3(-_pivot.x, -_pivot.y, 0));
_model =rotMat.mul(_model);//_model.mul(rotMat); //rotMat.mul(_model);
_model = Glm.translate(_model, new Vec3(_pivot.x, _pivot.y, 0));
在没有调用translateTo(x,y,z)的情况下,您是否可以确认旋转部分已经完成了它应该做的工作?您可以通过将
rotMat
与Glm.rotate(新Mat4(1.0f),AngleDeger,axis)进行比较来检查这一点
。对于相同的旋转,它们应该是相同的。我知道什么是四元数。而且我也不使用固定管道:)但第一个建议解决了您的问题,对吧。您可以像我说的那样计算变换矩阵,或者将四元数和平移应用到每个顶点。
Vector3 newVertex = quat.transform(oldVertex) + translation;
_model = Glm.translate(_model, new Vec3(-_pivot.x, -_pivot.y, 0));
_model =rotMat.mul(_model);//_model.mul(rotMat); //rotMat.mul(_model);
_model = Glm.translate(_model, new Vec3(_pivot.x, _pivot.y, 0));