Sign in to follow this  
Endar

Major problems with translation

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this