PDA

View Full Version : beginner trying to wrap head around a bugged call to GLtranslate



WillJBD
08-04-2012, 11:38 PM
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.



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.

uwi2k2
08-05-2012, 12:32 AM
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

WillJBD
08-05-2012, 12:45 AM
it was my order of operations, needed to look like this


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().BottomLeftCorn er.x),"Y=");
mRect.Draw(GL10.GL_TRIANGLES, gl);


if (l2rArrow.hasCollided) {
//run collision code for detecting a hit and ending turn
}
}

WillJBD
08-05-2012, 12:47 AM
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.

uwi2k2
08-05-2012, 12:55 AM
ok,

sorry i missunderstood this one ...

uwi