🔒 Locked
rotation problems
Advertisement
Original Post
I''m yet another newbie on the subject of OpenGL but find it fascinating. I started with Lesson 6, reduced it to a single bitmap on a plane that is used for a space shooter game. Made it smaller and had it load the picture of the space craft. So fine, I have a space ship sitting in the middle of the screen I want it so ''W'' and ''S'' control speed and ''A'' and ''D'' control rotation. It moves up and down fine but when I try to turn the spaceship in a new direction it rotates around the center of the screen instead of the spaceships center point. I''ve tried a few different things. I''ve tried messing with the glTranslatef and glVertex3f functions but nothing seems to work. Any help would be greatly appreciated. Thanks again.
I am a newbie to and I find it fascinating too...so welcome!
I too, maybe making a space shooter soon hehe..
as for your question...
try glTranslate3f(0.0f, 0.0f, 0.0f);
what I think this will do is move to the coordinate system''s origin, which is when you rotate it, then move back to where you were before...
please, someone tell me if I''m wrong cause I have doubts about this code...lol
<- http://www.digitalexplosions.com ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)
as for your question...
try glTranslate3f(0.0f, 0.0f, 0.0f);
what I think this will do is move to the coordinate system''s origin, which is when you rotate it, then move back to where you were before...
please, someone tell me if I''m wrong cause I have doubts about this code...lol
<- http://www.digitalexplosions.com ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)
Thanks for the reply Krisc. I messed around with that for a bit but I couldn''t get it to work. Anyone else with any help??
Thanks anonymous poster for sending me in the right direction. I can''t seem to find a good definition of glPushMatrix() or glPopMatrix(). I mean I vaguely understand the idea of how they work but can''t see any effects when I use them. Could you maybe explain it a little more indepth and how I would use them? Thank you in advance.
try to use glPushMatrix() and glPopMatrix() as little as possible.....it helps with speed
for pushing and popping:
You have a set of vertices that define an object(like your space ship). When you "PUSH" the vertices on the (don't worry bout the terms here).."stack" you are saying to OpenGL to only mess around with the space ship stuff(like location and color and texture stuff)..and not the whole scene. You "POP" the vertices off of the "stack" to let OpenGL know you are done messing with the space ship stuff...and it(OpenGL) can send the stuff down the processor bus and go back to what it was doing before you did the glPushMatrix() and glPopMatrix() stuff. There is a lot more detail to all of this stuff, but that should get you by for now. In case you were wondering the "stack" is the "stuff you send down the bus of your computer at a particular time" (loosely)
[edited by - tylerbingham on December 9, 2002 2:45:19 AM]
You have a set of vertices that define an object(like your space ship). When you "PUSH" the vertices on the (don't worry bout the terms here).."stack" you are saying to OpenGL to only mess around with the space ship stuff(like location and color and texture stuff)..and not the whole scene. You "POP" the vertices off of the "stack" to let OpenGL know you are done messing with the space ship stuff...and it(OpenGL) can send the stuff down the processor bus and go back to what it was doing before you did the glPushMatrix() and glPopMatrix() stuff. There is a lot more detail to all of this stuff, but that should get you by for now. In case you were wondering the "stack" is the "stuff you send down the bus of your computer at a particular time" (loosely)
[edited by - tylerbingham on December 9, 2002 2:45:19 AM]
I''m still having problems but now I think it''s just a logic error and not the whole rotation around the center. Anyone have any help on the logic involved in moving a spaceship around the screen where ''A'' and ''D'' rotate it and ''S'' and ''W'' move it in the direction it''s facing. I have variables xspeed, yspeed, and angle. Here''s my DrawGLScene() function
Maybe just point out where I''m going wrong? Thanks again.
int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glPushMatrix();
glTranslatef(xpos,ypos,-5.0f);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glRotatef(angle,0.0f,0.0f,1.0f); // Rotate On The Z Axis
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f,0.0f); glVertex3f(-0.15f,-0.15f, 1.0f);
glTexCoord2f(1.0f,0.0f); glVertex3f( 0.15f,-0.15f, 1.0f);
glTexCoord2f(1.0f,1.0f); glVertex3f( 0.15f, 0.15f, 1.0f);
glTexCoord2f(0.0f,1.0f); glVertex3f(-0.15f, 0.15f, 1.0f);
glEnd();
if (keys[''A'']){angle+=0.0002f;}
if (keys[''D'']){angle-=0.0002f;}
if (keys[''S'']){yspeed-=0.0002f;}
if (keys[''W'']){yspeed+=0.0002f;}
ypos+=(yspeed - sin(angle));
xpos-=(xspeed + cos(angle));
glPopMatrix();
return TRUE; // Keep Going
}
Maybe just point out where I''m going wrong? Thanks again.
Please ignore everything tylerbingham said, it was all total rubbish.
These should be:
Also note that sin and cos take the angle in radians. If you are expecting angle to be in degrees you need to convert:
sin( angle * (3.14/180.0) );
(that 3.14 should be PI of course)
EDIT:
In fact you don't want two different speed variables. Use a single variable 'speed'.
Also note that glRotatef takes it's angle in degrees so depending on whether you were using 'angle' as degrees or radians you'll have to convert it in the call to glRotatef or the sin and cos calls.
You might need to play around with the signs of the ypos and xpos updates depending on the orientation of you ship bitmap.
You aren't quite using glPush/Pop correctly. You use glPushMatrix to save the current transformation matrix before you change it so you can restore it again later (using glPopMatrix). Now in your code you modify the matrix using glLoadIdentity before saving it using glPushMatrix which is probably not what you want. Even so, in your code it isn't clear whether you have other drawing going on outside this function which would warrant using glPush/Pop.
Having said that it is a good idea to push the MODELVIEW matrix before beginning rendering and restore it at the end so your code is fine in this sense (once you've moved the glLoadIdentity call).
[edited by - JuNC on December 9, 2002 12:52:19 PM]
ypos+=(yspeed - sin(angle));
xpos-=(xspeed + cos(angle));
These should be:
ypos += (yspeed * sin(angle));
xpos += (xspeed * cos(angle));
Also note that sin and cos take the angle in radians. If you are expecting angle to be in degrees you need to convert:
sin( angle * (3.14/180.0) );
(that 3.14 should be PI of course)
EDIT:
In fact you don't want two different speed variables. Use a single variable 'speed'.
if (keys['S']){speed-=0.0002f;}
if (keys['W']){speed+=0.0002f;}
ypos += (speed * sin(angle));
xpos += (speed * cos(angle));
Also note that glRotatef takes it's angle in degrees so depending on whether you were using 'angle' as degrees or radians you'll have to convert it in the call to glRotatef or the sin and cos calls.
You might need to play around with the signs of the ypos and xpos updates depending on the orientation of you ship bitmap.
You aren't quite using glPush/Pop correctly. You use glPushMatrix to save the current transformation matrix before you change it so you can restore it again later (using glPopMatrix). Now in your code you modify the matrix using glLoadIdentity before saving it using glPushMatrix which is probably not what you want. Even so, in your code it isn't clear whether you have other drawing going on outside this function which would warrant using glPush/Pop.
Having said that it is a good idea to push the MODELVIEW matrix before beginning rendering and restore it at the end so your code is fine in this sense (once you've moved the glLoadIdentity call).
[edited by - JuNC on December 9, 2002 12:52:19 PM]
1) glPush/Pop will make absolutely no difference to speed, unless you are calling them 4000 million times per primitive (over-exaggerated for effect). They are used for a specific non-performance critical purpose.
2) Using glPushMatrix does not push ''vertices'' onto any sort of stack, it saves the current transformation matrix onto one of three matrix stacks (depending on your matrix mode). glPopMatrix takes the last pushed matrix and puts it into the current matrix.
3) The matrix stacks do not make OpenGL push anything onto the ''processor bus'' (except possibly with hardware T&L). Data is sent across the buses using immediate mode (glVertex) or vertex buffers.
4) A ''stack'' is a general term for a kind of data structure, true the 80x86 family has hardware provision for a calling stack but this is not where the OpenGL matrix stacks operate.
So essentially what he said was utter gibberish, maybe I shouldn''t have been so blunt about it and explained more fully to begin with, but I don''t want anyone reading that stuff and thinking he was talking definitively (as he sounded).
2) Using glPushMatrix does not push ''vertices'' onto any sort of stack, it saves the current transformation matrix onto one of three matrix stacks (depending on your matrix mode). glPopMatrix takes the last pushed matrix and puts it into the current matrix.
3) The matrix stacks do not make OpenGL push anything onto the ''processor bus'' (except possibly with hardware T&L). Data is sent across the buses using immediate mode (glVertex) or vertex buffers.
4) A ''stack'' is a general term for a kind of data structure, true the 80x86 family has hardware provision for a calling stack but this is not where the OpenGL matrix stacks operate.
So essentially what he said was utter gibberish, maybe I shouldn''t have been so blunt about it and explained more fully to begin with, but I don''t want anyone reading that stuff and thinking he was talking definitively (as he sounded).
Thanks everyone for all the help in explaining everything. After quite a few hours of messing around with this I finally got it to move correctly. Xnin you said you were in the same position. All I can really say to you is read over what has been said here and mess around with it. You''ll get it. If I knew exactly what I did I would tell you. I can tell you however that I did not use glPushMatrix() or glPopMatrix() functions. Thanks again everyone.
Hey JUNC.....that is what I was saying.Push /Pop all you want, but when you get into raytracing and advance graphicd stuff, you want to call functions as little as possible. As far as the Push comment....it is pushing the current matrix onto the "stack" so you can modify it, and you "POP" to go back to the old matrix. What I was saying was not rubish, it was just my attempt to simplify things for someone that might not understand all of the stuff in OpenGL yet.
Unfortunately, when you start ''simplifying'' things by giving wrong information:
you''ll only end up confusing people more in the end.
Please believe me, readability and functionality is far more important than premature optimization by avoiding function calls (especially in advanced graphics algorithms).
I didn''t mean to offend you and I apologise if it came across that way.
quote:
"PUSH" the vertices
you''ll only end up confusing people more in the end.
Please believe me, readability and functionality is far more important than premature optimization by avoiding function calls (especially in advanced graphics algorithms).
I didn''t mean to offend you and I apologise if it came across that way.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement