Matrix Functions

In a recent update to GameMaker: Studio, there were a few new functions added to create and manipulate matrices, and this article will give you a brief overview of how they work.

The matrix functions are primarily designed as a compliment or replacement to the d3d_transform_* functions, as, by getting access to the different matrices, you can perform complex transforms in a far more efficient way.

NOTE: If all you are doing is performing a single rotation, translation or rotation, then you should still use the d3d_transform_* functions, however for two or more transforms the matrix functions are more efficient.

These new functions generate arrays of 16 values (representing a 4x4 matrix) which you can then use in further functions, or you can change individual values. It is worth noting that the same rules for regular arrays apply for these "matrix arrays" in that they are passed by reference to scripts and that if in a script any value of the array is modified, a new array is created with the new values which must then be returned from the script.

The Functions

The new functions in GameMaker: Studio enable you to get and set a matrix type, build a new matrix or multiply two matrices together. Below you can find a list of the functions and a brief description of how to use them.

matrix_get(type)

This will return the matrix values for the given matrix type, creating a 16 value 1D array which you would assign to a variable, eg:

v_array = matrix_get(matrix_view);

The available matrix types are accessed as constants:

matrix_view

matrix_projection

matrix_world

matrix_set(type, matrix)

You can use this function to set any one of the three available matrices (see the constants listed above) using the values stored in the matrix indexed. For example:

matrix_set(matrix_world, m_array);

matrix_build(x, y, z, xrotation, yrotation, zrotation, xscale, yscale, zscale)

This function can be used to create your own custom matrix and will return an index value for this new matrix which should be stored in a variable for future reference and use. The matrix itself is created as 16 value 1D array (first 4 values are row 1, second 4 values row 2 etc... of a 4x4 matrix).

NOTE: When you build a new matrix in this way the order of operation is YXZ

matrix_multiply(matrix, matrix)

With this function you can multiply two matrix together. The function will return a new matrix (again, as an array) which should be stored in a variable for future use, for example:

new_matrix = matrix_mutliply(m_array1, m_array2);

NOTE: You can't use a matrix constant as an argument here, so if you wish to multiply the (for example) view matrix with a custom matrix that you have built, you must first call matrix_get(matrix_view) and assign the view matrix values to a variable, then multiply it by your custom matrix, and then set the view matrix.

Have more questions? Submit a request

0 Comments

Article is closed for comments.