Major problems with translation

Started by
3 comments, last by kburkhart84 19 years ago
I'm having problems with translating objects. At least, I assume its translation, because when I try to rotate a triangle around a point that should be in its center, it rotates about a point below and to the right. But, when this triangle is placed at point 0,0 (x,y) it rotates fine. Entity.zip In flocking.cpp, in the very last function, the DrawGLScene function, there is a #define that you can change to change the triangle you have keyboard control over. I was told that I had to center the triangle around the point (0,0) and then rotate, then translate. This is what I came up with:

void Entity::render()
{
	glPushMatrix();

	CColor temp;
	temp.setToOpenGLColor();	// save OGL current color

	color.setOpenGLColor();		// set OGL color to entity color

		
	glLoadIdentity();		// reset the matrix


	CVector2d tmp = pos;
	pos = CVector2d(0,0);
	setPoints();		// center the triangle around 0,0
	pos = tmp;


	// set rotation on z axis
	glRotatef(-angle, 0.0f, 0.0f, 1.0f);	

	// set translation
	glTranslatef(pos.x, pos.y, -10.0f);

	// render triangle
	glBegin(GL_TRIANGLES);

		// Draw the three points of the triangle
		glVertex3f(pts[0].x, pts[0].y, pts[0].z);
		glVertex3f(pts[1].x, pts[1].y, pts[1].z);
		glVertex3f(pts[2].x, pts[2].y, pts[2].z);

	glEnd();

	
	// restore prev OGL color
	temp.setOpenGLColor();
	
	glPopMatrix();
}

Needless to say, it didn't work, and I was needing some help, because I'm not exactly sure what was meant by "center the triangle around (0,0)", because as you can see, I do that. (or at least set all the points to be around (0,0) ) So, if you can see anything blatently wrong, please, let me know.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
For starters, it's a good idea to place your LoadIdentity() outside of your push-pop routine. This is because when you load the identity matrix you're basically wiping the matrix slate clean. Having said that though, pushMatrix() should initialize it to the identity matrix anyway.

EDIT:

Sorry it's 4 AM here, whether you need to use identity matrix or not depends on what you're doing. In your case though, simply rotating and translating, it shouldn't need a loadIdenity() in this particular push-pop stack.
Quote:Original post by WithoutRemorse
Having said that though, pushMatrix() should initialize it to the identity matrix anyway.


Maybe I'm not reading this correctly but glPushMatrix most definately does not do that. PushMatrix pushes the current matrix down on the stack, duplicating it in the process (ie. the current matrix will then be identical to the following one on the stack).
---Mikael Lax
Quote:Original post by TheSalmon
Quote:Original post by WithoutRemorse
Having said that though, pushMatrix() should initialize it to the identity matrix anyway.


Maybe I'm not reading this correctly but glPushMatrix most definately does not do that. PushMatrix pushes the current matrix down on the stack, duplicating it in the process (ie. the current matrix will then be identical to the following one on the stack).


Whoops, sorry about that. I really shouldn't be posting stuff when it's 4 AM. Even so, moving the glLoadIdentity() to before the matrix push is something to try.
Besides the above comments, I also wanted to sway that if you want to rotate an object about it's translated center, you must translate, THEN rotate. You are doing it backwards. You are causing the object to rotate about the origin, or wherever you have translated before, which is why when you don't translate, it works fine. I had hell with that when I first started learning OpenGL.


This topic is closed to new replies.

Advertisement