community icon indicating copy to clipboard operation
community copied to clipboard

Chaining transformations using Matrix class gives wrong results

Open udiboy1209 opened this issue 9 years ago • 4 comments

mat_tx = Matrix().translate(-5,-5,0) # translate by -5,-5
mat_rot = Matrix().rotate(3.14159/4, 0,0,1) # Rotate by pi/4 around z
mat_tf = mat_rot.multiply(mat_tm)

The above gives correct transformation matrix for translating and rotating an object.

I was expecting this to work but it gives strange result

mat_tf = Matrix().translate(-5,-5,0).rotate(3.14159/4,0,0,1)

Both mat_tfs are different but trying to translate (7,7) with first gives correct point (0,2.82) but gives some weird result for second mat_tf.

Can the Matrix class be used like this? If not, there needs to be a mention in the docs.

udiboy1209 avatar Sep 26 '16 17:09 udiboy1209

I wonder if it's not a confusion about the effect of x.multiply(y) (instead of y.multiply(x))

>>> from kivy.graphics.transformation import Matrix
>>> mat_tf = Matrix().translate(-5,-5,0).rotate(3.14159/4,0,0,1)
>>>
>>> mat_tx = Matrix().translate(-5,-5,0) # translate by -5,-5
>>> mat_rot = Matrix().rotate(3.14159/4, 0,0,1) # Rotate by pi/4 around z
>>> mat_rot.multiply(mat_tx).tolist() == mat_tf.tolist()
False
>>> mat_tx.multiply(mat_rot).tolist() == mat_tf.tolist()
True

tshirtman avatar Nov 01 '16 21:11 tshirtman

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 07 '17 05:10 stale[bot]

👋 We use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please use our support channels to get help with the project.

If you're having trouble installing Kivy, make sure to check out the installation docs for Windows, Linux and macOS.

Let us know if this comment was made in error, and we'll be happy to reopen the issue.

github-actions[bot] avatar Nov 09 '23 02:11 github-actions[bot]

It's true - the transformations can't be chained, what Kivy usually does when using those matrices is that it applies individual transforms to the identity matrix and then multiplies the resulting matrices with one another. The way scale and translate are implemented in particular will only work on the identity matrix.

This is probably something we'd want to fix (instead of merely documenting it) - being correct will still work on the identity matrix (as it does currently) but will also allow chaining transformations.

Cheaterman avatar Jul 24 '25 13:07 Cheaterman