Constructing transformation matrix from final position - math

I have an object in 2D space, that is rotated/scaled around. Normally I would find the transformation matrix straightforwardly:
Translate by origin
Rotate
Scale
Translate by -origin
Translate by original position
Given an original object position at (0,0), I can easily get the new location as a vector by multiplying that by the transformation matrix.
However, for this problem I don't have the original position. I only have the final position.
How can I construct the same transformation matrix, when I only have the already-multiplied vector, not the original position?
I still have the transformation point, rotation and scale.

Not sure if I am telling you anything new but you use the inverses of the matrices applied in reverse order to find the original point(s) that was(were) transformed. Not entirely sure that this is what you are looking for but if so then there is a solution below.
Below is an example for 2D transformations. The method is easily extended for 3D
as you know the vector t and the rotation R and the scale s the inverses are easily calculated and applied.

Related

Calculate forward and up vectors from euler position and rotation?

I have an object in 3D space where all I have is a euler position and rotation. How can I calculate forward and up vectors from the information I have?
I know that I can calculate the forward vector in this way:
Vector3 forward = (target.getPosition() - object.getPosition()).normalize();
.. where target is any point along the axis which the object is looking. Using the information I have, how can I pick an arbitrary point in this way to normalize?
I'm not sure how to go about solving the "up" vector at all.
First create a transform matrix from your euler angles (with the same method as you are using while rendering). Then extract the axises vectors for forward and up from it directly. For example my view matrices uses Z axis for forward/backward and X axis for left/right so I would just use those two. You will find the location of the vectors here:
Understanding 4x4 homogenous transform matrices

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.

Getting a vector coordinate post matrix multiplication

I have a series of transformations that take my object and put it somewhere else. I am manually multiplying these transformations for the programmable pipeline in GL/ES. I'm rotating around distant arbitrary points and also translating, and while I have no trouble getting my object to finally position where I want it, I'd like to know how I can extract the final 3D vector coordinate of its position after these transformations.?
One option, suggested by this question, is to simply multiply your starting position by the final matrix and keep that result vector as the final coordinate. If so, what is the vector I use to represent my object's origin before these transformations? Because multiplying a matrix by my origin (0,0,0) simply results in a vector of zeroes.
The solution is surprisingly simple.
If I have a matrix M that is the final transformation created by all the matrix multiplications, then I can find the center of an object transformed by M by simply:
M * vector(0,0,0,1) // creates a 4D vector, where the first three, x,y,z are the coordinates
This is easily done manually in the code.
The key piece the question was missing was the exact vector to use for this multiplication.

Scaling an object with a local scale and a rotation

I have an object which has a position, a rotation angle and a scale (x and y). The overall transform matrix is as follows :
QTransform xform;
xform.translate(instance.definition.position.x, instance.definition.position.y);
xform.rotateRadians(instance.definition.rotation);
xform.scale(instance.definition.scale.x, instance.definition.scale.y);
I need to scale this object using a global scale which then modifies the local scale of the object. For example, the object is rotated by 45 degrees, I apply a scale of 1,2, I need to know how this affects the local scale as it should affect both local scale axes.
Thanks.
PS : maybe this is impossible due to being a non affine transformation, I don't know, I didn't find much on Google about this particular problem
UPDATE : I think I need to have at least a 3 col by 2 rows matrix transform to keep enough information, I tried some things in SVG which uses this kind of matrix transform and it seems to work, I will need to update this matrix according to the position and rotation though.
Either scale the object first
or calculate the inverse matrix, apply it to object (that undoes the translation/rotation), scale it and apply the first matrix again.
If you take, say, a rectangle, rotate it so that its edges are no longer parallel to the coordinate axes, then apply a scaling factor to, say, X, it will no longer be a rectangle. It will be a parallelogram, and your data structures will have to accommodate more information than they do now.

Resources