视差XY和旋转 - 平铺计算

Paw*_*weł 12 java math android opengl-es andengine

我有这个代码用于绘制我的视差背景

pGLState.pushModelViewGLMatrix();
final float cameraWidth = pCamera.getWidth();
final float cameraHeight = pCamera.getHeight();
final float shapeWidthScaled = this.mShape.getWidthScaled();
final float shapeHeightScaled = this.mShape.getHeightScaled();

//reposition

float baseOffsetX = (pParallaxValueX * this.mParallaxFactorX);
if (this.mRepeatX) {
    baseOffsetX = baseOffsetX % shapeWidthScaled;
    while(baseOffsetX > 0) {
            baseOffsetX -= shapeWidthScaled;
    }
}

float baseOffsetY = (pParallaxValueY * this.mParallaxFactorY);
if (this.mRepeatY) {
    baseOffsetY = baseOffsetY % shapeHeightScaled;
    while(baseOffsetY > 0) {
        baseOffsetY -= shapeHeightScaled;
    }                              
}

//draw

pGLState.translateModelViewGLMatrixf(baseOffsetX, baseOffsetY, 0);
float currentMaxX = baseOffsetX;
float currentMaxY = baseOffsetY;
do {     

    //rows     

    this.mShape.onDraw(pGLState, pCamera);
    if (this.mRepeatY) {
        currentMaxY = baseOffsetY;   

        //columns  

        do {       
            pGLState.translateModelViewGLMatrixf(0, shapeHeightScaled, 0);
            currentMaxY += shapeHeightScaled;                                              
            this.mShape.onDraw(pGLState, pCamera);
        } while(currentMaxY < cameraHeight);      

        //end columns

        pGLState.translateModelViewGLMatrixf(0, -currentMaxY + baseOffsetY, 0);                                    
    }

pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
currentMaxX += shapeWidthScaled;
} while (this.mRepeatX && currentMaxX < cameraWidth); 

//end rows

pGLState.popModelViewGLMatrix();
Run Code Online (Sandbox Code Playgroud)

当相机不旋转时,一切都很好.

当它旋转时,我认为tile(this.mShape)应该再绘制四次(顶部,底部,左侧和右侧),因此角落中没有空白空间可见.例如,当旋转是45度,但我无法弄清楚如何做到这一点.

WIl*_*JBD 4

从解释看来,您有一组 2x2 的图块,并且您想要旋转它们。但是当你这样做的时候,角落里有缝隙吗?所以不要这样做

    [][]
    [][]
Run Code Online (Sandbox Code Playgroud)

2x2 瓷砖套装可以做到这一点

    [][][]
    [][][]
    [][][]
Run Code Online (Sandbox Code Playgroud)

3x3 瓷砖组并将其置于中心瓷砖的中心,然后在其周围填充。

如果重要的是你有一个 4 个瓷砖图案,公共角位于中心,那么你必须这样做

    [][][][]
    [][][][]
    [][][][]
    [][][][]
Run Code Online (Sandbox Code Playgroud)

4x4 瓷砖套装。基本上只是围绕你的 2x2 构建。现在,当您旋转背景时,角落里将不再有间隙。

剩下的只是数学。

在 opengl 中,你正在旋转世界,而不是物体。所以想像这样你正在旋转 x,y,z 平面

        |
        |
    ---------
        |
        |
Run Code Online (Sandbox Code Playgroud)

     \      /
      \    /
       \  /
        ><
       /  \
      /    \
     /      \
Run Code Online (Sandbox Code Playgroud)

所以现在几何体将旋转到它被绘制的位置加上旋转。因此,如果我有一个角位于 x,y,z (10,0,0) 的正方形,该点仍将位于 (10,0,0),但 X 轴将旋转 45',因此对象将在 XY 平面上以 45' 角显示距离原点 (0,0,0) 10 个 X 单位的距离。

所以这就是以偏移量重新绘制图块。