# Vectors with Three Rotations

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

## Recommended Posts

Hi! This came up in the following situation: I'm making a sort of 3D asteroids game, and the spaceship has yaw (y axis rotation), pitch (xz axis rotation), and roll (y axis rotation). It has some guns mounted on each side. They are also higher than the ship's z axis. The tip of the guns are further than the front, so it needs to come forwards too. Where should I put the bullets? The bullets' position should be forward, up, and to either side. So, if the ship has a position at (0,0,0), the bullets' position should be like (-5,1,-2) on the left side and (5,1,-2), as long as the roll is 0 and the pitch is 0 and the sideways turn is 0. Anyone know how to solve this? Thanks, Geometrian

##### Share on other sites
Vector maths to the rescue! I'll assume you understand the maths, its pretty simple once you learn the necessary stuff.

What you need to do is figure out a few vectors. If you get each step working one at a time, (and fix any mistakes I made -- this is all from memory) you should have an implementation of your problem.

ASSUMPTIONS:
Your ship is a position vector P, a direction vector D, and the euler matrix R.
The guns have a vector offset from the center of the craft, O1 and O2. It is assumed that they face in the same direction as the ship.

To rotate the ship, you take the new rotation, and plug it into the rotation matrix R. Then, you multiply direction vector D by this matrix, thus rotating it.
D = D * R

To move the ship forwards, you take the direction vector, and add it to the position vector P.
P = P + D

The offset from the tip of the laser on the left, stored in vector O1, is multiplied by the rotation matrix as well, then added to the position of the ship, to create the world coordinate offsets WO1 and WO2.
WO1 = O1 * R
WO2 = O2 * R

Bullets start at this position, and head in some derivative of the direction vector D.
bullet.start = WO1 + P
bullet.direction = D

Unfortunately this solution suffers from what's known as gimbal lock once you add a third axis, but its the best I can offer.

To draw the ship, you need to convert the euler matrix into a matrix your graphics API can understand. Try posting in the correct forums here for those answers.

##### Share on other sites
I'm afraid I don't really understand anything you just said. I've just tried using spherical coordinates on the roll and pitch, then fixing the x and z with the direction. I didn't get very far.

OpenGL seems to do this sort of thing anyway (I see the tip of the gun in the right place regardless of the rotation). Can I make OpenGL do this?

I need just a simple piece of code that does it all for me.

##### Share on other sites
Quote:
 Original post by GeometrianI'm afraid I don't really understand anything you just said. I've just tried using spherical coordinates on the roll and pitch, then fixing the x and z with the direction. I didn't get very far. OpenGL seems to do this sort of thing anyway (I see the tip of the gun in the right place regardless of the rotation). Can I make OpenGL do this?I need just a simple piece of code that does it all for me.
OpenGL provides an interface for transforming geometry for rendering purposes, but it doesn't provide direct support for performing geometric transform on the client side. It's really easiest and most straightforward to just do that yourself, and that is easiest and most straightforward if you use a proper math library rather than relying exclusively on OpenGL function calls.

Anyway, speciesUnknown described the process pretty clearly, but I'll describe it again (in somewhat different terms):

1. Your ship has a transformation associated with it. It doesn't matter how the orientation is represented (spherical coordinates, Euler angles, etc.), because ultimately you're going to end up with a 4x4 transform matrix (or something similar) that represents the ships position and orientation (assuming a 'rigid body' transform). Now if you're just using OpenGL functions such as glRotate*() to set up this transform, you may not have easy access to this matrix (which is why it's helpful to use a math library that is external to OpenGL).

2. Each of your guns has a local transform associated with it. If the guns point in the same direction that the ship is pointing, then presumably the linear (rotation) portion of the transform is identity. That just leaves the relative positions - the 'O1' and 'O2' that speciesUnknown referred to.

3. At any given time, the world transform for one of the guns is the product of the matrices representing its local transform and the ship's world transform. This world transform can then (presumably) serve as a starting transform for a projectile fired from the gun.

##### Share on other sites
Quote:
 Original post by GeometrianI need just a simple piece of code that does it all for me.
