Rotating a number of textured objects at once.

Started by
5 comments, last by powdahound 21 years, 10 months ago
I'm working on a space invaders clone and I decided to use opengl just for fun. In the display I have a number of asteroids (with the x,y coordinates stored in a list) and I can draw them to the screen each step just fine. But when I try to rotate them (z axis, so they spin in place) they won't cooperate. Here's what I have so far, tell me what I'm doing wrong (this is in my function for drawing the scene):

	// *** asteroids ***
	glLoadIdentity();		// reset
	glEnable(GL_TEXTURE_2D);	// Enable Texture Mapping
	for (ast = ast_list->begin(); ast != ast_list->end(); ast++)
	{
		glPushMatrix();
			glTranslatef(ast->x(), ast->y(), 0);
			glRotatef(ast_rot, 0.0f, 0.0f, 1.0f);

			if (ast->destroyable())
				glBindTexture(GL_TEXTURE_2D, texture[4]);
			else
				glBindTexture(GL_TEXTURE_2D, texture[5]);

			glBegin(GL_QUADS);
				glTexCoord2d(0.0,0.0); glVertex2d(ast->x()-16, ast->y()-16);
				glTexCoord2d(1.0,0.0); glVertex2d(ast->x()+16, ast->y()-16);
				glTexCoord2d(1.0,1.0); glVertex2d(ast->x()+16, ast->y()+16);
				glTexCoord2d(0.0,1.0); glVertex2d(ast->x()-16, ast->y()+16);
			glEnd();
		glPopMatrix();
	}
	glDisable(GL_TEXTURE_2D);

	ast_rot += 0.1f;
  
Now the problem is that they seem to rotate on a circle with the origin at 0,0 (the upper left of my screen). I don't want them to move on the screen, just rotate in place... Thanks guys. Gamer-Insight.com [edited by - powdahound on June 8, 2002 12:07:55 PM]
Advertisement
do the glTranslatef after the glRotatef, this should work
I forgot to mention I had already tried that. I get the same result just at a different speed.
and add glLoadIdentity() before that
No go. same result
if the game is 2d, try rendering in ortho mode
Since you add ast->x/ast->y to your vertex coordinates AND make a translation, you're actually doing two translations : one before the rotation and one after it.

Try to change :

glTexCoord2d(0.0,0.0); glVertex2d(ast->x()-16, ast->y()-16);
glTexCoord2d(1.0,0.0); glVertex2d(ast->x()+16, ast->y()-16);
glTexCoord2d(1.0,1.0); glVertex2d(ast->x()+16, ast->y()+16);
glTexCoord2d(0.0,1.0); glVertex2d(ast->x()-16, ast->y()+16);

to :

glTexCoord2d(0.0,0.0); glVertex2d(-16, -16);
glTexCoord2d(1.0,0.0); glVertex2d( 16, -16);
glTexCoord2d(1.0,1.0); glVertex2d( 16, 16);
glTexCoord2d(0.0,1.0); glVertex2d(-16, 16);


[edited by - Prosper/LOADED on June 8, 2002 1:08:53 PM]

This topic is closed to new replies.

Advertisement