• ### Popular Now

• 13
• 27
• 9
• 9
• 20

#### Archived

This topic is now archived and is closed to further replies.

# Problem with rotation and vectors

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

## Recommended Posts

Okay, I am going to have a hard time explaining what I mean with this problem. First of all I have a 'spaceship' that moves around the screen when the player his the arrow keys. Simple enough. Now I want the spaceship to tilt about 15 degrees in the direction that it is moving in. With the glrotatef() function you have to specify the number of degrees and the vector that you want it to rotate around. Now I am not exactly sure how to specify the vector that I want to make it rotate around because the position of the spaceship is always changing. Right now I am trying something like this:

glRotatef(15.0f, Spaceship1->GetXLocation(), Spaceship1->GetYLocation(), 0.0f);

Now I know that doesn't work, how could i make it work? Are there any other functions for rotating models other than glrotatef? Is it even possible to create a vector that goes up and down but is to the left on the -X axis? edit; also what is the flag for culling the inside face so that you must draw your verticies counter clockwise and the inside faces will not be drawn edit2; I have yet another question, what is the best way to draw a polygon (in this case a 3d triangle). Should I draw two triangles (the front and back) then draw four more triangles which will join and look like rectangles that will make up the sides, and then draw the bottom, or would it be easier/better to just do glBegin(GL_POLYGON) and draw the entire polygon vertex after vertex? [edited by - Shadow1234567890 on September 16, 2002 9:38:19 AM] [edited by - Shadow1234567890 on September 16, 2002 9:53:28 AM]

##### Share on other sites
I believe:
That if you transalte your ship to a position and then rotate it, it will be rotated around the last translated position. And then you just choose which axis you wish to rotate it wround.

glFrontFace(GL_CCW)

The best way to draw a trinagle is with GL_TRIANGLES.
But you prpbably mean a pyramid.
Well, a triangle fan with four triangles and a quad?

##### Share on other sites
Hi,
Regarding the tilting in the direction of travel. Here is an idea...maybe not the best but I believe it will work.

if(direction==move_x_pos)
if(direction==move_x_neg)
glRotatef(-15,0,0,1);
if(direction==move_z_pos)
if(direction==move_z_neg)
glRotatef(-15,1,0,0);

So, if you move to the right along +X axis, you must rotate about the Z axis. Movement along -X axis requires the tilt in opposite direction.

Regards,
ChuckB

##### Share on other sites
I don't think that will make the spaceship rotate about itself, the farther away it gets from the origin the larger the orbit it will make around the Z axis right? I need the spaceship to rotate about itself not to orbit an axis.

Also, the spaceship needs to rotate about a line that goes through the middle of the spaceship from the top to the bottom of the spaceship, not through its width.

edit;
just to add another annoying dimension to my problem, I only want it to be tilted while the appropriate arrow key is being pushed down.

edit2;
When are you supposed to use glPushMatrix/glPopMatrix()? Is it supposed to be used in every glBegin/glEnd() functions?

[edited by - Shadow1234567890 on September 16, 2002 10:55:33 AM]

[edited by - Shadow1234567890 on September 16, 2002 10:56:47 AM]

##### Share on other sites
Read the Redbook chapter 1 - 3 ( == 63 pages). It will answer many of your questions.

Are you aware of that calling ...
glTranslate(...) after glRotate(...) will have a different result then vice versa?

quote:
When are you supposed to use glPushMatrix/glPopMatrix()?

e.g when you are drawing subparts of a mesh (eg. the wheels of a car).

quote:
Is it supposed to be used in every glBegin/glEnd() functions?
It depends.

Read 'Manipulating the Matrix Stacks' in the redbook (arround page 93).

[edited by - Jonus on September 16, 2002 12:38:36 PM]

##### Share on other sites
Sounds good to me. Now that you mention it i recall reading about how the effects of rotating and translating and vice versa differ from each other, and I''m very glad you brought that to my attention. I basically don''t care about the order that these events occur, I just want my spaceship to tilt to the left when it is moving left and tilt right when moving right. I also don''t want it to rotate around any one axis, rather I want it to rotate about the vector that goes up and down through its top and bottom.

##### Share on other sites
Since you want the ship to tilt relative to the thrust of the engines (determined by the player''s keypresses), all you need to do is apply the tilt rotation before any other transformation:

If the key is either left arrow or right arrow, rotate around the Z axis. If the key is either forward arrow or back arrow, rotate around the X axis. This is followed by the rotation that sets the ship''s orientation and ends with the translation to the ship''s current position in space.

##### Share on other sites
Okay cool. So the ship is always drawn at the origin, and then all of those transformations you mentioned take place so it looks like it is being moved around the screen?

##### Share on other sites
By the way, when I say that the tilt rotation is applied before any other what I really mean is that it''s specified last in the program (so that it''s the first to operate on the ship''s vertices).

##### Share on other sites
The ship is drawn at the origin, then it is tilted, then it is oriented and finally it is placed wherever you want it in space. The order in pseudocode looks like:

1. Translate ship to final position in space.
2. Rotate ship to set orientation.
3. Rotate ship to tilt in desired direction.
4. Draw the ship.

There are two ways to think about any given list of transformations: in terms of world coordinates (a fixed, global coordinate system) or in terms of local coordinates (a coordinate system that belongs to a specific object and moves the same way that the object moves).

If you look at the transformations in the order they appear in the code (1, 2, 3 above), think in terms of local coordinates. If you look at transformations in the order they''re applied to the transformed vertices (3, 2, 1 above), think in terms of world coordinates.