Convert position, rotation, and scale using matrices
Expressing the Transform of a Game Object as a Matrix
The articles written on this blog were created with the goal of teaching. Also, the information in the article may be incorrect, so please be careful.
The position, rotation, and size of a game object can be expressed as a matrix. Let’s first check the transformation based on the origin in 2D.
size conversion
[ sx 0 ] [ x ] = [ sx * x ]
[ 0 sy ] [ y ] [ sy * y ]
The X axis issx, Y axis issyincreases as much as If one of the two is negative, it is flipped based on that axis.
rotation transformation
[ cosθ -sinθ ] [ x ]
[ sinθ cosθ ] [ y ]
This matrix rotates the vector by θ about the origin. Be careful not to mix functions that accept Degree with functions that accept Radian.
Why is the movement 3×3?
The origin cannot be moved to another location with just a 2×2 matrix. So in 2D coordinatesw = 1Use homogeneous coordinates with the addition of .
[ 1 0 tx ] [ x ] [ x + tx ]
[ 0 1 ty ] [ y ] = [ y + ty ]
[ 0 0 1 ] [ 1 ] [ 1 ]
In 3D, a 4×4 matrix is used for the same reason.
Order changes results
Doubling the size of an object and then moving it 10 degrees to the right gives different results than moving it first and then doubling the entire coordinates.
Scale → Translate
Position moves by 10
Translate → Scale
Movement amount can be doubled
Since each engine and math library differs in whether vectors are placed in rows or columns, the multiplication order of the code must be checked in the documentation. It is more important in what coordinate system and in what order it is applied than the matrix itself.