Translate

Community Forums/Monkey Talk/Translate

D4NM4N(Posted 2011) [#1]
Is there any docs anywhere that explain this? The docs included in monkey don't explain very much.

Edit SORRY! I meant transform :/

Function Transform

Syntax
Function Transform( ix#, iy#, jx#, jy#, tx#, ty# )
Parameters
ix, iy, jx, jy, tx, ty - matrix components.
Description
Multiplies the current matrix by the specified matrix.



what is "i" "j" and "t" !?!


The other bit i do not really get is how the Image relates to Pushmatrix and popmatrix.
Push and pop what matricies? i have not declared any?

Last edited 2011


Warpy(Posted 2011) [#2]
I have a feeling I've posted this before, but it might have been on the beta forum:


The transformation matrix converts initial coordinates (x,y) to new coordinates (nx,ny). Here's the formula:
nx = ix*x + iy*y + tx
ny = jx*x + jy*y + ty


Here are a few values of (ix,iy,jx,jy,tx,ty) and what they do. You might want to work out the formula on paper to see why they do what they do:

(1,0,0,1,0,0) - does nothing! This is known as the identity matrix.

(1,0,0,1,50,30) - translate 50 pixels right, 30 down.

(5,0,0,5,0,0) - scale everything up by a factor of 5.

(0,-1,1,0,0,0) - rotate 90 degrees clockwise.

(1,1,0,1,0,0) - horizontal shear.


Mojo has a transformation matrix in memory which is applied to every drawing command. When you call one of the transformation functions (Scale, Translate, Rotate) the transformation matrix is changed to give the desired effect. You can see how this happens by looking at the source code in modules/mojo/graphics.monkey.

PushMatrix and PopMatrix allow you to take a snapshot of the current matrix, so that you can do some more transformations (eg to draw an enemy), and then reset the matrix back to how it was.

PushMatrix takes a copy of the current transformation matrix and puts it on top of a stack. PopMatrix takes the top matrix off the stack and makes it the current matrix for drawing.

Here's some code:
'at the start, the current matrix is the identity matrix

PushMatrix 'the identity matrix goes on top of the stack.

Rotate 90  'apply a rotation to the current matrix. The identity matrix on the stack is not affected.

PushMatrix 'Put the rotated matrix on the top of the stack.

Scale 3,3  'scale the current matrix by a factor of 3. Again, the matrices on the stack aren't affected.

DrawImage dude,0,0  'draw an image. It will be three times as big as normal, and rotated 90 degrees.

PopMatrix 'pop the top matrix off the stack. The rotated matrix was the last one we pushed, so now the current matrix is a rotation of 90 degrees but no scale.

DrawImage dude,0,0 'draw the image again, but this time it's at normal scale. It's still rotated 90 degrees.

PopMatrix 'pop the top matrix off the stack. The only matrix left on the stack is the identity matrix, so it takes that one.

DrawImage dude,0,0 'the image gets drawn with no transformations applied



D4NM4N(Posted 2011) [#3]
Great! thanks warpy.

Why is this stuff not in the manual? It is going to put a lot of people off. :/

Last edited 2011


Warpy(Posted 2011) [#4]
If only we had an official wiki...


Richard Betson(Posted 2011) [#5]
Warpy's the man on this. He has several demo's you can view included with Monkey as well as my own demo all using Transform. Once you get your head around it Transform is a very powerful and useful command.

L8r,


D4NM4N(Posted 2011) [#6]
I saw the demos, although if i am going to use code i want to know why :D

For example what warpy said above should be on the Mojo manual page for transform rather than a single line listing the cryptic parameters. I could have probably figured it out how transform works through trial and error but i should not have to (and i don't have the time to waste on sussing out stuff that should be explained in the documentation, which is what the actual commands -do-).

Some examples:
Function Transform
Syntax
Function Transform( ix#, iy#, jx#, jy#, tx#, ty# )

Parameters
ix, iy, jx, jy, tx, ty - matrix components.

Description
Multiplies the current matrix by the specified matrix.

Uhuh.
Function PopMatrix
Syntax
Function PopMatrix()

Description
Pops a matrix from the internal matrix stack and makes it the current matrix.

Um...
Function PushMatrix
Syntax
Function PushMatrix()

Description
Pushes the current matrix onto the internal matrix stack.



The matrix can be restored at a later time using PopMatrix.

right...


Basically I dont need to learn how to program, i just need a explainative API reference for mojo, which is something we do not seem to have.

Last edited 2011


Richard Betson(Posted 2011) [#7]
Well,

when I get a little time here I'll take a stab at working up a simple explanation of it's use. I'am hoping Warpy will beat me to it. For now check out these links on matrices:

http://www.sacredsoftware.net/tutorials/Matrices/Matrices.xhtml
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Matrix.html

Translate() really requires an understanding matrices before you can use it IMHO and that is not a simple subject to explain.

L8r,

Last edited 2011


D4NM4N(Posted 2011) [#8]
Thanks :)


Warpy(Posted 2011) [#9]
Richard - did you not see my post or do you think there's something else that needs explaining?

Because Monkey doesn't provide generic matrices for things other than screen transformations, I don't think there'd be much point in explaining 'proper' matrix maths.

Aaanyway, I've already done that, exactly two years ago.


Richard Betson(Posted 2011) [#10]
@Warpy,

Just wanted to pad-it-out a little:)

L8r,