No, that's what you want. What you need is to understand the linear algebra underlying the computer graphics you're trying to do. If you don't understand the relationship between a transformation and a matrix, if you can't "read" a matrix, if when confronted by a spatial math problem your first instinct is towards angles and trigonometry instead of matrices and linear algebra, you need to learn this stuff. The OpenGL red book is a good resource. Early versions of it are available for free online. Get it and read through the chapters on transformations. Don't let your eyes glaze over when you get to the mathy parts.

##### Share on other sites
Quote:
 No, that's what you want. What you need is to understand the linear algebra underlying the computer graphics you're trying to do.
Actually, I want to do computer programming. It arguably isn't necessary, but it is something I want to do. However, to progress, I need to have this code. Basically, I need this to do what I want to do. I need to understand the code too.
Quote:
 If you don't understand the relationship between a transformation and a matrix, if you can't "read" a matrix, if when confronted by a spatial math problem your first instinct is towards angles and trigonometry instead of matrices and linear algebra, you need to learn this stuff.
So, like this?: http://en.wikipedia.org/wiki/Euler_angles

##### Share on other sites
Quote:
 So, like this?: http://en.wikipedia.org/wiki/Euler_angles
Some of the information in that link is relevant to your problem, yes. But what you really need is more of an overview (specifically, of linear algebra and how it is used in the context of games and graphics programming).

##### Share on other sites
What info in [R] is important? How is it used?

##### Share on other sites
Quote:
 Original post by GeometrianWhat info in [R] is important? How is it used?
I only glanced through the article, but I'm assuming that in the given context, [R] represents a 3x3 rotation matrix. Multiplying a 3-d vector by such a matrix will, in effect, apply the specified rotation to the vector.

Typically, an 'orientation' transform will be combined with a translation to yield an affine transform, usually represented using a 4x4 matrix.

That said, this topic is really too broad to explain it in a series of forum posts. I'd recommend starting to read some online articles and references on the subject, and maybe a book or two (personally, I recommend '3D Math Primer' as a good starting point). Then, come back and ask questions when you run into stuff you don't understand.

Also, keep in mind that just because you read something online doesn't mean it's true or accurate (books tend to be at least a bit more reliable in this respect). This is why it's important to refer to multiple references rather than getting all your info from just a few sources.

Finally, there are various differing conventions used in computer graphics (most notably the use of row- or column-vector notation) that can be confusing to those unfamiliar to the subject. This is just something to be aware of when comparing similar examples from different sources.

##### Share on other sites
I have the feeling that I'm almost there, but yet it doesn't work.
X = ShipPosition[0]Y = ShipPosition[1]Z = ShipPosition[2]A = math.radians(ShipRotation[0]) #Direction in the XZ planeB = math.radians(ShipRotation[1]) #PitchG = math.radians(ShipRotation[2]) #RollGammaRotationMatrix = matrix([[  math.cos(G),  math.sin(G),  0.0        ],                              [ -math.sin(G),  math.cos(G),  0.0        ],                              [  0.0        ,  0.0        ,  1.0        ]])BetaRotationMatrix  = matrix([[  1.0        ,  0.0        ,  0.0        ],                              [  0.0        ,  math.cos(B),  math.sin(B)],                              [  0.0        , -math.sin(B),  math.cos(B)]])AlphaRotationMatrix = matrix([[  math.cos(A),  math.sin(A),  0.0        ],                              [ -math.sin(A),  math.cos(A),  0.0        ],                              [  0.0        ,  0.0        ,  1.0        ]])ProductMatrix        = GammaRotationMatrix*BetaRotationMatrix*AlphaRotationMatrixPositionVectorMatrix = matrix([[Point[0]],[Point[1]],[Point[2]]])PositionMatrix       = ProductMatrix*PositionVectorMatrixX += PositionMatrix[0][0]Y += PositionMatrix[1][0]Z += PositionMatrix[2][0]return [X,Y,Z]
Can you help? Thanks,
G

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633742
• Total Posts
3013636
×