PDA

View Full Version : glRotate changes coordinates so that the rectangle draws on a rotated plane



WillJBD
08-05-2012, 10:53 AM
I am running into an issue here. I set up a square projectile with a velocity x an y, and draw the actual square to the location, by setting the vertex positions there like so.


vertices.put(new float[] { x1, y1, 1, 1, 1, 1, 0.0f, 1.0f,
x2, y2, 1, 1, 1, 1, 1.0f, 1.0f,
x3, y3, 1, 1, 1, 1, 1.0f, 0.0f,
x4, y4, 1, 1, 1, 1, 0.0f, 0.0f });
// x y r g b A u|s v|t

where all the x's are locations of corners that i pass in.

I then move this rectangle (l2rArrow) along the x & y axis, and have the screen follow it:


boolean hasTranslated = false;
if (!l2rArrow.isFired) { // if the arrow has not yet been fired we render it thus
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 { // the arrow has been fired so we need to render it like this and have the "camera" follow it

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;}
if (camX < -4800) { camX = -4800;}

gl.glTranslatef(camX, camY,0f);



l2rArrow.Update(deltaTime);
texture.bind(gl, Texture.TEXTURE_L2RARROW);
mRect = new MeshRect(l2rArrow.getMyRect());
mRect.Draw(GL10.GL_TRIANGLES, gl);

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

this works as well. When the projectile is fired we will follow it perfectly. Now though I want the arrow head to point at the angle the arrow is traveling. So I need to rotate the arrow to in mid flight. I tried translating this arrow back to 0,0 rotating it, and the putting it back but when i put the arrow back it draws on the coordinates of a rotated plane.


} else {

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;}
if (camX < -4800) { camX = -4800;}

gl.glTranslatef(camX, camY,0f);

gl.glPushMatrix();

gl.glTranslatef(l2rArrow.GetToOriginX, l2rArrow.GetToOriginY, 0f); // translate to origin 0,0,0
gl.glRotatef((float) l2rArrow.getAngle(), 0f, 0f, 1f); //rotate
gl.glTranslatef(-l2rArrow.GetToOriginX, -l2rArrow.GetToOriginY, 0f);

l2rArrow.Update(deltaTime);
texture.bind(gl, Texture.TEXTURE_L2RARROW);
mRect = new MeshRect(l2rArrow.getMyRect());
mRect.Draw(GL10.GL_TRIANGLES, gl);

gl.glPopMatrix();

if (l2rArrow.hasCollided) {
//run collision code for detecting a hit and ending turn
}
}
I understand this. How do I rotate the arrow, and then move it back into position on a non-rotated plane?

The arrow rests when it comes to Y=0, and as of right now Y=0 runs at the angle of the rotation.