简体   繁体   English

Android openGL对象同时平移和旋转

[英]Android openGL object translation and rotation in the same time

Using Android opengl I need to move an object from point A to point B and rotate it around its local Z axis in the same time. 使用Android opengl,我需要将对象从A点移动到B点,并同时绕其局部Z轴旋转。 I have been reading tutorials for the past 3 days, everybody gives you bits of informations and hints, but nobody is capable of explaining this from top to bottom for beginners. 在过去的三天里,我一直在阅读教程,每个人都为您提供一些信息和提示,但是没有人能够为初学者从上到下进行解释。

I know how to only translate the object from point A to point B. I also know how to rotate the object in point A around its local axis (translate it to origin, rotate it, translate it back) I DON'T know how to rotate and translate in the same time. 我知道如何仅将对象从A点平移到B点。我也知道如何围绕A点的局部轴旋转A点(将其转换为原点,旋转,平移回去),我不知道如何同时旋转和平移。

I've tried to translate to origin, rotate, translate back, then translate to point B. It doesn't work, and I think I know why (the rotation is messing the object axis, so the translation to point B is incorrect) 我试图平移到原点,旋转,平移回去,然后平移到点B。它不起作用,我想我知道为什么(旋转使对象轴混乱,所以到点B的平移是不正确的)

A(-x1, y1 , -z1)
B(-x1 + deltaX, y1 + deltaY, -z1 + deltaZ)
_gl.glTranslatef(x1, -y1 , z1);         
_gl.glRotatef(degrees, x1, -y1 , z1);                               
_gl.glTranslatef(-x1, y1 , -z1);
_gl.glTranslatef(deltaX, deltaY, deltaZ);

I need to take into consideration the way the rotation is chaning the axes. 我需要考虑旋转改变轴的方式。 Some say I can do that with quaterninons, or with rotation matrixes, etc. But I don't have enough opengl knowledge to use apis to resolve this. 有人说我可以用quaterninons或旋转矩阵等来做到这一点。但是我没有足够的opengl知识来使用api来解决这个问题。

Can someone explain this to me? 谁可以给我解释一下这个? With somecode also? 用somecode也?

Thank you in advance. 先感谢您。

If you have the following code: 如果您具有以下代码:

glTranslate(x, y, z);
glRotatef(angle, 0, 0, 1);
drawObject();

The object will first be rotated around it's local z-axis and then translated with (x, y, z) . 首先将对象绕其本地z轴旋转,然后使用(x, y, z)进行平移。 The transform call that is closest to the draw call is the one that happens first. 最接近绘制调用的转换调用是最先发生的转换调用。

From your code it seems like you actually don't want to rotate the object around it's own origin but some other point, in this case you should do the following: 从您的代码看来,您实际上实际上并不希望围绕对象自身的原点旋转对象,但在其他方面,在这种情况下,您应该执行以下操作:

glTranslate(x, y, z);                         //Transform 4
glTranslate(origin.x, origin.y, origin.z);    //Transfrom 3
glRotatef(angle, 0, 0, 1);                    // Transform 2 
glTranslate(-origin.x, -origin.y, -origin.z); // Transform 1
drawObject();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM