The thing I am trying to do is this
.You move the ship to the right by pressing on the right side of the screen, and left by pressing on the left side.
The thing I'm trying to achieve is that the ship should always in the center of the screen. Except for when you are close to the edges of the background(like in the image above, you are at the top left side, and then the camera should stop following the ship, and just be still while the ship moves closer to the edges of the background.
So the ship should only be in the center if you far away from the backgrounds edges.
I cannot figure out how to do this..
This is my rendering code:
- Code: Select all
public void drawFrame(GL10 gl) {
updateScene(gl);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
if(mShip.getX() >= 0 && mShip.getX() <= (mViewWidth/2)) {
// don't translate
} else {
// Move the camera with the ship.
gl.glTranslatef(-mShip.getX(),-mShip.getY(),0);
}
mBackground.draw(gl);
gl.glPushMatrix();
if(mShip.goLeft) {
mShip.mX -= 12.0f;
}
if ( mShip.goRight) {
mShip.mX += 12.0f;
}
mShip.draw(gl);
gl.glPopMatrix();
}
- Code: Select all
public void sizeChanged(GL10 gl, int width, int height) {
mViewWidth = width;
mViewHeight = height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glViewport(0, 0, mViewWidth, mViewHeight);
gl.glOrthof(0.0f, mViewWidth, mViewHeight, 0.0f, -1.0f, 1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
- Code: Select all
@Override
public void surfaceCreated(GL10 gl) {
gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glViewport(0, 0, mViewWidth, mViewHeight);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, mViewWidth, mViewHeight, 0.0f, -1.0f, 1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
mShip.loadGLTexture(gl, mContext, R.drawable.ship);
mBackground.loadGLTexture(gl, mContext, R.drawable.space);
}
and here is the ship.java
and this is my Ship.java
- Code: Select all
public class Ship extends Renderable {
private FloatBuffer vertexBuffer;
private ShortBuffer indexBuffer;
private FloatBuffer textureBuffer;
private int coordinatesPerVertex = 3;
private int numberVertices = 4;
private int numberIndices = 4;
public boolean goLeft = false;
public boolean goRight = false;
private float[] vertices;
private int[] textures = new int[1];
private float texture[] = {
0.0f, 1.0f, // top left
0.0f, 0.0f, // bottom left
1.0f, 1.0f, // top right
1.0f, 0.0f // bottom right
};
public void handleInput(float dx, float dy, int sWidth, int sHeight) {
if(dx > 0 && dx < (sWidth/2)) {
goLeft = true;
goRight = false;
}
if( dx > (sWidth/2) && dx < sWidth) {
goLeft = false;
goRight = true;
}
}
public Ship(int width, int height) {
mWidth = width;
mHeight = height;
setX(64);
setY(64);
vertices = new float[] {
0.0f, mHeight, 0.0f,
0.0f, 0.0f, 0.0f,
mWidth, mHeight, 0.0f,
mWidth, 0.0f, 0.0f
};
ByteBuffer vBuffer = ByteBuffer.allocateDirect(numberVertices * coordinatesPerVertex * 4);
vBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = vBuffer.asFloatBuffer();
ByteBuffer iBuffer = ByteBuffer.allocateDirect(numberIndices * 2);
iBuffer.order(ByteOrder.nativeOrder());
indexBuffer = iBuffer.asShortBuffer();
short[] indices = {0, 1, 2, 3 };
vertexBuffer.put(vertices);
indexBuffer.put(indices);
vertexBuffer.position(0);
indexBuffer.position(0);
vBuffer = ByteBuffer.allocateDirect(texture.length * 4);
vBuffer.order(ByteOrder.nativeOrder());
textureBuffer = vBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
private void update() {
}
public void draw(GL10 gl) {
update();
gl.glTranslatef(getX(),getY(),0.0f);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(coordinatesPerVertex, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
public void loadGLTexture(GL10 gl, Context context, int picture) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
picture);
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
}
Update: Here is the background class.
- Code: Select all
public class Background extends Renderable {
private FloatBuffer vertexBuffer;
private ShortBuffer indexBuffer;
private FloatBuffer textureBuffer;
private int coordinatesPerVertex = 3;
private int numberVertices = 4;
private int numberIndices = 4;
private float[] vertices;
private int[] textures = new int[1];
private float texture[] = {
0.0f, 1.0f, // top left
0.0f, 0.0f, // bottom left
1.0f, 1.0f, // top right
1.0f, 0.0f // bottom right
};
public Background(int width, int height) {
mWidth = width;
mHeight = height;
vertices = new float[] {
0.0f, mHeight, 0.0f,
0.0f, 0.0f, 0.0f,
mWidth, mHeight, 0.0f,
mWidth, 0.0f, 0.0f
};
ByteBuffer vBuffer = ByteBuffer.allocateDirect(numberVertices * coordinatesPerVertex * 4);
vBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = vBuffer.asFloatBuffer();
ByteBuffer iBuffer = ByteBuffer.allocateDirect(numberIndices * 2);
iBuffer.order(ByteOrder.nativeOrder());
indexBuffer = iBuffer.asShortBuffer();
short[] indices = {0, 1, 2, 3 };
vertexBuffer.put(vertices);
indexBuffer.put(indices);
vertexBuffer.position(0);
indexBuffer.position(0);
vBuffer = ByteBuffer.allocateDirect(texture.length * 4);
vBuffer.order(ByteOrder.nativeOrder());
textureBuffer = vBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
private void update() {
}
public void draw(GL10 gl) {
update();
gl.glPushMatrix();
gl.glTranslatef(getX(),getY(),0.0f);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(coordinatesPerVertex, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glPopMatrix();
}
public void loadGLTexture(GL10 gl, Context context, int picture) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
picture);
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
}
Any help at all is appreciated!
I have tried searching for tutorials and documentation...Just can't make it work!
Thanks!



