Skip to main content
Inspiring
October 22, 2024
Answered

Transformation matrix - pivot point

  • October 22, 2024
  • 1 reply
  • 1258 views

Hi everyone. I'm trying to figure out matrix transformations, more specifically, how to change pivot/origin center. So, when you apply following transformation, rotate, scale and translate:

matrix.mat[0][0] = cosf(angle) * scale;
matrix.mat[0][1] = sinf(angle) * scale;
matrix.mat[1][0] = -sinf(angle) * scale;
matrix.mat[1][1] = -sinf(angle) * scale;

matrix.mat[2][0] = position_x;
matrix.mat[2][1] = position_y;
matrix.mat[2][2] = 1;

it works fine, just the issue is it's origin is upper left corner, but I want origin to be in the center. Now, I know I need to first translate by layers half, then rotate by custom angle, and then translate back, but I don't know how that should look like in terms of matrix transformation (code than I posted). Any help would be much appreciated.

This topic has been closed for replies.
Correct answer shachar carmi

the simple answer is that the anchor point goes in the place of the position with negative values.

however, you can't do that all in one placement of data in the matrix. you should create a matrix for the anchor point. then multiply it by the rotation matrix, and then multiply the result with the position matrix.

1 reply

shachar carmiCommunity ExpertCorrect answer
Community Expert
October 22, 2024

the simple answer is that the anchor point goes in the place of the position with negative values.

however, you can't do that all in one placement of data in the matrix. you should create a matrix for the anchor point. then multiply it by the rotation matrix, and then multiply the result with the position matrix.

Inspiring
October 23, 2024

Thanks a lot. Do you know if there is a built in function for matrix multiplication, or I have to make my own?

Community Expert
October 23, 2024

Thank you very much!


♫♪♪ what can i say except "you're welcome" ♪♫♪

just don't forget to start off with an identity matrix for each of the separate matrices, or strange thigs will happen...

oh, and if the pivot is still not correct, try reversing the matrix order. matrix multiplication is not commutative, so a*b isn't the same as b*a, and some multiplaction code gives the opposite order from what you'd expect, so either change the multiplication code, or reverse the order of multiplication.