12

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Paweł
  • 2,144
  • 1
  • 18
  • 25
  • Can you put up an image that explains the problem? I think I know what you mean, but I just want to make sure. – Tim Sep 02 '12 at 22:50

1 Answers1

4

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.

WIllJBD
  • 6,144
  • 3
  • 34
  • 44
  • this is what i want - a 3x3 , z is always 0 because this is for 2d engine. I have problem with math in this i have tried but without luck. Thats why the bounty is activated. – Paweł Sep 04 '12 at 20:30
  • Opengl should do the math for you. If you rotate it on the z-axis, objects will be placed where they belong. You simply have to place them in the right spots. so you need to center the center tile, and then place the surrounding tiles. so place the center tile, the place a tile at the (center tiles x plus width, center tiles y), (centertile x - width, center tile y), (center tile X, center tile y+height),(center tile X, center tile y-height) and so on – WIllJBD Sep 04 '12 at 21:26