mike's Banner
mike

Age/Gender: 24, Male
Job: stupids

hello!

Newgrounds Stats

Sign-Up Date:
2/24/00

Level: 20
Aura: Neutral

Rank: Police Lieutenant
Blams: 1,161
Saves: 811
Rank #: 3,423

Whistle Status: Normal

Exp. Points: 4,264 / 4,440
Exp. Rank #: 5,211
Voting Pow.: 6.20 votes

BBS Posts: 382 (0.11 per day)
Flash Reviews: 44
Music Reviews: 0
Trophies: 3
Stickers: 0

Entry #14

Jump to Entry: [ 1611 | 12 | 13 | 14 | 15 | 16 | 171921 ]


mike

_xscale & _yscale

Posted by mike Jul. 12, 2008 @ 3:10 PM EDT

In Flash, throw a movie clip on the stage and give it an instance name of "clip". Go to Modify -> Transform -> Flip Horizontal. Then do a trace(clip._xscale); 100 is traced, even though you might expect -100. What gives?

Short Answer:
Flipping horizontally is the same as flipping vertically and rotating 180 degrees. This means that _xscale and _yscale can either be positive or negative. Flash can't really tell which you've done, so it assumes positive.

Long Answer:
The position info of every graphic in Flash is stored as a 3x3 homogeneous matrix:

[[ a b tx ]
[ c d ty ]
[ 0 0 1 ]]

This is a combination of a scale, rotation, skew, and translation.

A scale transformation looks like:
[[ x 0 0 ]
[ 0 y 0 ]
[ 0 0 1 ]]
where x and y are the amount of scaling on the x and y axis. Note that _xscale = x*100

A rotation transform is:
[[ cos(t) sin(t) 0 ]
[ -sin(t) cos(t) 0 ]
[ 0 0 1 ]]
where t is the amount of rotation.

Combine these by matrix multiplication, and you get:

[[ x*cos(t) x*sin(t) 0 ]
[ -y*sin(t) y*cos(t) 0 ]
[ 0 0 1 ]]

Flash only knows the final values (a, b, c, d). We know a = x*cos(t) and b = x*sin(t). You can start to see the sign ambiguity appear already -- whenever you multiply two values, you lose some information about the signs of those values.

To calculate _xscale, Flash has to solve for x. Your first thought might be x = a/cos(t), but that requires you to find out t. A more elegant way to solve this is to realize that the ( x*cos(t), x*sin(t) ) in the first row is just a vector x units long, in the direction of t. So x is just the length of the vector:

x = sqrt( a^2 + b^2 )

This makes sense intuitively, too -- because a scale transformation just scales a basis vector, (1, 0), by x. Rotation changes this more, but it doesn't affect vector length! So getting the magnitude of this vector tells you the amount of scaling.

You can chug through the math to see that this identity holds:
sqrt( a^2 + b^2 ) = sqrt( (x * cos(t))^2 + (x * sin(t))^2 )
= sqrt( x^2 * cos^2(t) + x^2 * sin^2(t) )
= sqrt( x^2 * ( cos^2(t) + sin^2(t) ) )
= sqrt( x^2 * 1 );
= +/- x

Since _xscale and _yscale are the result of a square root, we get two solutions, either + or -.

When you do _xscale = -100 in ActionScript, why does Flash trace(_xscale) as -100 then? Because whenever you set _rotation, _xscale, _yscale, Flash actually caches the value you passed and returns that, instead of wastefully recalculating it!

Updated: 02/12/09 2:31 PM Log in to comment! | Share this!

The People Have Spoken

42 Comments

Jul. 12, 2008 | 3:44 PM KupaMan says:

Whoa, that just melted my mind.


Jul. 12, 2008 | 4:13 PM Phil says:

