Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallax XY and rotation - tile calculation

I have this code for drawing my parallax background

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();

Everything is working good when camera is not rotated.

When it is rotated, I think tile (this.mShape) should be drawn four more times (top, bottom, left and right) so no empty space in the corner is visible. For example, when rotation is 45 degrees, but I can't figure out how to do this.

like image 979
Paweł Avatar asked Aug 27 '12 22:08

Paweł


1 Answers

It seems from the explanation that you have a 2x2 set of tiles and you would like to rotate them. But when you do there are gaps in the corners? so instead of doing this

    [][]
    [][]

2x2 tile set do this

    [][][]
    [][][]
    [][][]

3x3 tile set and center it on the center tiles, then fill in around it.

If it is important that you have a 4 tile pattern with the common corner in the center then you will have to do this

    [][][][]
    [][][][]
    [][][][]
    [][][][]

4x4 Tile set. Basically just build around your 2x2. Now when you rotate your background there will be no gap in the corners.

The rest id just math.

In opengl you are rotating the world, not the object. So to think of it like this you are rotating the x,y,z planes from

        |
        |
    ---------
        |
        |

too

     \      /
      \    /
       \  /
        ><
       /  \
      /    \
     /      \

so now the geometry will rotated to the position which it was drawn plus rotation. So if i had a sqare with a corner at x,y,z (10,0,0) that point would still be at (10,0,0) but the X axis will have been rotated say 45' so the object will apear 10 X-units distance from the origin of (0,0,0) at a 45' angle on the XY plane.

So all it is is about redrawing your tile, at offsets.

like image 62
WIllJBD Avatar answered Sep 17 '22 15:09

WIllJBD