LWJGL: Rotation affects more than it should (Matrix is pushed)

Hi everyone!

I have a problem with rotating models that I can’t solve on my own and I hope you can help me.

I am planning to rotate a moving model so that it points into the direction it is moving to.
I started with the movement into the direction without rotating the model which worked well.
Then I added the possibility to rotate the model into the rendering function and the model is rotating the way it should.
But: When I rotate the model the movement direction is changed back to 0. Since I push the matrix just for the rotation I don’t understand why it is affecting the movement direction of the model.
When I remove the rotation line the movement direction is back to the way it should be.

Here is my rendering function:

public void render(float x, float y, float z, float rY, float scale, Texture t)
	{
		if(t != null)
			t.bind();
		else
			glBindTexture(GL_TEXTURE_2D, 0);
		glPushMatrix();
		
		//This line!
		glRotatef((float)Math.toDegrees(rY), 0, 1, 0);         
		
		glBegin(GL_TRIANGLES);
		
		
		for(Face f:faces)
		{
			Vector3f v1 = verts.get((int) f.verts.x - 1);
			Vector3f n1 = norms.get((int) f.norms.x - 1);
			Vector3f v2 = verts.get((int) f.verts.y - 1);
			Vector3f n2 = norms.get((int) f.norms.y - 1);
			Vector3f v3 = verts.get((int) f.verts.z - 1);
			Vector3f n3 = norms.get((int) f.norms.z - 1);
			glTexCoord2f(0f, 0f);
			glNormal3f(scale * n1.x + x, scale * n1.y + y, scale * n1.z + z);
			glVertex3f(scale * v1.x + x, scale * v1.y + y, scale * v1.z + z);
			glTexCoord2f(1.0f, 0f);
			glNormal3f(scale * n2.x + x, scale * n2.y + y, scale * n2.z + z);
			glVertex3f(scale * v2.x + x, scale * v2.y + y, scale * v2.z + z);
			glTexCoord2f(0.5f, 1.0f);
			glNormal3f(scale * n3.x + x, scale * n3.y + y, scale * n3.z + z);
			glVertex3f(scale * v3.x + x, scale * v3.y + y, scale * v3.z + z);
		}
		
		glEnd();
		glPopMatrix();


	}

Here is the movement:

public void moveBy(float amt)
	{
		this.setPosX(this.getPosX() + amt * (float)Math.cos(Math.toRadians(hasDirection)));
		this.setPosY(this.getPosY() + amt * (float)Math.sin(Math.toRadians(hasDirection)));
	}

I hope you can help me and thank you in advance.

Regards
Smire

I’m quite new to OpenGL, and I’ve done only some 2D stuff with it, but I think your problem may be caused by the fact that you’re rotating the matrix, not the model. For example, let’s say you have a cube of width, height, and length equal to 2. If you translate matrix at 0, 1, 1, then you render the cube at 0, 0, 0 relative to the matrix, and then you call glRotatef(degrees, xAxis, yAxis, zAxis) (in this example), it will rotate the cube at point 0, 0, 0, however, the center of the cube is at 1, 1, 1 relative to the matrix. Then the cube gets rotated differently than it should, and gets offset a little bit. I hope that answers your question. If not, I have to repeat that I don’t know almost anything about OpenGL, especially the 3D stuff.

Mr.Nerd :slight_smile: