OpenGL 2D - problem with following player

Problems with Canvas, OpenGL, etc...

OpenGL 2D - problem with following player

Postby peaver » Thu Mar 10, 2011 4:42 pm

Hi, i'm fairly new to Android & OpenGL, and I'm just messing around, trying to learn.

The thing I am trying to do is this Image.

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!
Last edited by peaver on Mon Mar 14, 2011 8:24 am, edited 1 time in total.
peaver
Freshman
Freshman
 
Posts: 3
Joined: Thu Mar 10, 2011 2:48 pm

Top

Re: OpenGL 2D - problem with following player

Postby benngiro » Sat Mar 12, 2011 10:55 am

Hi peaver,
is your problem still unsolved?
If so would you also post the draw method of your Background class.
benngiro
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Mar 11, 2011 10:49 pm

Re: OpenGL 2D - problem with following player

Postby peaver » Mon Mar 14, 2011 8:25 am

Hi, yes, it is still unsolved :)
updated the original post with the background class.
peaver
Freshman
Freshman
 
Posts: 3
Joined: Thu Mar 10, 2011 2:48 pm

Re: OpenGL 2D - problem with following player

Postby ruelblapid » Mon Mar 14, 2011 10:50 am

Hi Peaver,

Have you tried getting the display size of the screen? If you're not familiar here's how to get the height and width of the screen.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Display display = getWindowManager().getDefaultDisplay();
  2. int screenwidth = display.getWidth() / 2;
  3. int screenheight = display.getHeight() / 2;
  4.  
Parsed in 0.050 seconds, using GeSHi 1.0.8.4


Divide it by 2 for you to have the center of the screen then subtract the ((screenheight) - (shipheight / 2)) and ((screenwidth) - (shipwidth / 2)) the result that you've get will be the X and Y of the ship. This is just an Idea hope this will help solve your problem.
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Re: OpenGL 2D - problem with following player

Postby peaver » Mon Mar 14, 2011 3:22 pm

Hi ruelblapid,
I've tried implementing the code you posted, but that just gives me the same numbers. 427, am I missing something? The screenwidth is not changing, and the ship width is not changing :)
peaver
Freshman
Freshman
 
Posts: 3
Joined: Thu Mar 10, 2011 2:48 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 4 guests