Tag Archives: matrix

Matrix.transformPoint()

Actionscript:
  1. var circle:Shape = Shape(addChild(new Shape()));
  2. with(circle.graphics) beginFill(0x00000), drawCircle(0,0,10);
  3.  
  4. var point:Point = new Point();
  5. var trans:Matrix = new Matrix();
  6. trans.rotate(Math.PI);
  7. trans.scale(.5, .5);
  8. trans.tx = stage.stageWidth / 2;
  9. trans.ty = stage.stageHeight / 2;
  10.  
  11. addEventListener(Event.ENTER_FRAME, onLoop);
  12.  
  13. function onLoop(evt:Event):void {
  14.     point.x = mouseX - trans.tx;
  15.     point.y = mouseY - trans.ty;
  16.    
  17.     point = trans.transformPoint(point);
  18.    
  19.     circle.x = point.x;
  20.     circle.y = point.y;
  21. }

If you don't feel like rolling your own transformations Matrix.transformPoint() is a very powerful method. It simply applies the tranformations of a given matrix to a Point. The above example scales rotates and translates a point which is then used to position a circle Shape.

Matrix.transformPoint() is well suited for creating orbiting behavior:

Actionscript:
  1. var circle:Shape = Shape(addChild(new Shape()));
  2. with(circle.graphics) beginFill(0x00000), drawCircle(0,0,10);
  3.  
  4. var point:Point = new Point(100,0);
  5. var trans:Matrix = new Matrix();
  6.  trans.rotate(.1);
  7.  trans.scale(.99,.99);
  8.  
  9. addEventListener(Event.ENTER_FRAME, onLoop);
  10.  
  11. function onLoop(evt:Event):void {
  12.      
  13.       point = trans.transformPoint(point);
  14.      
  15.       circle.x = point.x + stage.stageWidth / 2;
  16.       circle.y = point.y + stage.stageHeight / 2;
  17. }

This code will cause the circle Shape to move in a spiral formation.

Posted in misc, motion | Also tagged , | Leave a comment