AS3 Offset rotation (0)

April 14th, 2009 by terrypaton, under Library.

An interesting problem to solve, when a movieclip is rotated around a registration point that is off centre, but you still want to rotate it around the centre, or even off centre the object. Strange explanation, but it’s useful when making games sometimes. The coloured dots are a very simplistic way for me to solve this, this can be coded a LOT better.

 
Download zip

stop ();
clip1.x = 240;
clip1.y = 60;
clip2.x = 240;
clip2.y =60;

var rot:Number = 45;
var centerDistance:Number = 60;
clip2.rotation = rot;

var rad:Number = rot/180*Math.PI;
var rad2:Number = -90/180*Math.PI;
var currentPoint:Point = new Point(Math.cos(rad)*centerDistance,Math.sin(rad)*centerDistance);
var basePoint:Point = new Point(Math.cos(rad2)*centerDistance,Math.sin(rad2)*centerDistance);

dot1.x = clip1.x-basePoint.x;
dot1.y= clip1.y-basePoint.y;
dot2.x = clip2.x-currentPoint.x;
dot2.y = clip2.y+currentPoint.y;

var xDif:Number = dot2.x – dot1.x
var yDif:Number = dot2.y – dot1.y
clip2.x = clip1.x-xDif
clip2.y= clip1.y-yDif

wrongclip1.x = 105
wrongclip2.x =105
wrongclip1.y = 60
wrongclip2.y = 60
wrongclip2.rotation = rot

dot1.x = clip1.x-basePoint.x;
dot1.y= clip1.y-basePoint.y;

Leave a Reply