space wars

Started by
18 comments, last by phil67rpg 10 years, 5 months ago

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.

Advertisement

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();
 }

My Oculus Rift Game: RaiderV

My Android VR games: Time-Rider& Dozer Driver

My browser game: Vitrage - A game of stained glass

My android games : Enemies of the Crown & Killer Bees

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

f@dzhttp://festini.device-zero.de


Switch the order of your rotate and translate

I have tried this but it still does not work

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?

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

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?

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.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


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

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

This topic is closed to new replies.

Advertisement