简体   繁体   English

OpenGL,围绕ELEMENT中心向下平移y并在z上旋转(在Android上)

[英]OpenGL, translate down on y and rotate on z around ELEMENT center (on Android)

I have an orthogonal perspective which I initialize like so: 我有一个正交的透视图,我这样初始化:

gl.glViewport(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);   
gl.glMatrixMode(GL10.GL_PROJECTION);    
gl.glLoadIdentity();                    
gl.glOrthof(0,Constants.GAME_AREA_WIDTH, Constants.GAME_AREA_HEIGHT, 0, 1, 10);
gl.glMatrixMode(GL10.GL_MODELVIEW);     
gl.glLoadIdentity();

What I want to do here is have a square start off the top of the screen (at like (x,-100,z) and the that square should descend (on y) while at the same time roate (on z). 我想在这里做的是在屏幕顶部开始一个正方形(如(x,-100,z),并且那个正方形应该下降(在y上),同时旋转(在z上)。

The square's upper-left is what I use as reference for the square's position. 正方形的左上角是我用作正方形位置的参考。

Ok, now, I think I get how to roate it around itself. 好的,现在,我想我知道如何围绕它自己旋转它。 I translate the thing to (-squareSize/2, -squareSize/2,z), rotate it along z, then translate back. 我将事物翻译为(-squareSize / 2,-squareSize / 2,z),将其沿z旋转,然后反向翻译。 And indeed, if I only test this rotation it works ok: 确实,如果我仅测试此旋转,则可以正常工作:

    gl.glLoadIdentity();
    angle = angle + 3;
    if(angle>360) {
        angle = angle - 360;
    }
    gl.glTranslatef(xCurrent+size/2, yCurrent+size/2,0);
    gl.glRotatef(angle, 0, 0, 1);
    gl.glTranslatef(-(xCurrent+size/2), -(yCurrent+size/2),0);
//omitted: enable client state, draw elements, disable client state.

With just this, no matter where I place my square (even small negative values for x and y which only make it partially show on the screen), it will rotate around its center. 这样,无论我将正方形放在哪里(即使x和y的负值很小,也只会使其部分显示在屏幕上),它将围绕其中心旋转。

However I can't figure out how to add the downwards translation on y. 但是我不知道如何在y上添加向下转换。 If I do something like this: 如果我做这样的事情:

    angle = angle + 3;
    if(angle>360) {
        angle = angle - 360;
    }
    gl.glTranslatef(xCurrent+size/2, yCurrent+size/2,0);
    gl.glRotatef(angle, 0, 0, 1);
    gl.glTranslatef(-(xCurrent+size/2), -(yCurrent+size/2),0);

    yCurrent = yCurrent + realSpeed;
    if(yCurrent>Constants.GAME_AREA_HEIGHT+size) {
        yCurrent=-size;
    }
    gl.glTranslatef(0f, yCurrent,0f);

it will only work ok if my square start at (0,0,z) - in which case it will move down and rotate around it's center. 仅当我的正方形从(0,0,z)开始时它才能正常工作-在这种情况下,它将向下移动并围绕其中心旋转。

If however I start it at any positive or negative non 0 value for either x or y, it will still move down, but do a weird spiral motion instead of rotating agains its center. 但是,如果我以x或y的任何正或负非0值启动它,它仍然会向下移动,但会执行怪异的螺旋运动,而不是再次旋转其中心。

The OpenGL matrix stack post multiplies. OpenGL矩阵堆栈后乘。 Which effectively means that you should do the most local transformation last. 这实际上意味着您应该最后进行最局部的转换。

So what you probably want to do is to perform a glTranslatef to the tile's current position, then do the translate/rotate/untranslate sequence to effect your rotation. 因此,您可能想要执行的操作是对图块的当前位置执行glTranslatef ,然后执行平移/旋转/取消平移序列以实现旋转。

Editor's Note: This answer was moved from a question edit, it is written by the Original Poster. 编者注:该答案是从问题编辑中移出的,由原始海报撰写。

First off, what Tommy sais in the answer below is right, I should first code the translation to the new position, and THEN add the lines of code that do translate/rotate/translate. 首先,汤米在下面的答案中说的是正确的,我应该首先将翻译编码到新位置,然后添加进行翻译/旋转/翻译的代码行。

Also, the values I asign to x and y when wanting to translate the center of the square to coordinates (0,0,z) are simply wrong, I misscalculated them. 另外,当我想将正方形的中心转换为坐标(0,0,z)时,我赋予x和y的值就是错误的,我计算错了。 The basic idea here is this. 这里的基本思想是这样。 Let's say a square has the following vertices: 假设一个正方形具有以下顶点:

private static float xLeft = -0.75f;
private static float xRight = +0.25f;
private static float yTop = 2f;
private static float yBottom = 1f;
protected static float vertices[] = { 
        //x      y        z
        xLeft,  yTop,    -5f,   //Top left      triangle1-1     triangle2-1
        xRight, yTop,    -5f,   //Top right     triangle1-2
        xLeft,  yBottom, -5f,   //Bottom left                   triangle2-3
        xRight, yBottom, -5f    //Bottom right  triangle1-3     triangle2-2
}; 

then the translation amounts needed to place this square's center at (0,0,z) are: 那么将该正方形的中心置于(0,0,z)所需的平移量为:

private float xCenterTranslation = (xRight+xLeft)/2f;
private float yCenterTranslation = (yTop+yBottom)/2f;

and the code for translating the square on the y axis while at the same time rotating it along its center is: 用于在y轴上平移正方形并同时沿其中心旋转的代码为:

gl.glTranslatef(0, translationAmountLinearY, 0); //translate on y
//decrement Y translation for next rendering
translationAmountLinearY+=translationDeltaLinearY;

gl.glTranslatef(xCenterTranslation, yCenterTranslation, 0);//translate BACK from center
gl.glRotatef(rotationAmountZDegrees, 0, 0, 1);//rotate
gl.glTranslatef(-xCenterTranslation, -yCenterTranslation, 0);//translate to center
//increment z rotation for next rendering: 
rotationAmountZDegrees+=0.04f;

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

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