My mind blew up. x(


Jul. 12, 2008 | 4:34 PM doberman7 says:

is this basically an easier process in flash then or. . . .?


Jul. 12, 2008 | 5:16 PM Kart-Man says:

...Has anyone ever told you that you should become a math teacher?


Jul. 12, 2008 | 5:33 PM Wingle-Flotz says:

I have NO idea what you just said, but i'm gonna try and figure it out. Or, my brain'll shrivel up and die in the process. That was a useless comment, wasn't it?


Jul. 12, 2008 | 7:07 PM TjA says:

thats pretty smart how flash does that, how did you find that out?


Jul. 12, 2008 | 7:13 PM Atgod88 says:

What do you think I am, a freakin' rocket surgeon?


Jul. 12, 2008 | 10:02 PM Wholyish says:

wow... that actually made sense to me, though ive never dealt with the scenario. thanks for that, ill keep it in mind. (you might want to explain the matrices and trig a little more for the typical newgroundser)


Jul. 13, 2008 | 12:40 AM Wiiporter says:

*pop*


Jul. 13, 2008 | 12:48 AM Matt475 says:

Yeah I understood that


Jul. 13, 2008 | 1:19 AM Sirkowski says:

...and that's why I only make drawings. x_X


Jul. 13, 2008 | 1:45 AM Kidlazarus says:

I understand the mathematical lingo but you lost me in the ActionScript...


Jul. 13, 2008 | 3:25 AM WolfBlitz2 says:

I think my brain broke


Jul. 13, 2008 | 6:44 AM Drift-And-Die says:

what


Jul. 13, 2008 | 7:34 AM The-Super-Flash-Bros says:

Flash is smart sometimes, but sometime not so helpful. You can see exactly how it stores the transform if you have the transform window open - type in -100% for width, then reselect the clip, you'll see it actually counts as a skew of 180 degrees

Try this one! Its a problem with AS3, I'm not sure if it used to do it in AS2. Bear in mind that height and width of a movieclip are always relative to its parent's x/y axis, not the clips own internal axis (unlike xScale and yScale)

Add a 100x100 square MovieClip to the timeline, call it mc and add this to the stage timeline:

mc.rotation = 90;
mc.width = 50;
trace(mc.width);
trace(mc.height);

You'll see that it completely scews up which way it scales! then try this:

mc.rotation = 90;
mc.width = 50;
mc.width = 50;
trace(mc.width);
trace(mc.height);

It seems like the first time it uses width (same goes for height) it hasn't updated the rotation on the clip, but the second time it has! Crazy Flash...

Tom-


Jul. 13, 2008 | 9:28 AM vaifan90 says:

I understood the main message, but I've never used a flash program. It'd be freaking awesome to have the skills of Adam Phillips, Michael Swain or Jeff Bandelin, but sadly I know not one thing about it and I'm afraid of these mathematical bullshit you seem to have to go through. I hate math. :|

Any advice?
xx Martin xx


Jul. 13, 2008 | 9:29 AM ace210493 says:

i understand the maths
but i fell swamped out by actionscript


Jul. 13, 2008 | 12:53 PM JonBro says:

I have more problems with _height and _width acting as positive when I try making them negative than I have with _xscale and _yscale.


Jul. 13, 2008 | 2:04 PM UberCream says:

I think my brain just popped... Painfully...


Jul. 13, 2008 | 2:25 PM I-smel says:

I think the point of this is:

Flash uses a square root function to flip things horizontally or vertically
the square root of any number is allways positive
so flipping something will allways give you a positive _xscale

I could be wrong there. Well done for finding out though.


Jul. 13, 2008 | 2:42 PM Paranoia says:

Hehe, you certainly learn new stuff from your posts. I always wondered what that third column was for... Thanks for that link :P


Jul. 13, 2008 | 3:02 PM Weirdo says:

All I know is that Flash applies trigonometry into it's programming. Except the plane is upside down and I think backwards.


Jul. 13, 2008 | 3:12 PM MindChamber says:

my girlfriend_flipped when I X_scaled into her mouth


Jul. 13, 2008 | 3:26 PM super-sense says:

God I wish I understood action script. The only thing vaguely familiar to me is cos and sin. And it's been ages since i've done that.


Jul. 13, 2008 | 3:42 PM Vincoid says:

My brain was about to die out, but Mindchamber's comment fixed that problem :P


Jul. 13, 2008 | 4:13 PM mongoid says:

Fascinating. Where did you learn this? Adobe forums?


Jul. 13, 2008 | 4:36 PM SecretClock says:

0.99999 (repeating) = 1 because

1/3 = 0.33333333333333333333333

1/3 X 3=1 or 1/3 X 3= 0.9999999999999999999999999999999


Jul. 13, 2008 | 4:51 PM SecretClock says:

Steven Hawking could explain better!

http://spamtheweb.com/ul/upload/13070 8/60172_bob2.mp3


Jul. 13, 2008 | 4:58 PM gankro says:

I never liked trig anyway... I can code just about anything without looking something up except when trig comes up... I really shoulda paid better attention during that unit :/ ... Also: why does flash not have an _xskew/_yskew option yet?


Jul. 13, 2008 | 5:23 PM Listen2Reason says:

Heh, interesting stuff! I didn't know Flash was actually using a complete 3x3 transformation matrix, but now that I do it all makes sense. ;)


Jul. 13, 2008 | 6:05 PM Junky2005 says:

lolwut?


Jul. 13, 2008 | 8:22 PM KingKombat says:

..what the fu--?
*head explodes all over computer*


Jul. 14, 2008 | 12:10 AM P-I-N says:

Great, someone give him a Nobel!!!

¬¬ I accept it; Impressing for teenagers.


Jul. 14, 2008 | 12:52 AM ageofthoughts says:

I cannot comprehend what the fuck you just said back there...How did you get all this down in your head?


Jul. 14, 2008 | 3:24 AM Nyubis says:

BOOOOOOMM!!!!
That was my head when reading the second part.
I'll stick to the short version...


Jul. 14, 2008 | 4:34 AM Djmr says:

When you flip the x axis in flash that way, the xscale is 100 since that is the new shape's scale. If you wanted to flip it in an .swf, flash records the old position, so that a scale of 100 would return the object to scale.

Flipping horizontally is different than changing the _xscale. Although it would do the same thing, these are two seperate functions. Its more common sense than having to look at the math of it, although I did understand that dreadfully, hated learning that stuff last year.


Jul. 14, 2008 | 2:15 PM Alexman159 says:

Thats wierd... Are you using flash CS3 because i'm pretty sure that flash cs3 was supposed to be the ultimate calculating system. (and you would think it wouldn't do something like that.)


Jul. 14, 2008 | 8:56 PM Ghostdemon says:

If my mind was a Nuclear Power Plant,then you just set off another "Chernobyl"


Jul. 15, 2008 | 5:52 AM 0497335119 says:

Ok , the mathematics are ok , but the actionscript are problematic :p


Jul. 15, 2008 | 9:42 PM mranarchy says:

Conclusion: I suck


Jul. 16, 2008 | 8:53 AM jamesb1995 says:

AAAAAAAAAGGGH!! MY BRAIN EXPLODED!!!! AGGHHGHHH (foams at the mouth) *gag *blurb


Aug. 17, 2008 | 5:05 PM BentRainbows says:

What.

Jump to Entry: [ 1611 | 12 | 13 | 14 | 15 | 16 | 171921 ]