使用四元数旋转对象(Rotating an object with quaternions)

编程入门 行业动态 更新时间:2024-10-25 02:29:12
使用四元数旋转对象(Rotating an object with quaternions)

我正在尝试使用平移在3d view旋转对象。 有2个旋转轴:物体y轴和全局固定x轴。 在y轴周围,我简单地使用由方法CATransform3DRotate生成的旋转矩阵旋转,并且在固定的x轴周围使用四元数。 这里有一些代码可以演示这种行为:

UITouch* touch = [touches anyObject]; CGPoint location = [touch locationInView: viewController.view]; CGPoint lastLoc = [touch previousLocationInView: viewController.view]; CGPoint diff = CGPointMake(lastLoc.x - location.x, lastLoc.y - location.y); float rotX = 1 * DEGREES_TO_RADIANS(diff.y / 4.0); float rotY = -1 * DEGREES_TO_RADIANS(diff.x / 4.0); CATransform3D m = glView.modelTransform; if(fabs(diff.y) > fabs(diff.x) * 5.0) { //up-down Vector3D xAxis = {1.0, 0.0, 0.0}; Vector4D quaternion = [self makeQuaternionFromAxis: xAxis andAngle:rotX]; Vector4D currentQuaterion = [self makeQuaternionFromRotationMatrix: m]; quaternion = [self multiplyQuaternion:currentQuaterion buAnother:quaternion]; m = [self makeRo tationMatrixFromQuaternion:quaternion]; m = CATransform3DConcat(m, origin); } else { //right-left Vector3D yAxis = {0.0, 1.0, 0.0}; m = CATransform3DRotate(m, rotY, yAxis.x, yAxis.y,yAxis.z); } glView.modelTransform = m;

我将水平和垂直运动分开。 原点变量是三维视图中物体的原始位置。问题是有时物体会产生偏航角度旋转,并且旋转变得混乱,有时物体本身! 当我在屏幕上混乱地制作平底锅时出现这种情况。 我试图使运动正常化,但这并没有帮助我。 有人能告诉我我做错了什么。

I'm trying to rotate an object in a 3d view using pans. There are 2 axes of rotation: the objects y-axis and a global fixed x-axis. Around the y axis i rotate simply using rotation matrices generated by method CATransform3DRotate and around fixed x-axis i use quaternions. Here i some code wich demonstrates the behaviour:

UITouch* touch = [touches anyObject]; CGPoint location = [touch locationInView: viewController.view]; CGPoint lastLoc = [touch previousLocationInView: viewController.view]; CGPoint diff = CGPointMake(lastLoc.x - location.x, lastLoc.y - location.y); float rotX = 1 * DEGREES_TO_RADIANS(diff.y / 4.0); float rotY = -1 * DEGREES_TO_RADIANS(diff.x / 4.0); CATransform3D m = glView.modelTransform; if(fabs(diff.y) > fabs(diff.x) * 5.0) { //up-down Vector3D xAxis = {1.0, 0.0, 0.0}; Vector4D quaternion = [self makeQuaternionFromAxis: xAxis andAngle:rotX]; Vector4D currentQuaterion = [self makeQuaternionFromRotationMatrix: m]; quaternion = [self multiplyQuaternion:currentQuaterion buAnother:quaternion]; m = [self makeRo tationMatrixFromQuaternion:quaternion]; m = CATransform3DConcat(m, origin); } else { //right-left Vector3D yAxis = {0.0, 1.0, 0.0}; m = CATransform3DRotate(m, rotY, yAxis.x, yAxis.y,yAxis.z); } glView.modelTransform = m;

I separate horizontal and vertical movement. The origin variable is original posiotion of the object in 3d view.The problem is that sometimes the object gets yaw angle rotation and and the rotation gets messed up and sometimes the object itself! This situation appears when i chaotically make pans on the screen. I tried to normalize the movement but this didnt helped me. Can someone tell me what i'm doing wrong.

最满意答案

固定代码如下:

Vector4D quaternion; if( fabs(diff.y) > fabs(diff.x) ) { //up-down Vector3D xAxis = {1.0, 0.0, 0.0}; quaternion = [self makeQuaternionFromAxis: xAxis andAngle:rotX]; quaternion = [self multiplyQuaternion:quaternion buAnother:currentQuaterion]; } else { //right-left Vector3D yAxis = {0.0, 1.0, 0.0}; quaternion = [self makeQuaternionFromRotationMatrix:CATransform3DMakeRotation(rotY, yAxis.x, yAxis.y,yAxis.z); quaternion = [self multiplyQuaternion:quaternion buAnother:currentQuaterion] } currentQuaterion = quaternion; glView.modelTransform = CATransform3DConcat([self makeRotationMatrixFromQuaternion:quaternion],perspective);

currentQuaternion保持当前对象旋转。

我的总结:要乘以'旋转',请始终使用四元数,然后将其转换为旋转矩阵。

Fixed code like this:

Vector4D quaternion; if( fabs(diff.y) > fabs(diff.x) ) { //up-down Vector3D xAxis = {1.0, 0.0, 0.0}; quaternion = [self makeQuaternionFromAxis: xAxis andAngle:rotX]; quaternion = [self multiplyQuaternion:quaternion buAnother:currentQuaterion]; } else { //right-left Vector3D yAxis = {0.0, 1.0, 0.0}; quaternion = [self makeQuaternionFromRotationMatrix:CATransform3DMakeRotation(rotY, yAxis.x, yAxis.y,yAxis.z); quaternion = [self multiplyQuaternion:quaternion buAnother:currentQuaterion] } currentQuaterion = quaternion; glView.modelTransform = CATransform3DConcat([self makeRotationMatrixFromQuaternion:quaternion],perspective);

currentQuaternion holds current object rotation.

My summary: To multiply 'rotations' always use quaternions, and than transform tham into rotation matrix.

更多推荐

本文发布于:2023-04-29 09:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   四元数   Rotating   quaternions   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!