How to build a matrix for rotating a square around itself?

Started by
6 comments, last by Yann_A 5 years, 1 month ago

Hi,

Say I have a square with the following vertices in world position.

topleft=100,100

bottomRight= 200,200

So the center is at 150,150

So my local to world matrix should be a translation T to 150,150 (if my for vertices where offset around the origin by 50 in local space)

Now, this is what I tried to build my rotation matrix.

  • Compute Tinv as the inverse of T to come back to local space.
  • Compute R as the rotation matrix around 0,0,1 axis with pi/2 as the angle.

So my plan to builld this matrix was to first come back to local space, then rotate, then go back to world space.

F = Tinv * R * T

And that doesn't work.

It does work when I apply those matrices independently though, so like:

rotatedTopLeft = topLeft * TInv

rotatedTopLeft = rotatedTopLeft * R

rotatedTopLeft = rotatedTopLeft * T

But what I want is combine TInv, R and T into a single matrix so I can just do

rotatedTopLeft = topLeft * F

So how exactly do I compute F?

Thanks ?

Advertisement

In what way doesn't it work? Can you describe the results?

Things to double check might include that matrix multiplication is implemented correctly, and that the conventions are consistent (it looks like you're using row vectors consistently and that that's what the library expects, but it might be worth confirming what conventions the library uses, just for clarity).

It might also be useful to know what the values of F are, and to see example output for one of the vertices (that is, what you get when you multiple a vertex by F).

Hi, Yann. I'm new here so don't know if you've specified your math library or game engine in tags or something, but based on what you are saying you should be able to combine those matrices into a single matrix to get the expected result.  If it's not working, try reversing the order of multiplication.  You may need  F = T * R * Tinv instead.

Hmmm, It's funny I see this different...When I look at 

2 hours ago, Yann ALET said:

F = Tinv * R * T

And that doesn't work.

It does work when I apply those matrices independently though, so like:

rotatedTopLeft = topLeft * TInv

rotatedTopLeft = rotatedTopLeft * R

rotatedTopLeft = rotatedTopLeft * T

I see comparing apples and oranges. The first is not putting anything through the transformation. Seems everyone would agree, you have to touch the space instead of hopping over it, in order to say you did something there. So we're left with F = R in that case because T is canceled out. 

48 minutes ago, David Peterson said:

If it's not working, try reversing the order of multiplication.  You may need  F = T * R * Tinv instead.

Although it's definitely worth double checking conventions here, it's worth noting that the order 'Tinv * R * T' does appear to be correct given the information the OP has provided. (If the opposite order worked, that would raise some additional questions, and further investigation would probably be needed anyway.)

17 minutes ago, GoliathForge said:

Am I wrong? Serious...

I think perhaps so. T and Tinv don't necessarily cancel in this case, and regardless, performing the vector multiplications in sequence should produce the same results (within numerical limits) as a single vector multiplication with a combined matrix.

Around itself? You are planing to rotate around the center right?

Then first build matrix which translates center of square to 0,0,0 point then apply rotation matrix and translate it back to original position

Well, not sure what to say.

I had this problem at work, and posted this from what I remember my problem was, how I tried to solve it, and how it failed.

Now, I wrote it again from home so I could give you folks actual values during transforms, and it works just fine.

Well... thanks for your help, it's really great to see so many people willing to help!

This topic is closed to new replies.

Advertisement