Godot: Create and manipulate matrices in GDscript

Created on 8 Mar 2019  路  11Comments  路  Source: godotengine/godot

Godot version:
Perhaps in the next version (after 3.1)?

Issue description:
Feature request - Allow user to create a matrix of any dimension and perform basic matrix-matrix/vector manipulations.

It seems (correct me if I'm wrong) there is no way to create a matrices of a given size (e.g. 5x5) and perform basic matrix manipulations and calculations (e.g. matrix-matrix multiplication, matrix inversion, determinant calculation, etc). E.g. in numpy (yes, yes, I know numpy!=GDScript) you would create matrices using this method , e.g. numpy.array([[1,0,0],[0,2,0],[0,0,3]]) and you'd be able to perform matrix multiplcation using the @ operator. Maybe GDScript can use a similar method, e.g. to create a matrix use Matrix([1,0,0],[0,2,0],[0,0,3]) etc.

archived feature proposal core

Most helpful comment

Please describe the use case. A priori there is little use for most game developers in doing n-dimension linear algebra, and we won't implement advanced math features that are not useful for making games.

All 11 comments

Please describe the use case. A priori there is little use for most game developers in doing n-dimension linear algebra, and we won't implement advanced math features that are not useful for making games.

Please describe the use case. A priori there is little use for most game developers in doing n-dimension linear algebra, and we won't implement advanced math features that are not useful for making games.

It'd be very useful for simulation programs which involve hefty calculations - e.g. solving systems of discretised m-s-d equations (creating matrices with large dimensions and perform matrix inversion and multiplication becomes essential). Matrix-vector multiplcation would be very handy for rotating 2D screen
coordinates by a set angle (multiply transform matrix by 2D coordinate vector).

matrix-matrix multiplication, matrix inversion, determinant calculation

not a math major, but couldn't a developer create these formulas using gdscript? since it's possible to manipulate and create these matrices already

I think its not for games and for scientific applications..

Matrix-vector multiplcation would be very handy for rotating 2D screen
coordinates by a set angle (multiply transform matrix by 2D coordinate vector).

For this use case you already have Transform, Basis and Quat in 3D, and Transform2D in 2D.

I guess you can create an n-dimensional matrix (and also perform required calculations), but it's up to the developer to code the functions in GDScript, as @girng mentioned (see the last reply in this thread https://godotdevelopers.org/forum/discussion/20590/matrix-matrix-vector-multiplication). My original question was about using out-of-the-box Matrix functionality built directly into the engine (e.g. like Vector2, Vector3, .cross() , .dot(), etc but instead for matrices) to do this (without having to go through the hassle of manually programming subroutines in GDScript), but it seems as though that doesn't exist for now (hopefully will be implemented in the future?). I'll close this thread (if your stuck, please view the reply in this link https://godotdevelopers.org/forum/discussion/20590/matrix-matrix-vector-multiplication)

for what it's worth I'm making a game for which a n-dimensional matrix would've been helpful.

For me its AI, ML etc. Godot like game engines uses GPU for texture and others. So, if we have inbuilt feature for that kind of calculation that would be helpful. Already Unity and other engines adding AI features in their engine. Godot should introduce optional downloadable package or library for doing so.

matrix-matrix multiplication, matrix inversion, determinant calculation

not a math major, but couldn't a developer create these formulas using gdscript? since it's possible to manipulate and create these matrices already

I made these formulas for gdscript and the performance is god-awful. Currently re-implementing all of it in a c++ module.

For me its AI, ML etc. Godot like game engines uses GPU for texture and others. So, if we have inbuilt feature for that kind of calculation that would be helpful. Already Unity and other engines adding AI features in their engine. Godot should introduce optional downloadable package or library for doing so.

I'm currently doing just that. Once it's ready I'll put it in a public repo for anyone who wants it.

There are two distinct advantages of built-in matrices if done well:

  • matrix calculations are way faster than looping over data in scripts (~50 times in case of python). Geometry and statistics gain tremendously from linear algebra, both in terms of speed and coding clarity (and clarity in the underlying math too). Say, I want to collect some sort of feedback from the player habits, and do something as basic as linear regression using that data. It is almost trivial to do it using linear algebra in a dimension-agnostic way, I don't even know how to achieve this without relying on non-linear optimization if you cannot use matrices.
  • matrices (and higher-dimensional arrays) are useful data structures. It is not just that C++ is faster than the script, but data structures that are organized in a certain manner allow to use parallel CPU instructions, can be offloaded to GPU etc., there area also special data structures for sparse, symmetric, diagonal, etc matrices.

There is also quite a few existing matrix and linear algebra libraries to choose from that one can link to. But doing it well... This is probably a big task.

Guys I would like to know which more operations should be added to Matrix class, if you know which more is needed please comment, then a godot PR can be made from there! https://github.com/goostengine/goost/issues/14

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SleepProgger picture SleepProgger  路  3Comments

mefihl picture mefihl  路  3Comments

Zylann picture Zylann  路  3Comments

ducdetronquito picture ducdetronquito  路  3Comments

testman42 picture testman42  路  3Comments