旋转后平移 [英] Translation after rotation

查看:140
本文介绍了旋转后平移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用适用于Android的OpenGL ES 2.0.我正在使用触摸屏平移和旋转模型.我的平移仅在(x,y)平面上,而我的旋转仅绕z轴.想象一下,直接从桌子上的地图向下看,然后移动到地图上的各种坐标,并且能够围绕着您要看的点旋转地图.

I'm using OpenGL ES 2.0 for Android. I'm translating and rotating a model using the touch screen. My translations are only in the (x, y) plane, and my rotation is only about the z-axis. Imagine looking directly down at a map on a table and moving to various coordinates on the map, and being able to rotate the map around the point you are looking at.

问题是,旋转后,我的后续平移将与屏幕上指针的运动更长地匹配,而轴又不同了.

The problem is that after I rotate, my subsequent translations are to longer matched to the motions of the pointer on the screen, the axes are different.

我尝试过的所有方法都会给我两种行为之一,一种等同于:

Everything I've tried gives me one of two behaviors One is equivalent to:

Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, Xposition, Yposition, 0.0f);
Matrix.rotateM(mModelMatrix, 0, rotationAngle, 0.0f, 0.0f, 1.0f);

这使我能够按照预期进行平移(屏幕上的上下移动模型,上下左右移动模型),而不管旋转情况如何.问题是旋转围绕对象的中心,我需要旋转围绕我正在观察的点,这与对象的中心不同.

This allows me to translate as expected (up/down on the screen moves the model up and down, left/right moves model left and right), regardless of rotation. The problem is that the rotation is about the center of the object, and I need the rotation to be about the point that I am looking at, which is different than the center of the object.

我可以得到的另一种行为等同于:

The other behavior I can get is equivalent to:

Matrix.setIdentityM(mModelMatrix, 0);
Matrix.rotateM(mModelMatrix, 0, rotationAngle, 0.0f, 0.0f, 1.0f);
Matrix.translateM(mModelMatrix, 0, Xposition, Yposition, 0.0f);

这给了我想要的旋转角度,总是围绕着我要看的点.问题是旋转后,翻译是错误的.屏幕上的左/右沿旋转轴以不同角度平移对象.

This gives me the rotation that I want, always about the point I am looking at. The problem is that after a rotation, the translations are wrong. Left/right on the screen translates the object at a different angle, along the rotated axes.

我需要某种方式来同时获得两种行为.它需要绕着我正在看的点旋转,沿手指在屏幕上移动的方向平移.

I need some way to get both behaviors at the same time. It needs to rotate about the point I am looking at, and translate in the direction that the finger moves on the screen.

这甚至有可能吗?我基本上是在试图将量子力学与牛顿物理学相协调,注定要失败吗?

Is this even possible? Am I basically trying to reconcile quantum mechanics with Newtonian physics, and doomed to failure?

我不想列出我尝试过的所有技巧,因为我想以全新的视角考虑所有可能性.

I don't want to list all of the tricks that I've tried, because I want to consider all possibilities with a fresh perspective.

我仍然完全陷入困境.

I'm still completely stuck on this.

我有一个从世界坐标开始(0,0,0)的对象.我的视线是从对象的z轴向下看,并且我想将平移限制为x/y平面.我也只想绕z轴旋转对象.旋转中心必须始终是屏幕的中心.

I have an object that starts at (0, 0, 0) in world coordinates. My view is looking down the z-axis at the object, and I want to limit translation to the x/y plane. I also want to rotate the object about the z-axis only. The center of the rotation must always be the center of the screen.

我正在通过触摸屏控制平移,因此无论手指如何旋转,我都需要用与手指移动相同的方式来操作对象.

I am controlling the translation with the touch screen so I need the object to the same way the finger moves, regardless of how it it rotated.

旋转后,所有平移都开始在旋转的坐标系上发生,这意味着对象不会随屏幕上的指针一起移动.我尝试按照休·费舍尔(Hugh Fisher)的建议进行第二次翻译,但是我不知道如何计算第二次翻译.还有另一种方法吗?

As soon as I rotate, then all of my translations start happening on the rotated coordinate system, which means the object does not move with the pointer on the screen. I've tried to do a second translation as Hugh Fisher recommended, but I can't figure out how to calculate the second translation. Is there another way?

推荐答案

这是旧文章,但我发布的解决方案最适合我后代使用.

This is an old post, but I'm posting the solution that worked best for me for posterity.

解决方案是保留一个单独的模型矩阵,该模型矩阵在发生转换时对其进行累积,并在onDrawFrame()方法中将每个转换乘以该矩阵.

The solution was to keep a separate model matrix that accumulates transformations as they occur, and multiply each transformation by this matrix in the onDrawFrame() method.

//Initialize the model matrix for the current transformation
Matrix.setIdentityM(mModelMatrixCurrent, 0);
//Apply the current transformations
Matrix.translateM(mModelMatrixCurrent, 0, cameraX, cameraY, cameraZ);
Matrix.rotateM(mModelMatrixCurrent, 0, mAngle, 0.0f, 0.0f, 1.0f);
//Multiply the accumulated transformations by the current transformations
Matrix.multiplyMM(mTempMatrix, 0, mModelMatrixCurrent, 0, mModelMatrixAccumulated, 0);
System.arraycopy(mTempMatrix, 0, mModelMatrixAccumulated, 0, 16);

然后将累积的矩阵用于定位对象.

Then the accumulated matrix is used to position the object.

这篇关于旋转后平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