Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: beginner trying to wrap head around a bugged call to GLtranslate

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2012
    Posts
    4

    beginner trying to wrap head around a bugged call to GLtranslate

    I have a archer shooting a arrow. When the archer shoots i need the "camera" to follow the arrow, as to keep the arrow dead center while every thing else translates.

    basically follow the arrow.
    My code does this to a point, but the arrow somehow gains on the transformation. I don't know how this happens as I set the transformation to position itself where the arrow is positioned.


    Code :
    public void Render(GL10 gl, float deltaTime) {
    		/************************************************************************** 
    		 *  This is where you draw the screen, called every auto Loop pass
    		 * ************************************************************************/
    		//Log.d("Game", "Render");
    		gl.glViewport(0, 0, Screen.SCREEN_WIDTH, Screen.SCREEN_HEIGHT);
    		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    		gl.glMatrixMode(GL10.GL_PROJECTION);
    		gl.glLoadIdentity();
    		gl.glOrthof(0, Screen.WIDTH_SCALE, 0, Screen.HEIGHT_SCALE, 1, -1);
     
     
    		gl.glMatrixMode(GL10.GL_MODELVIEW);
    		gl.glLoadIdentity();
     
    		gl.glEnable(GL10.GL_BLEND);
    		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
     
     
    		gl.glEnable(GL10.GL_TEXTURE_2D);
    		MeshRect mRect; // this holds all of our rectangles
     
     
    		boolean hasTranslated = false;
    		if (!l2rArrow.isFired) {
    			gl.glPushMatrix(); //save our state
     
    				gl.glTranslatef(l2rArrow.GetToOriginX, l2rArrow.GetToOriginY, 0f); // translate to origin 0,0,0
    				gl.glRotatef(LBow.getAngle(), 0f, 0f, 1f); //rotate
    				gl.glTranslatef(-l2rArrow.GetToOriginX, -l2rArrow.GetToOriginY, 0f); // translate to where we want to draw it
     
    				texture.bind(gl, Texture.TEXTURE_L2RARROW);
    				mRect = new MeshRect(l2rArrow.getMyRect());
    				mRect.Draw(GL10.GL_TRIANGLES, gl);
     
    			gl.glPopMatrix(); //return to saved state
     
    		} else {
    			l2rArrow.Update(deltaTime);
    			texture.bind(gl, Texture.TEXTURE_L2RARROW);
    			mRect = new MeshRect(l2rArrow.getMyRect());
    			mRect.Draw(GL10.GL_TRIANGLES, gl);
     
     
     
    			hasTranslated = true;
    			gl.glPushMatrix();
    			gl.glLoadIdentity();
    				float camX = (float) ((l2rArrow.startX - l2rArrow.PosX));
     
    				float camY = (float) ((l2rArrow.startY - l2rArrow.PosY));
     
    				if (camY > 0) { camY = 0;}
     
    				gl.glTranslatef(camX, camY,0f);
     
     
    			if (l2rArrow.hasCollided) {
    				//run collision code for detecting a hit and ending turn
    			}
    		}
    		/**=======================================================================================================
    		 * Everything between here should be effected by translate | | gl.gltranslatef(x,y,z) x = Xoffset
    		 *=======================================================================================================*/
     
    		// draw translated things		
    		/**=======================================================================================================
    		 * 	gl.glPopMatrix(); were done go back to normal
    		 *=======================================================================================================*/
     
    		if (hasTranslated){
    			gl.glPopMatrix();
    		}
     
     
    	}

    as i said, the screen follows the arrow, but the arrow seems to gain on the edge of the screen until it runs off it.

  2. #2
    Junior Member Regular Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    138
    hi,

    i do not totaly understand what you wanne do with this lines ?:

    gl.glTranslatef(l2rArrow.GetToOriginX, l2rArrow.GetToOriginY, 0f); // translate to origin 0,0,0
    gl.glRotatef(LBow.getAngle(), 0f, 0f, 1f); //rotate
    gl.glTranslatef(-l2rArrow.GetToOriginX, -l2rArrow.GetToOriginY, 0f); // translate to where we want to draw it
    and is LBow->Angle changing somewhere els in the script while arow is flying ?

    cu
    uwi
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    ---------------------------------------------------------
    The OpenGL and VULKAN Podcast ( OpenGL2GO ) : OpenGL & Vulkan Podcast
    ---------------------------------------------------------
    timbling.com - Indie TeamUp

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2012
    Posts
    4

    Figured it out

    it was my order of operations, needed to look like this

    Code :
    boolean hasTranslated = false;
    		if (!l2rArrow.isFired) {
    			gl.glPushMatrix(); //save our state
     
    				gl.glTranslatef(l2rArrow.GetToOriginX, l2rArrow.GetToOriginY, 0f); // translate to origin 0,0,0
    				gl.glRotatef(LBow.getAngle(), 0f, 0f, 1f); //rotate
    				gl.glTranslatef(-l2rArrow.GetToOriginX, -l2rArrow.GetToOriginY, 0f); // translate to where we want to draw it
     
    				texture.bind(gl, Texture.TEXTURE_L2RARROW);
    				mRect = new MeshRect(l2rArrow.getMyRect());
    				mRect.Draw(GL10.GL_TRIANGLES, gl);
     
    			gl.glPopMatrix(); //return to saved state
     
    		} else {
    			l2rArrow.Update(deltaTime);
     
    			hasTranslated = true;
    			gl.glPushMatrix();
    			gl.glLoadIdentity();
    				float camX = (float) ((l2rArrow.startX - l2rArrow.PosX));
    				//Log.d("cam X =" + Float.toString(camX),"Y=");
    				float camY = (float) ((l2rArrow.startY - l2rArrow.PosY));
    				if (camY > 0) { camY = 0;}
    				if (camX < -4800) { camX = -4800;}
     
    				gl.glTranslatef(camX, camY,0f);
     
    				texture.bind(gl, Texture.TEXTURE_L2RARROW);
    				mRect = new MeshRect(l2rArrow.getMyRect());
    				//Log.d("arrow X =" + Float.toString(l2rArrow.getMyRect().BottomLeftCorner.x),"Y=");
    				mRect.Draw(GL10.GL_TRIANGLES, gl);
     
     
    			if (l2rArrow.hasCollided) {
    				//run collision code for detecting a hit and ending turn
    			}
    		}

  4. #4
    Junior Member Newbie
    Join Date
    Aug 2012
    Posts
    4
    Quote Originally Posted by uwi2k2 View Post
    hi,

    i do not totaly understand what you wanne do with this lines ?:



    and is LBow->Angle changing somewhere els in the script while arow is flying ?

    cu
    uwi
    Yes, before the shot is fired the angle and power of the shot is adjusted by the user drawing a line on the screen. so before the shot that is used to rotate the arrow (and the bow elsewhere in the code) to match the angle of the drawn line.

  5. #5
    Junior Member Regular Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    138
    ok,

    sorry i missunderstood this one ...

    uwi
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    ---------------------------------------------------------
    The OpenGL and VULKAN Podcast ( OpenGL2GO ) : OpenGL & Vulkan Podcast
    ---------------------------------------------------------
    timbling.com - Indie TeamUp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •