What I'm attempting to do is create a game in which a space ship explores a large area of space. I'm randomly generating placement of planets, space stations etc. I want to use the ParallaxBackground (I actually had to find a custom implementation which supports both x and y parallax values).
When a planet is generated I create a sprite, and add a new parallax entity to the background.
My issue is that when I place the sprite, it is not being placed properly (due to the parallax calculation I'm assuming). As the player navigates further away from the origin (0,0 in world space), the offset from the position the object is supposed to appear in the background and the position in which it actually appears gets larger and larger.
Here is the code for the custom drawing of the ParallaxEntity2D:
public void onDraw(final GLState pGLState, final Camera pCamera, final float pParallaxValueX, final float pParallaxValueY) {
pGLState.pushModelViewGLMatrix();
{
final float cameraWidth = pCamera.getWidth();
final float cameraHeight = pCamera.getHeight();
final float shapeWidthScaled = this.mAreaShape.getWidthScaled();
final float shapeHeightScaled = this.mAreaShape.getHeightScaled();
//reposition
float baseOffsetX = (pParallaxValueX * this.mParallaxFactor);
if (this.mRepeatX) {
baseOffsetX = baseOffsetX % shapeWidthScaled;
while(baseOffsetX > 0) {
baseOffsetX -= shapeWidthScaled;
}
}
float baseOffsetY = (pParallaxValueY * this.mParallaxFactor);
if (this.mRepeatY) {
baseOffsetY = baseOffsetY % shapeHeightScaled;
while(baseOffsetY > 0) {
baseOffsetY -= shapeHeightScaled;
}
}
//draw
pGLState.translateModelViewGLMatrixf(baseOffsetX, baseOffsetY, 0);
float currentMaxX = baseOffsetX;
float currentMaxY = baseOffsetY;
do {
this.mAreaShape.onDraw(pGLState, pCamera);
if (this.mRepeatY) {
currentMaxY = baseOffsetY;
do {
pGLState.translateModelViewGLMatrixf(0, shapeHeightScaled, 0);
currentMaxY += shapeHeightScaled;
this.mAreaShape.onDraw(pGLState, pCamera);
} while(currentMaxY < cameraHeight);
pGLState.translateModelViewGLMatrixf(0, -currentMaxY + baseOffsetY, 0);
}
pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
currentMaxX += shapeWidthScaled;
} while (this.mRepeatX && currentMaxX < cameraWidth);
}
pGLState.popModelViewGLMatrix();
}
This is far beyond my ability to understand in terms of the calculation required to figure out where in screen space the sprite needs to be placed for it to show up in the background properly. I've gotten close by hacking together some simple math, but I want this to be perfect.
I guess first I need to understand what the above onDraw code is doing exactly. Maybe there is another approach that would work better for this instance? I explored being able to use the z component on a sprite, but I couldn't get this to work with GLES2 (the example for GLES1 is here).