Transform 3d point using angles - math

I have a 3d point that I would like to rotate using angles (yaw, pitch and roll) around { 0, 0, 0 }.
How would I go about it, without converting the angles into a matrix?

Well, you don't really "convert angles into a matrix". Strictly speaking, rotations are linear transformation and are in general comprised of an angle and an axis (vector) about which the rotation occurs. The two easiest approaches to define an angle / axis rotation is by using quaternions or rotation matrices. There may be other approaches, but these two methods are used largely because they are the easiest methods anyone has proposed to date. Of the two, I personally prefer quaternions for rotations since they are easier to implement and require fewer arithmetic operations. 3x3 matrices have the benefit that they are able to handle general 3D->3D linear transformation; 4x4 matrices can perform general 3D->3D affine transformation on 3D vectors.
If you want to use separate rotations for yaw, pitch and roll, you should probably review issues related to Euler Angles. You can model these using either rotation matrices or quaternions. Both approaches will essentially be equivalent. It's simply a matter of defining a sequence of angle / axis pairs and multiplying them to get the final rotation. That rotation is then applied to whatever points you have to arrive at the rotated value.

Related

Representing rotation per second in 3D

What's a good way to represent 3D rotation per second that can handle more than 180° of rotation and is easy/efficient to apply with a time delta? Quaternions are great for orientation but fail to represent more than 180° of rotation and aren't too straight-foward to combine with a time delta.
You may use axis-angle representation, it's quite similar to quaternions, so you can easily convert between them, it's also easy to read/understand/interpolate and don't have the 180deg limit.
You have to be careful with conversions to quaternions though. You can't convert your target rotation like rotate by 180deg to quaternion, but you can interpolate it for current frame and convert only current delta if it's less than 180deg.

How to calculate rotation axis and angle?

I am trying to rotate a model in 3D so that it faces the right direction. The rotation I want is fairly trivial and can be broken down into two steps:
Rotate the model 90 degrees on its x-axis.
Rotate the model 180 degrees on its z-axis (relative to the first rotation).
The way to set a model's rotation in the framework I'm using (openFrameworks) is by calling its setRotation method. This method takes an angle, as well as floats x, y and z that specify the axis of rotation. How do I calculate the axis of rotation and angle for this particular rotation? I can't rotate the model two times sequentially because any call to setRotation overwrites previous rotations.
Please let me know if I can provide more information or clarity.
EDIT: In case anyone has the same question, this post helped me a lot.
weird that you can not apply more then one transform ... maybe you just use wrong function but anyway:
If you have direct access to transform matrix (or by get,set)
google for transform matrices if you do not have the knowledge
I suspect you are using 4x4 homogenous cartesian transform matrices
transform matrix anatomy
generate first rotation matrix and store it to M1
can use the setRotation for that
generate second rotation matrix and store it to M2
multiply them M=M1*M2
use this M instead of setRotation
If yo do not have the direct access to transform matrix and have to use just the setRotation
in that case you have to use quaternion which is the 4D vector you call the setRotation with
google for quaternion math and find the application of 2 rotations
I do not use them so I can not help with that but there are also equations out there
which converts 3x3 rotation matrix into quaternion and back
so you can still use the algorithm above
obtain M
extract the rotation matrix from it (it is just sub matrix you omit last row and column)
compute quaternion from it
and call setRotation with the result

How to convert points between two coordinate systems with different rotations

Imagine two coordinate systems layed on top of each other, with a rotation and scale difference between the two:
The problem is to convert a point from the non-rotated system to the other. What we do have, are four corner points forming a rectangle, with coordinates known for both systems at each point. We also know the rotation difference, and I think I at least should know the scale difference too. How do I convert a point from the non-rotated system to the rotated system? I have Unity3D at use.
Extra points for clarity in math :)
PS: I'm writing this really late, going to edit later for more clarity.
Some linear algebra does the trick:
Express each operation as a matrix and matrix multiply those to combine them into a single resulting matrix (for efficiency).
If translation is involved you need to add a dimension to your matrices, see homogenous coordinates.
The reason is that the mappings are affine ones then, not linear ones. You can ignore the extra dimension in the end result. It is just a nice way to embed affine mappings into linear ones, so the algebra is easier.
Example
M = M_trans * M_rot * M_scale
x' = M x
The order here is right to left: vector x is first scaled, then rotated, then translated into vector x'. (Using column vectors).
Hints on the matrices: Rotation Matrix, Scaling Matrix
For deriving 2D formulas when given 3D ones: either keep z = 0 or delete the 3rd row and 3rd column from each matrix.

Linear Algebra in Games in a 2D space

