#### Archived

This topic is now archived and is closed to further replies.

# Help? Simple 3D math question? (I think)

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

## Recommended Posts

I have an object (say a space ship) in 3D space at known position (x, y, z). My ship is currently pointed in a direction indicted by 3 known rotation angles (yaw, pitch, roll). My ship is currently moving at a known (scalar) speed ''s'' in the direction in which it is pointed. I would like to compute the exact velocity (in terms of changes for x, y, and z). In other words, I would like to know how to change x, y, and z each update based upon the speed ''s''. How do I determine the formula? I think maybe I am looking at this in the wrong way. Any help would be appreciated.

##### Share on other sites
Instead of storing the three values pitch, yaw, roll it is generally a good idea to store something like a ''forward'' vector of your ship. If your ship moves forward you just have to add this to your position. If the ship rotates, you rotate the vector along.

(I know there''s quaternions and other approaches too, but I tried to keep it simple.)

##### Share on other sites
Hmm.. I guess I'm not clear exactly how to do that.

The weird thing is, I understand vectors on paper, but I'm not sure how to apply them to my game in the way that you describe.

[edited by - The Frugal Gourmet on August 19, 2003 2:32:58 PM]

##### Share on other sites
like Wildfire said, you should probobly have a forward vector instead of yaw, pitch, and roll... maybe in your design, this actually makes it more complex, but in most situations I have dealt with, it is much easier to use a forward vector.

A forward vector is a vector which is normally a unit vector, magnitude of 1, and it points in the direction the object is moving. So if you an object is moving along the x axis, the vector would be {1, 0, 0}... if it was moving at a 45deg angle between x and y axis, the vector would be {.707, .707, 0} = {sqrt(2)/2,sqrt(2)/2,0}... etc

now if you want to move your unit, just add this vector to it''s position and it will move one unit. if you want to move it x number of units, add the vector * x to it''s position. If you want to change the direction the unit is moving, then just alter the forward vector.

if you want, I can dig up the math to convert from your spherical coords to rectangular coords. just google sphereical rectangular conversion... It''s a little tricky though...

If you want me to explain the conversion, just let me know

Dwiel

##### Share on other sites
Thanks for your help. That is a pretty clear way of putting it.

So, I think I know what to do in theory, but I'm a little bit fuzzy on how to convert my current system into vectors.

So I guess I need to figure out a way to convert my current position (x, y, z) and rotation angles (yaw, pitch, roll) into a vector and vice versa.

Or, if I have a vector, how do I know how to represent it in terms of what's on the screen (and the opposite)?

You said "conversion" between spherical and rectangular coordinates. Is this what I need to change the represenation? Will look it up and see if I am enlighted.

Thanks.

[edited by - The Frugal Gourmet on August 19, 2003 4:04:39 PM]

[edited by - The Frugal Gourmet on August 19, 2003 4:06:38 PM]

##### Share on other sites
Actaully, it would probobly be advantagous for you to just drop the yaw/pitch/roll thing completely so there are no conversions... If there is a situation in which you are confused about implementation using cartesian/rectangular coordinates instead of spherical, just let me know, and I can probobly help you out. I think having both systems used in your engine/program/whatever will only make it more confusing, chose one method and stick with it.

Hope I can help

Dwiel

##### Share on other sites
this looks like t1m''s question (you some kind of twin brother of his? ).

If you keep a 3x3 rotation matrix of your ship, you can find the forward vector as being the third row or column of the matrix

Forward = Vector3(Orientation.e13, Orientation.e23, Orientation.e33);

Equally, you can find the up vector of your ship using the second row/column of the matrix

Up = Vector3(Orientation.e12, Orientation.e22, Orientation.e32);

and the left vector of your ship as the first row/column of the matrix

Left = Vector3(Orientation.e11, Orientation.e21, Orientation.e31);

now, you have the axis around your ship, and the angles you''d like your ship to rotate about around those axis. What you need is a function to convert a rotation made of an axis and an angle around that axis to a matrix

YawMatrix .FromRotationAroundAxis(Up, YawAngle);
PitchMatrix.FromRotationAroundAxis(Left, PitchAngle);
RollMatrix .FromRotationAroundAxis(Forward, RollAngle);

then you can multiply your current orientation matrix by those rotations

Orientation = Orientation * YawMatrix * PitchMatrix * RollMatrix;

there are complications involved with this one. First, you need to know if your matrix is row-aligned or column-aligned. It depends on the maths system you use.

secondly, the order in which you do the matrix multiplications is important. You might want to do

Orientation = Orientation * PitchMatrix * RollMatrix * YawMatrix;

with this algorithm, the RollAngle, PitchAngle and YawAngle are delta angles, that depends on your inputs at the current frame. They are not absolute angles.

##### Share on other sites
What you want to do is pretty easy. First, when the ship is not rotated, there is some unit vector that points in the direction that the ship is pointing, right? All you do is rotate that vector by your yaw, pitch, and roll (just like the ship). Then the velocity is the rotated vector scaled by the ship''s speed.

##### Share on other sites
JohnBolton: you got it. Just rotate your direction vector so that it points in the direction you wnt you unit moving

Dwiel

##### Share on other sites
Thanks, guys.

You''ve given me a lot of really good advice and helped me alot.

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

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633298
• Total Posts
3011249
• ### Who's Online (See full list)

There are no registered users currently online

×