Sign in to follow this  
janezkranjc

beginner problem with rotation

Recommended Posts

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


Link to post
Share on other sites
biggoron    154
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 this post


Link to post
Share on other sites
wlw_wl    146
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 this post


Link to post
Share on other sites
iNsAn1tY    476
Quote:
Original post by biggoron
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.

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


Link to post
Share on other sites
janezkranjc    122
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:
glLoadIdentity();
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 this post


Link to post
Share on other sites
chipmeisterc    268
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 this post


Link to post
Share on other sites
Wan    1366
Quote:
Original post by janezkranjc
glLoadIdentity();
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);

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


Link to post
Share on other sites
janezkranjc    122
@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 this post


Link to post
Share on other sites
Wan    1366
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this