Jump to content
  • Advertisement
Sign in to follow this  
deavik

OpenGL rotation in opengl

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello! I am trying to make a 2d car simulation game using opengl for a start. The texture is a textured GL_QUAD which is larger than the viewport. My idea is to make the track rotate when someone turns the car (which is in the center of the screen). So, obviously the track would have to rotate about that point as well. But when I move the image around (moving the car forward), the GL_QUAD then rotates about IT's center, which is not at the center of the screen anymore. My drawing code:
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
	glLoadIdentity();  // Reset The Current Modelview Matrix
    
    glTranslatef(0.0f, 0.0f, -30.0f);
    // zrot is the angle by which the car has turned
    glRotatef(zrot,0.0f,0.0f,1.0f);
    
    glBindTexture(GL_TEXTURE_2D, texture[0]); // bind the texture
    
	// the track
    glBegin(GL_QUADS);
		glNormal3f(0.0f, 0.0f, 1.0f);
		glTexCoord2f(0.0f, 1.0f); 
        glVertex3f(-20.0f+y*sine(zrot), -15.0f+y*cosine(zrot),  0.0f);
        // y is the forward movement of the car	
		glTexCoord2f(1.0f, 1.0f); 
        glVertex3f( 20.0f+y*sine(zrot), -15.0f+y*cosine(zrot),  0.0f);
		glTexCoord2f(1.0f, 0.0f); 
        glVertex3f( 20.0f+y*sine(zrot),  15.0f+y*cosine(zrot),  0.0f);
		glTexCoord2f(0.0f, 0.0f); 
        glVertex3f(-20.0f+y*sine(zrot),  15.0f+y*cosine(zrot),  0.0f);
	glEnd();


cosine() and sine() I have defined to work with degree angles, so don't worry about that. You will notice I am moving the image sideways on my modelview matrix (no glTranslatef()), so that the origin is always at the center of my screen. However, the problem remains as I described. I would be glad if you could help, maybe I am misunderstanding the glRotatef function? Thanks ...

Share this post


Link to post
Share on other sites
Advertisement
Hi,
If i understand correctlly you're trying to rotate your quad by changing its four vertices (the sine/cosine stuff), but i can't understand why? Just put glTranslatef(x_car, y_car, 0) after your glRotatef(zrot,..) function and draw the quad without the cosnie/sine. This should palce the centre of rotation in the position of your car and then rotate it by your angle..

The way you tried it is not very smart, because your sine and cosine function are slower than the openGL implematation of glRotatef().

Hope this helps a little :)
Y.

Share this post


Link to post
Share on other sites
something else you could try is
glMatrixMode(GL_TEXTURE);
glRotatef(...);
glMatrixMode(GL_MODELVIEW);
...draw quad
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);

Share this post


Link to post
Share on other sites
Hi yanco! Thanks for the post. I wasn't using sine/cosine to rotate, but as you said, a glTranslatef after rotation did what I wanted. But I have a new problem (less severe!). First the revised code:

glTranslatef(0.0f, 0.0f, z);
glRotatef(zrot,0.0f,0.0f,1.0f);
glTranslatef(0.0f, y, 0.0f);

glBindTexture(GL_TEXTURE_2D, texture[0]); // bind the texture

// the track
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-20.0f, -15.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 20.0f, -15.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 20.0f, 15.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-20.0f, 15.0f, 0.0f);
glEnd();


After any rotation, further forward movement moves the car along the quad's y-axis, which is at an angle to the screen. I want it to move along the y-axis of the screen. Anyone got any suggestions?

Share this post


Link to post
Share on other sites
Hi it's me again! To adam17: I tried your way, but it's doing very strange things. The textures look like they're lined up on after the other, and the rotation is occuring about some point out of the top left of my viewport. Maybe I messed up what you interpreted?

Share this post


Link to post
Share on other sites
Try to model the simulation first.

For example, develop a Car entity that has position and rotation. In addition, you will of course need a world to drive in. During every update tick, get user input and update the cars position and rotation in the world.

Then concentrate on rendering a view of this model using OenGL. Adam12's suggestion of modifying the texture coordinates for a static Quad on the creen will give you the effect you want.

This will provide a loose coupling between your simulation and the actual rendering of it to the screen.

Hope this helps....

Share this post


Link to post
Share on other sites
Thanks guys. I stuck at it and got what I wanted with a little trigonometry, in the end! Maybe it is a little slow, so I'll look into rotation the texture coords and stuff later - thanks for your advice. Cheers!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!