# portal-portal rotation

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

## Recommended Posts

im working on a game which uses teleportals to transport players. the stencil buffer rendering portion is done with. unfortunately though, i cant figure out how to rotate the scene to fit properly into the linked portal. take the following image for example. i have several planes with normals facing in different directions. the planes are the portals i want to draw a translated world into. i have the normals and position of each portal. i can get the up and right vectors if needed. my problem is, i dont know how to use the normals to rotate the world into place. using the dot product of the 2 normals just gives me a single angle but no axis to rotate the world on. i tried translating the world by the vector from the origin to the first portal. i could then rotate the world about the origin with the dot product and then translate it again by the vector from the origin to the linked portal. this only works on one axis, mainly the y-axis. im really confused and would appreciate any light on the subject! i hope im making sense. thanks! -adam

##### Share on other sites
If you have the normal, up, and side vector (all of unit length) then you can assemble a rotation/orientation matrix from them. There is most often no need to compute the corresponding angles (in whatever flavor) from them. Together with the position the full transformation is known (assuming you avoid scaling in this situation).

I assume the two sides of the portal are related by co-ordinate frames, both given in global space for simplicity. On this side of the portal (i.e. where the camera is) is the frame descibed by transformation T1, and on the other side is T2. Virtually, but not physically, the both frames should overlap to identity.

