# beginner problem with rotation

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

## Recommended Posts

I'm making a space shooter like game and I've come across a problem.. I need help correctly rendering the spaceship.. I have 4 vectors that describe the position and the rotation of the spaceship: Position(x,y,z) UpVector(x,y,z) RightVector(x,y,z) ViewDir(x,y,z) the vectors are changed correctly when the ship rotates I just don't know how to render it.. I guess I should use glRotatef but I dunno around which vectors and for how much to achieve that the space ship is aligned with the above 4 vectors.. glTranslatef(Position.x, Position.y, Position.z); /* I'm guessing i ought to put some glRotatef sentences here but I don't know how */ glCallList(objektDL); sorry for my bad english, and if the problem wasn't clear enough please let me know.

##### Share on other sites
It looks like you have the right vectors to represent a matrix. I believe it goes right, up, view, position. Append a 0 to the direction vectors and 1 to the position vector, then arrange them in that order and use glMultMatrix.

##### Share on other sites
first of all you don't need all of them. For example if you have up and right vector you can produce the viewing direction vector easily.

And I think that a spaceship can be one vector and rotation parameter. A vector will describe it's position, orientation and velocity. You will only need the orientation parameter to perform the ship's roll along it's vector, but it only affects the model, not the abstract object of the ship.

The algorithm would be like this:

- place the ship in the origin (0,0,0)
- apply any rotations (in correct order!)
- move the object to it's position

you need to rotate the model alond with the object.

##### Share on other sites
Quote:
 Original post by biggoronIt looks like you have the right vectors to represent a matrix. I believe it goes right, up, view, position. Append a 0 to the direction vectors and 1 to the position vector, then arrange them in that order and use glMultMatrix.

Yeah, this. Assuming your right, up and look-at vectors are always normalised, you can just construct a 4x4 matrix out of them. Don't forget OpenGL takes it's matrices in column-major order.

##### Share on other sites
Thanks to everybody for very quick replies!

glMultMatrix worked like a charm..

this is what I did if anyone else should come across a similar problem:
GLfloat m[16];
m[0]=RightVector.x;
m[1]=RightVector.y;
m[2]=RightVector.z;
m[3]=0.0f;
m[4]=UpVector.x;
m[5]=UpVector.y;
m[6]=UpVector.z;
m[7]=0.0f;
m[8]=ViewDir.x;
m[9]=ViewDir.y;
m[10]=ViewDir.z;
m[11]=0.0f;
m[12]=Position.x;
m[13]=Position.y;
m[14]=Position.z;
m[15]=1.0f;
glMultMatrixf(m);
glScalef(scale,scale,scale);
glCallList(objektDL);

##### Share on other sites
Just out of interest why dont you store your vectors in a 4X4 matrix class (usually represented as an array of 16 floats)? That is essentially what your vectors are representing.

This will mean you can use matrix operations, rotate and translate etc which will make your life much easier.

Plus then using glMultMatrix will be alot easier and more efficient than doing your copying to a GLfoat[16] each frame.

##### Share on other sites
Quote:

Just a note: in case the matrix to multiply with is an identity matrix, like in your example, there's no need to perform the multiplication as its result would be the very same matrix 'm'. You can just set it.

##### Share on other sites
@chipmeisterc: didn't really know that i'd need it in that form at first, but I'll change it definately now

@wanmaster: thanks! I figured as much but i didn't know the function was called glloadmatrix, fixed it now, though.
Edit: ok.. as it turns out i do need multmatrix, because if i loaded the identity the camera didn't work because it overwrote the matrix that glulookat created

[Edited by - janezkranjc on May 13, 2008 1:06:45 PM]

##### Share on other sites
Yeah, that could be. If you're not already familiar with the matrix stack, you may want to look at glPushMatrix and glPopMatrix to preserve matrix states.

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 27
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001791
×