# space wars

This topic is 1522 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

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 this post

##### 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 this post

##### 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 this post

##### Share on other sites

Switch the order of your rotate and translate

I have tried this but it still does not work

#### Share this post

##### 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 this post

##### Share on other sites

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

#### Share this post

##### 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 this post

##### 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 this post

##### 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 this post

##### Share on other sites

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

#### Share this post

##### 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?

yes  you are correct. I posted the code above.

#### Share this post

##### Share on other sites

As I really don't know OpenGL, I can't be of much help to you.

I can't help but to notice that your code, as posted on the last post, isn't changed. Are you sure the changes were all saved and submitted when you swapped the rotate and translate functions?

Is it viable to draw it on the center, rotate it and then move it?

#### Share this post

##### Share on other sites

Is it viable to draw it on the center, rotate it and then move it?
yes it is.

I can't help but to notice that your code, as posted on the last post, isn't changed. Are you sure the changes were all saved and submitted when you swapped the rotate and translate functions?
yes all the changes have been implemented. I have swapped the rotate and translate functions but no success.

#### Share this post

##### Share on other sites

I suggest youpost some screenshots so it's easier to understand what's going wrong. Go back to your old unswapped code, take a screenshot or two with that, then repeat with the new swapped code.

#### Share this post

##### Share on other sites

here is a screen shot.[attachment=18404:space.png]

#### Share this post

##### Share on other sites

Hello there.

I think you need to find the offset that will place your ship at the centre then rotate and put the offset back.

and like Trienco said.

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".

but you can convert the outside influences to your models space too. I do this to rotate some objects based on a direction(heading vector fromw world coords).

#### Share this post

##### Share on other sites

I think you need to find the offset that will place your ship at the centre then rotate and put the offset back.
do you mean use pushmatrix and  popmatrix.

#### Share this post

##### Share on other sites

I suggest learning modern openGL with glm, using the matrix objects will teach you more about whats really going on, also learning the modern way is a good thing.

#### Share this post

##### Share on other sites

I would like to solve this problem before I go any further.

#### Share this post

##### Share on other sites

well I have not posted in a long time, I have been reading opengl programming guide 8th ed. I am going back to my space wars game. I need some help with the above problem.