I am currently teaching myself linear algebra in games and I almost feel ready to use my new-found knowledge in a simple 2D space. I plan on using a math library, with vectors/matrices etc. to represent positions and direction unlike my last game, which was simple enough not to need it.
I just want some clarification on this issue. First, is it valid to express a position in 2D space in 4x4 homogeneous coordinates, like this:
[400, 300, 0, 1]
Here, I am assuming, for simplicity that we are working in a fixed resolution (and in screen space) of 800 x 600, so this should be a point in the middle of the screen.
Is this valid?
Suppose that this position represents the position of the player, if I used a vector, I could represent the direction the player is facing:
[400, 400, 0, 0]
So this vector would represent that the player is facing the bottom of the screen (if we are working in screen space.
Is this valid?
Lastly, if I wanted to rotate the player by 90 degrees, I know I would multiply the vector by a matrix/quarternion, but this is where I get confused. I know that quarternions are more efficient, but I'm not exactly sure how I would go about rotating the direction my player is facing.
Could someone explain the math behind constructing a quarternion and multiplying it by my face vector?
I also heard that OpenGL and D3D represent vectors in a different manner, how does that work? I don't exactly understand it.
I am trying to start getting a handle on basic linear algebra in games before I step into a 3D space in several months.
You can represent your position as a 4D coordinate, however, I would recommend using only the dimensions that are needed (i.e. a 2D vector).
The direction is mostly expressed as a vector that starts at the player's position and points in the according direction. So a direction vector of (0,1) would be much easier to handle.
Given that vector you can use a rotation matrix. Quaternions are not really necessary in that case because you don't want to rotate about arbitrary axes. You just want to rotate about the z-axis. You helper library should provide methods to create such matrix and transform the vector with it (transform as a normal).
I am not sure about the difference between the OpenGL's and D3D's representation of the vectors. But I think, it is all about memory usage which should be a thing you don't want to worry about.
I can not answer all of your questions, but in terms of what is 'valid' or not it all completely depends on if it contains all of the information that you need and it makes sense to you.
Furthermore it is a little strange to have the direction that an object is facing be a non-unit vector. Basically you do not need the information of how long the vector is to figure out the direction they are facing, You simply need to be able to figure out the radians or degrees that they have rotated from 0 degrees or radians. Therefore people usually simply encode the radians or degrees directly as many linear algebra libraries will allow you to do vector math using them.

3d rotation around the origin

I know there are plenty of questions about 3d rotation that have been answered here but all of them seem to deal with rotational matrices and quaternions in OpenGL (and I don't really care if I get gimbal lock). I need to get 3d coordinates EX:(x,y,z) of a point that always must be the same distance, I'll call it "d" for now, from the origin. The only information I have as input is the deltax and deltay of the mouse across the screen. So far here is what I have tried:
First:
thetaxz+=(omousex-mouseX)/( width );
thetaxy+=(omousey-mouseY)/( height);
(thetaxy is the angle in radians on the x,y axis and thetaxz on the x,z axis)
(I limit both angles so that if they are less than or equal to 0 they equal 2*PI)
Second:
pointX=cos(thetaxz)*d;
pointY=sin(thetaxy)*d;
(pointX is the point's x coordinate and pointY is the y)
Third:
if(thetaxz)<PI){
pointZ=sqrt(sq(d)-sq(eyeX/d)-sq(eyeY/d));
}else{
pointZ=-sqrt(abs(sq(d)-sq(eyeX/d)-sq(eyeY/d)));
}
(sq() is a function that squares and abs() is an absolute value function)
(pointZ should be the point's z coordinate and it is except at crossing between the positive z hemisphere and negative z hemisphere. As it approaches the edge the point gets stretched further than the distance that it is always supposed to be at in the x and y and seemingly randomly around 0.1-0.2 radians of thetaxz the z coordinate becomes NAN or undefined)
I have thought about this for awhile, and truthfully I'm having difficulty warping my head around the concept of quaternions and rotational matrices however if you can show me how to use them to generate actual coordinates I would be glad to learn. I would still prefer it if I could just use some trigonometry in a few axis. Thank you in advance for any help and if you need more information please just ask.
Hint/last minute idea: I think it may have something to do with the z position affecting the x and y positions back but I am not sure.
EDIT: I drew a diagram:
If you truly want any success in this, you're going to have to bite the bullet and learn about rotation matrices and / or quaternion rotations. There may be other ways to do what you want, but rotation matrices and quaternion rotations are used simply because they are widely understood and among the simplest means of expressing and applying rotations to vectors. Any other representation somebody can come up with will probably be a more complex reformulation of one or both of these. In fact it can be shown rotation is a linear transformation and so can be expressed as a matrix. Quaternion rotations are just a simplified means of rotating vectors in 3D, and therefore have equivalent matrix representations.
That said, it sounds like you're interested in grabbing an object in your scene with a mouse click and rotating in a natural sort of way. If that's the case, you should look at the ArcBall method (there are numerous examples you may want to look over). This still requires you know something of quaternions. You will also find that an at least minimal comprehension of the basic aspects of linear algebra will be helpful.
Update: Based on your diagram and the comments it contains, it looks like all you are really trying to do is to convert Spherical Coordinates to Cartesian Coordinates. As long as we agree on the the notation, that's easy. Let θ be the angle you're calling XY, that is, the angle between the X axis rotated about the Z axis; this is called the azimuth angle and will be in the range [0, 2π) radians or [0°, 360°). Let Φ be an angle between the XY plane and your vector; this is called the elevation angle and will be in the range [-π/2, +π/2] or [-90°, +90°] and it corresponds to the angle you're calling the XZ angle (rotation in the XZ plane about the Y axis). There are other conventions, so make sure you're consistent. Anyway, the conversion is simply:
x = d∙cos(Φ)∙cos(θ)
y = d∙cos(Φ)∙sin(θ)
z = d∙sin(Φ)

Resources