2
votes

I'm working on sort of a space shooter, where you can rotate your ship arround both x, y and z axis and thus shoot in any direction. To rotate the ship (I'm using openGL in c++), im rotating with 0-360 degrees arround either the x, y or z axis. The camera will always look the same direction (down along the z axis, like "default"). You simply look at the ship rotating.

Example: glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); The above example will rotate the ship -90 degrees arround the x axis. If the ship was pointing up along the positive y axis like in an ordinary space shooter before the roation, the ship would point inn along the negative z axis after the rotation.

What I'm struggeling to figure out, is how to, at any time, be able to calculate the "direction" of the ship, and turn it into a vector3d (x, y, z inside a class called vector3d).

I calculate the position (another vector3d, simply x, y and z) of any moving object by adding current position (x y z) to a velocity that I also store in a vector3d, and multiply it by delta time (time since last frame/update). When I shoot a projectile from the spaceship, I want to have the direction vector of the ship to use it as basis for the velocity of the projectile I'm shooting, so that I can calculate the position of the projectile the same way I calculate position of other objects by simply adding current projectile position to the velocity multiplied by deltatime.

Since I have the direction of the ship on the form of rotation arround x, y and z, I figure it must be posible to somehow turn this into a vector3d that represents the same direction in a vector format (if that makes sence).

1
You start with a Cartesian (x,y,z) vector representing the direction of the ship. As time goes by, you apply various rotations about the x, y, and z axes. What do you want to keep track of? - Beta
I never actually have the xyz vector representing direction of the ship. The ship just starts off in origo, pointing the way the original vertexes paints it. In my case, the vertexes are put together so the ship "sits" on the xz plane, pointing towards negative z. Once I rotate it, I use the glRotatef function, which asks for an angle (0-360) and the axis you want to rotate arround. If you write glRotatef(180, 1.0f, 1.0f, 0.0f); you rotate 180 degrees arround x and y axis, but not z axis. This means I never actually have the xyz direction, only the xyz rotation. - skakri
@skakri: there is no absolute concept of direction, without you saying in which direction something faces at start, you cannot calculate this. imagine you draw not a spaceship but a sphere, what direction is that facing? You cannot know that unless you specify it at some point in time (usually in the initial draw). - KillianDS
The assumption is that when we start and the ship faces down along the z axis, the direction is x = 0; y = 0; z = 1; Then lets say we do glRotatef(50.0f, 1.0f, 0.0f, 1.0f); What will the old direction (0, 0, 1) then have been translated into? I'm fairly sure in my assumption that this can be calculated. I guess you misunderstood the fact that the way the spaceship points is the direction, and that since it starts off in a direction (0,0,1) and then is rotated with an angle, it can be calculated. - skakri
So you do have the xyz vector representing the original direction of the ship, namely (0,0,1). Now as time goes by and you apply various rotations, what do you want to keep track of? Would you prefer to maintain a list of the rotations, in order? Or to recalculate direction (and, what the heck, maybe orientation too) after each rotation? - Beta

1 Answers

2
votes

Take the vector of the direction you're interested, e.g. the z axis of the original unrotated ship. This is a vector like (0,0,1). Now apply the matrix of the rotation to it, as you find it in the glRotate documentation. You can safely ignore the last row and column of this matrix, as you're only rotating about the origin. The result of this matrixĂ—vector multiplication will be the image of the vector under the given rotational matrix.