Anything given in global space can then be localized on the other side of the portal and then globalized on this side by multiplying with (notice that I'll use column vectors)
T1 * T2-1
This actually means that the thing of interest is virtually moved in global space so that its spatial relation to frame 1 after transformation is the same as to frame 2 before transformation.

If necessary the other way around is also possible, of course:
T2 * T1-1
to transfer something seen on this side of the portal to the other.

Any locally related space can be incorporated the usual way by globalizing and multiplying. Which steps are actually necessary depends on the particular problem you have to solve.

Hopefully its the stuff above you are looking for!?

[Edited by - haegarr on September 28, 2006 3:45:35 AM]

##### Share on other sites
Quote:
 Original post by haegarrIf you have the normal, up, and side vector (all of unit length) then you can assemble a rotation/orientation matrix from them. There is most often no need to compute the corresponding angles (in whatever flavor) from them. Together with the position the full transformation is known (assuming you avoid scaling in this situation).I assume the two sides of the portal are related by co-ordinate frames, both given in global space for simplicity. On this side of the portal (i.e. where the camera is) is the frame descibed by transformation T1, and on the other side is T2. Virtually, but not physically, the both frames should overlap to identity.Anything given in global space can then be localized on the other side of the portal and then globalized on this side by multiplying with (notice that I'll use column vectors)T1 * T2-1This actually means that the thing of interest is virtually moved in global space so that its spatial relation to frame 1 after transformation is the same as to frame 2 before transformation.If necessary the other way around is also possible, of course:T2 * T1-1to transfer something seen on this side of the portal to the other.Any locally related space can be incorporated the usual way by globalizing and multiplying. Which steps are actually necessary depends on the particular problem you have to solve.Hopefully its the stuff above you are looking for!?

you are correct about me putting them in a global coordinate system, for easier math. now let me see if i understand what you are talking about. lets say i have 2 portals which are linked. their matrices (im assuming you are calling them column vectors) look like this:
for simplicities sake:               and the linked portal's matrix:right  [1, 0, 0]                     [0, 0, 1]up     [0, 1, 0]                     [1, 0, 0]normal [0, 0, 1]                     [0, 1, 0]

if i multiply the matrices together i get another matrix. from there im stuck. i dont know how to use it for rotations.

also am i understanding that my "translate, rotate, translate" method is the right way to go?

[Edited by - adam17 on September 28, 2006 10:32:06 AM]

##### Share on other sites
I'll answering your post in several replies, because it is too much for one.

Be aware that using either "column vectors" or "row vectors" makes a big difference when applying matrices. E.g. when using column vectors, multiplication of a matrix and a vector is done this way
v' := M * v
while when using row vectors it is done this way
v' := v * M

That plays a role since matrix multiplication isn't commutative. If you build up the matrix from several sub-matrices (as is often the case for transformation matrices), then the this in column vector representation
v' := M2 * M1 * v
means that 1st M1 is applied to the vector, than M2 is applied on the result. The same in row vector representation is
v' := v * M1 * M2
Notice the change in order.

The relation of a column and row vector is the transpose operation:
vC = vRT
To be correct the one kind has to be written transposed if both kinds appear in the same text.

To yield in the same result, also the matrices are related in this way:
MC = MRT
You can see that if actually performing the matrix vector product in both ways.

Hence, whenever dealing with matrices, make clear what kind of vectors you are using!

[Edited by - haegarr on September 28, 2006 12:49:59 PM]

##### Share on other sites
ok cool! im getting the idea of the multiplication and importance of column vectors and row vectors. i have one question though. when you are building the matrix, how do you store them?

like this maybe?
axis-->        X  Y  Zright vector  <1, 0, 0>up vector     <0, 1, 0>normal vector <0, 0, 1>

it may help if i mention that im using OpenGL (a right-handed coordinate system)

##### Share on other sites
Quote:
 Original post by adam17lets say i have 2 portals which are linked. their matrices (im assuming you are calling them column vectors) look like this:for simplicities sake: and the linked portal's matrix:right [1, 0, 0] [0, 0, 1]up [0, 1, 0] [1, 0, 0]normal [0, 0, 1] [0, 1, 0]if i multiply the matrices together i get another matrix. from there im stuck. i dont know how to use it for rotations.

First, if you have the 3 vectors, and each vector is of unit length, and they are pairwise orthogonal so that
r x u = n
(and so forth), then writing all vectors as column vectors (or, if used instead, as row vectors) of a matrix
M := [ r   u   n ]
then you have effectively a so-called "ortho-normal basis". That matrix is by itself a rotation matrix.

You can extend this matrix to a homogeneous matrix to supply translations in the same matrix as well.

Quote:
 Original post by adam17lets say i have 2 portals which are also am i understanding that my "translate, rotate, translate" method is the right way to go?

A local to global transformation is (for column vectors) normally modelled as
T * R
that is first a rotation, then a translation. Doing so for portal 1 and portal 2, and setting this into the transformation I suggested in an earlier reply, yields in
( T1 * R1 ) * ( T2 * R2 )-1 = T1 * R1 * R2-1 * T2-1
The overall structure is hence a translation, then a rotation (assembed by 2 particular rotations), then another translation. So you are right with the structure.

EDIT: Please notice that in one of my previous replies the T has denoted a whole transformation, while in this reply the T means a translation matrix only!

[Edited by - haegarr on September 28, 2006 11:14:47 AM]

##### Share on other sites
Quote:
 Original post by adam17ok cool! im getting the idea of the multiplication and importance of column vectors and row vectors. i have one question though. when you are building the matrix, how do you store them? like this maybe?axis--> X Y Zright vector <1, 0, 0>up vector <0, 1, 0>normal vector <0, 0, 1>it may help if i mention that im using OpenGL (a right-handed coordinate system)

OpenGL uses column vectors, so you are lucky since I prefer using column vectors in my explanations as well ;)

The (homogeneous) matrix structure as used by OpenGL is
[ right_x  up_x  normal_x  0 ][ right_y  up_y  normal_y  0 ][ right_z  up_z  normal_z  0 ][    0       0       0     1 ]

if you interpret right being the x vector, up being the y vector, and normal being the z vector.

EDIT: As an array, OpenGL expects right_x at index 0, right_y at index 1, ... up_x at index 4, ...

##### Share on other sites
okay, cool i think im getting it. so let me try an example with translation.

portal1 (facing +Z and @ position <5 0 0>[ 1  0  0  5][ 0  1  0  0][ 0  0  1  0][ 0  0  0  1]portal2 (facing +Y and @ position <0 0 2>[ 0  1  0  0][ 0  0  1  0][ 1  0  0  2][ 0  0  0  1]

if i try to rotate the world to fit into portal 1, i would do the following:
(Mr = final rotation matrix, M1 = portal1's matrix, and M2 = portal2's matrix
Mr = M1 * M2^-1     [ 1  0  0]   [ 0  1  0]-1  Mr = [ 0  1  0] * [ 0  0  1]  =      [ 0  0  1]   [ 1  0  0]            [ 1  0  0]   [ 0  0  1]   [ 0  0  1]   = [ 0  1  0] * [ 1  0  0] = [ 1  0  0]     [ 0  0  1]   [ 0  1  0]   [ 0  1  0]

the M2^-1 appears to be the transverse of M2 but i think im wrong. am i?
maybe because i was using an identity matrix for the first matrix, but the answer seems to be the same as M2^-1. am i wrong?

btw you dont know just how MUCH i appreciate your help!!! thanks

##### Share on other sites
im still struggling with the matrices and all but i had a stroke of genius!

i take the 2 normals of the portals and find the dot product. then i take the cross product of the 2 normals, and rotate the scene around the cross product! i think it will work....

EDIT: it works, but the dot product is not oriented correctly. its only 0 <= theta <= pi. i need something for 0 <= theta <= 2pi

[Edited by - adam17 on September 30, 2006 1:15:22 AM]

##### Share on other sites
Quote:
 Original post by adam17if i try to rotate the world to fit into portal 1, i would do the following:(Mr = final rotation matrix, M1 = portal1's matrix, and M2 = portal2's matrixMr = M1 * M2^-1 [ 1 0 0] [ 0 1 0]-1 Mr = [ 0 1 0] * [ 0 0 1] = [ 0 0 1] [ 1 0 0] [ 1 0 0] [ 0 0 1] [ 0 0 1] = [ 0 1 0] * [ 1 0 0] = [ 1 0 0] [ 0 0 1] [ 0 1 0] [ 0 1 0]the M2^-1 appears to be the transverse of M2 but i think im wrong. am i?

For ortho-normal matrixes the inverse is identical to the transpose. I don't know the term transverse for this, but perhaps it is (also) a valid term?!

Quote:
 Original post by adam17maybe because i was using an identity matrix for the first matrix, but the answer seems to be the same as M2^-1. am i wrong?

Nope, you're correct. That is what gives the identity matrix its name: The fact that it does an identity mapping if used for multiplication.

BTW: If you're interested in: This kind of naming is also used for scalar algebras. E.g. 1 is the identity value for the scalar multiplication, and 0 is the identity value for the scalar addition. Here you can see that "identity" is AFAIK normally only a valid term if used in conjuntion with an operation.

[Edited by - haegarr on September 30, 2006 6:47:14 AM]

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
15
5. 5

• 9
• 13
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632592
• Total Posts
3007280

×