• Create Account

# space wars

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

19 replies to this topic

### #1phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 28 September 2013 - 10:15 PM

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.

### #2SillyCow  Members   -  Reputation: 1284

Like
2Likes
Like

Posted 28 September 2013 - 11:26 PM

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, 28 September 2013 - 11:27 PM.

My browser game: Vitrage - A game of stained glass

My android games : Enemies of the Crown &  Killer Bees

### #3Trienco  Crossbones+   -  Reputation: 2551

Like
1Likes
Like

Posted 29 September 2013 - 12:54 AM

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, 29 September 2013 - 12:58 AM.

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

### #4phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 29 September 2013 - 11:54 AM

Switch the order of your rotate and translate

I have tried this but it still does not work

### #5dejaime  Crossbones+   -  Reputation: 4157

Like
0Likes
Like

Posted 29 September 2013 - 12:10 PM

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?

### #6phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 29 September 2013 - 03:23 PM

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

### #7dejaime  Crossbones+   -  Reputation: 4157

Like
0Likes
Like

Posted 30 September 2013 - 07:25 AM

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?

### #8BeerNutts  Crossbones+   -  Reputation: 4251

Like
0Likes
Like

Posted 30 September 2013 - 02:03 PM

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)

### #9phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 30 September 2013 - 03:52 PM

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

### #10phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 01 October 2013 - 08:27 PM

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

### #11phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 15 October 2013 - 06:16 PM

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.

### #12dejaime  Crossbones+   -  Reputation: 4157

Like
0Likes
Like

Posted 15 October 2013 - 07:08 PM

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?

### #13phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 15 October 2013 - 07:15 PM

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.

### #14King of Men  Members   -  Reputation: 394

Like
0Likes
Like

Posted 16 October 2013 - 07:47 AM

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.

To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.

### #15phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 16 October 2013 - 02:22 PM

here is a screen shot.

### #16ankhd  Members   -  Reputation: 2261

Like
0Likes
Like

Posted 17 October 2013 - 04:36 AM

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

### #17phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 17 October 2013 - 09:15 PM

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.

### #18EddieV223  Members   -  Reputation: 1827

Like
0Likes
Like

Posted 17 October 2013 - 11:15 PM

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.

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

// Easy to learn 2D Game Library c++

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

// SFML 2 book

### #19phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 18 October 2013 - 06:17 PM

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

### #20phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 16 November 2013 - 02:44 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS