Groups | Blog | Home
all groups > flash actionscript > october 2005 >

flash actionscript : Transform.matrix problem


VoidMainVoid
10/1/2005 6:05:19 PM
Hi everyone,

i'm having a problem with the transform property of a movieclip. I'm trying to
transform a triangle to another triangle using the transform.matrix property.
The script is as follows:


import flash.geom.Matrix;
import flash.geom.Transform;

obj = createEmptyMovieClip("triangle1", 2);
obj.beginFill(0x00FF00);
obj.moveTo(0, 0);
obj.lineStyle(1,0x0000FF,100,false,'none','none');
obj.lineTo(1, 0);
obj.lineStyle(1,0x00FF00,100,false,'none','none');
obj.lineTo(0, 1);
obj.lineStyle(1,0xFF0000,100,false,'none','none');
obj.endFill;

obj.onPress = function() {
var xn1 = 0;
var yn1 = 0;
var xn2 = 200;
var yn2 = 200;
var xn3 = 0;
var yn3 = 300;

var tmpMat:Matrix = new Matrix(xn2-xn1, xn3-xn1, yn2-yn1, yn3-yn1, xn1, yn1);
this.transform.matrix = tmpMat;
trace(this._xmouse+"---"+this._ymouse)
trace(_root._xmouse+"---"+_root._ymouse)

}

What's it all about? I draw a triangle with points (0,0) to (1,0) to (0,1). I
want to "map" (transform) it to the triangle (0,0)(200,200)(0,300). It is a
well known result that all triangle are affine i.e. there exist a unique affine
transform that send a triangle to another. In this case, the affine transform
is the transfrom.matrix of the movie clip. So, you have 6 variables (the
coordinates of the mapped triangle) and 6 unknown (the elements of the
transform matrix A,B,C,D,Tx,Ty). If you solve the system you get :

A=xn2-xn1
B=xn3-xn1
C=yn2-yn1
D=yn3-yn1
Tx=xn1
Ty=yn1

The problem is that the transform doesn't work correctly. If you run the
script, zoom in at the upper left corner and press on the original triangle,
the new transformed triangle is not what it's supossed to be. I'm running out
of ideas and i think that the "transform.matrix" does not work correctly (maybe
a bug?)...or am i doing something wrong here?
VoidMainVoid
10/2/2005 10:27:17 PM
After some snooping around. I've found out the answer. The transform.matrix
property (and the Matrix Class too) has a bug. Flash documentation defines the
matrix as:

[a b Tx
c d Ty
0 0 1]

While this should be the case, the matrix method instantiates a matrix with
swapped elements. The matrix that is being used is actually the following:

[a C Tx
B d Ty
0 0 1]

mind that elements b and c have swapped places. So, if you create a matrix, be
sure ( until macromedia actually fixes this) to swap those elements, e.g tmp=
new Matrix(a,C,B,d,Tx, Ty) and not Matrix(a,b,c,d,Tx,Ty) as the flash
documentation suggests.

Hope this will be helpfull guys. Cheers.
laura_shears
5/22/2006 5:28:49 PM
I've been trying to work with matrices also, but when I use the line:

import flash.geom.Matrix;

I get an error statement saying: "The class 'flash.geom.Matrix' could not be
loaded."

I am using Flash MX 2004 with ActionScript 2.0.

Is there something I need to download to get the class to load or do you have
to have the latest version of Flash (Flash 8) for this to work?

When I look in the help menu within my version of Flash, I find the Matrix
object, but there is no example on how to actually apply the transformations to
an object. You can view the help at: <a target=_blank
class=ftalternatingbarlinklarge
href="http://livedocs.macromedia.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common
/html/wwhelp.htm?context=Flash_MX_2004&file=00003467.html


I">http://livedocs.macromedia.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/ht
ml/wwhelp.htm?context=Flash_MX_2004&file=00003467.html

I</a> don't know how to apply this to a movie clip that I have on the stage.
abeall
5/22/2006 5:59:44 PM
That's a different matrix, used for "extending" the application Flash, not used in the SWF format.

laura_shears
5/23/2006 2:15:38 PM
Thanks, I thought that might be the case.

Any idea how to get the matrix in Flash MX 2004 to work on controling a
movieclip? I understand the idea of matrices and I know the mathematics behind
them. (I am a college math teacher.) I just don't know how to make the matrix
point to the movie clip that you want. (See the link to Macromedia's help
documentation on the subject in my earlier posting.)
AddThis Social Bookmark Button