Sign in to follow this  

Vectors with Three Rotations

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

If you intended to correct an error in the post then please contact us.

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


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


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


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


Link to post
Share on other sites
Quote:
Original post by Geometrian
I 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 this post


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


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


Link to post
Share on other sites
Quote:
Original post by Geometrian
What 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 this post


Link to post
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 plane
B = math.radians(ShipRotation[1]) #Pitch
G = math.radians(ShipRotation[2]) #Roll
GammaRotationMatrix = 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*AlphaRotationMatrix
PositionVectorMatrix = matrix([[Point[0]],[Point[1]],[Point[2]]])
PositionMatrix = ProductMatrix*PositionVectorMatrix
X += PositionMatrix[0][0]
Y += PositionMatrix[1][0]
Z += PositionMatrix[2][0]
return [X,Y,Z]
Can you help? Thanks,
G

Share this post


Link to post
Share on other sites
Quote:
Original post by Geometrian
I have the feeling that I'm almost there, but yet it doesn't work.*** Source Snippet Removed ***Can you help? Thanks,
G
Hm. It's a little hard to help with an excerpt of an implementation such as the one you've presented here. I don't know what some of the variables represent; I don't know what language is being used; I don't know the conventions or behavior of the math library you appear to be using (row vectors or column vectors? what does it mean to construct a matrix from a vector?); and I don't know how your Euler-angle rotations are intended to be applied, or if you're applying them the same way here as you do when rendering the player ship model.

Now, it kind of looks like you have the right idea, but the vector-as-matrix stuff at the end has me a bit lost.

Can you be more specific about what 'doesn't work'?

Share this post


Link to post
Share on other sites
Quote:
Hm. It's a little hard to help with an excerpt of an implementation such as the one you've presented here.
I shall attempt to clarify.
Quote:
I don't know what some of the variables represent;
ShipPosition has the x, y, and z positions. That's the location of the ship, and so is not directly involved in the calculation, just in translating the bullets to the location of the ship in 3D. The Point thing is the position of the bullets relative to the ship in x, y, and z.
Quote:
I don't know what language is being used;
Python
Quote:
I don't know the conventions or behavior of the math library you appear to be using (row vectors or column vectors? what does it mean to construct a matrix from a vector?);
std. math and Numpy: http://www.scipy.org/Tentative_NumPy_Tutorial
Quote:
Can you be more specific about what 'doesn't work'?
The bullet append location swerves around in weird ways that aren't right. The bullets don't start where they need to be.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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