Jump to content
  • Advertisement
Yann_A

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

Recommended Posts

Posted (edited)

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 🙂

Edited by Yann ALET

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Posted (edited)

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. 

Edited by GoliathForge
Am I wrong? Serious...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!