Public Group

space wars

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

Recommended Posts

I am working a little space wars game. I am able to get my  space ship to move up and down and follow its nose but I cant get it to rotate around its center.

void DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
glColor3f(color,0.0f,0.0f);
glRotatef(rotation,0.0f,0.0f,1.0f);
glTranslatef(position_X,position_Y,position_Z);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f+position_Y,0.0f);
glVertex3f(-0.25f,-0.5f+position_Y,0.0f);
glVertex3f(-0.5f,-0.5f+position_Y,0.0f);
glVertex3f(-0.0f,0.5f+position_Y,0.0f);
glVertex3f(0.5f,-0.5f+position_Y,0.0f);
glVertex3f(0.25f,-0.5f+position_Y,0.0f);
glVertex3f(0.0f,-0.25f+position_Y,0.0f);
glEnd();
glPopMatrix();
}


I am doing this because I am reading a book on opengl. I have not got to shaders yet.

Share on other sites

Switch the order of your rotate and translate.

When you want to rotate an object on it's axis, rotation should come first.

However, since openGL transforms are a stack, what happens first is what is closest to the code.

So the order of transforms of opengl is actually resversed.

This is kind of confusing at first, but is really logical when you need heirerchial animations (like a skeleton). It's also required because of the linear-algebra behind openGL.

So what you should be doing is:

void DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
glColor3f(color,0.0f,0.0f);
glTranslatef(position_X,position_Y,position_Z); //move only after the ship is rotated.
glRotatef(rotation,0.0f,0.0f,1.0f); //Now rotation will happen before movement
glBegin(GL_LINE_LOOP);glVertex3f(0.0f,-0.25f+position_Y,0.0f); glVertex3f(-0.25f,-0.5f+position_Y,0.0f); glVertex3f(-0.5f,-0.5f+position_Y,0.0f); glVertex3f(-0.0f,0.5f+position_Y,0.0f);
glVertex3f(0.5f,-0.5f+position_Y,0.0f);
glVertex3f(0.25f,-0.5f+position_Y,0.0f);
glVertex3f(0.0f,-0.25f+position_Y,0.0f);
glEnd();
glPopMatrix();
}
Edited by SillyCow

Share on other sites

So the order of transforms of opengl is actually resversed.

Rather than thinking backwards, I'd prefer to think in terms of object space, which means that everything in OpenGL is perfectly logical and forward. All transformations are based on your current local coordinate system. A rotation will obviously rotate your coordinate system. x might not align with the global x anymore, but it will always be your objects "right". It also means that all rotations will always happen around your local origin, which also causes a lot less headache than trying to rotate an object around itself from a "world perspective".

Unless dealing with "outside influence" on your object, thinking in local coordinates is almost always more convenient and easier to reason about. It also means that nothing is actually happening "backwards".

Edited by Trienco

Share on other sites

Switch the order of your rotate and translate

I have tried this but it still does not work

Share on other sites

What exactly is the problem?

Is it not rotating at all? or is it rotating around a corner or other weird point that is not the center?

Share on other sites

well it is rotating around the center of the screen not around the center of the space ship.

Share on other sites

You mean it is rotating in an arc around the center of the screen?

Could you please post the code where you create the ship?

Share on other sites

If you were using a simple API (like SFML), it'd be pretty obvious:

void DrawShip(sf::Vector2f position, float rotation, sf::Sprite& spaceship)
{
spaceship.SetPosition( position );
spaceship.SetCenter( spaceship.GetSize() / 2.f );
spaceship.SetRotation( rotation);
screen.Draw( spaceship);


For openGl, there's a lot more work to do.

Share on other sites

Could you please post the code where you create the ship?

void DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)

{

glPushMatrix();

glColor3f(color,0.0f,0.0f);

glRotatef(rotation,0.0f,0.0f,1.0f);

glTranslatef(position_X,position_Y,position_Z);

glBegin(GL_LINE_LOOP);

glVertex3f(0.0f,-0.25f,0.0f);

glVertex3f(-0.25f,-0.5f,0.0f);

glVertex3f(-0.5f,-0.5f,0.0f);

glVertex3f(-0.0f,0.5f,0.0f);

glVertex3f(0.5f,-0.5f,0.0f);

glVertex3f(0.25f,-0.5f,0.0f);

glVertex3f(0.0f,-0.25f,0.0f);

glEnd();

glPopMatrix();

}

here is my code

Share on other sites

well I have almost solved the problem, but I need a little more help.

• 13
• 18
• 29
• 11