Reputation: 1977
I was trying to create a game the involves the user walking into the screen. I found some idea on how to do this here being lead by this question. But the tutorial is based on GLES1 and I could not adopt it for GLES2.
Here is what the design looks like,
Here is what I tried so far,
I modified GLState
and added
public void frustumProjectionGLMatrixf(final float fov_degrees, float aspect, float zNear, float zFar) {
this.mProjectionGLMatrixStack.glFrustumf(fov_degrees, aspect, zNear, zFar);
}
and I modified GLMatrixStack
and added
public void glFrustumf(float fov_degrees, float aspect, float zNear, float zFar) {
float[] mTemp = new float[2 * GLMatrixStack.GLMATRIX_SIZE];
Matrix.setIdentityM(this.mMatrixStack, this.mMatrixStackOffset);
// Matrix.frustumM(this.mMatrixStack, this.mMatrixStackOffset, 0, this.getWidthRaw(), this.getHeightRaw(), 0, 10, 300);
Matrix.perspectiveM(this.mMatrixStack, this.mMatrixStackOffset, fov_degrees, aspect, zNear, zFar);
Matrix.setLookAtM(this.mMatrixStack, this.mMatrixStackOffset, 0, 120f, zNear * 10, 0, 0, 0f, 0, 1, 0);
Matrix.scaleM(this.mMatrixStack, this.mMatrixStackOffset, 1, -1, 1);
System.arraycopy(this.mMatrixStack, this.mMatrixStackOffset, mTemp, GLMatrixStack.GLMATRIX_SIZE, GLMatrixStack.GLMATRIX_SIZE);
Matrix.multiplyMM(this.mMatrixStack, this.mMatrixStackOffset, mTemp, GLMatrixStack.GLMATRIX_SIZE, mTemp, 0);
}
and I created the camera as follows
this.camera = new Camera(0, 0, WIDTH, HEIGHT) {
@Override
public void onApplySceneMatrix(final GLState pGLState) {
super.onApplySceneMatrix(pGLState);
this.setZClippingPlanes(-3000, 3000);
setFrustum(pGLState);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void setFrustum(GLState pGLState) { // set field of view to 60 degrees
float fov_degrees = 60;
float fov_radians = fov_degrees / 180 * (float) Math.PI;
float aspect = this.getWidth() / (this.getHeight());
float camZ = this.getHeight() / 2 / (float) Math.tan(fov_radians / 2);
pGLState.frustumProjectionGLMatrixf(fov_degrees, aspect, -100, 100);
pGLState.flush();
}
};
But there is nothing drawn on the screen. The scene is totally black. Any if there is no way to adapt perspective in AndEngine GLES2, other tricks to make this work will be appreciated.
Upvotes: 0
Views: 342
Reputation: 364
Honestly i gave up and made my own pseudo 3D projection engine. The problem with this is that it is very slow, as it is all done without GL coding. I had to use Sprite groups and 1 really big mesh(Mesh being the road), which really hurt the layering on the Z axis if you have rolling hills or tunnels etc. You will see in the pictures below.
In the end i had to create 3 sprite groups and 3 meshes. Detect if a sprite was behind the ground/tunnel/etc. From that point backward(into the distance) i would 1: take sprites out of the first group and into the 2nd 2: stop all the first meshes vertices and draw the 2nd mesh from that point onward 3: repeat, if collision occurred behind the 2nd mesh, start the 3rd
hard to explain, took me a while to get it working and it's very quirky with a lot of restrictions. It took me months to get the FPS from 3 back to 60.
Without Layering (Trees show over the ground)
With collision detection layering
I can go into more detail if you would like.
Also if anyone has a better way of doing this, drop in some information for us.
Upvotes: 1