[osflash] drift problem rotating an object around an arbitrary axis

David Holroyd osflash at badgers-in-foil.co.uk
Thu Feb 15 03:47:42 EST 2007


On Wed, Feb 14, 2007 at 04:54:30PM -0800, larry kirschner wrote:
> This approach seems to work, except that on each rotation the object
> slightly off the axis.

It looks like you are updating the transform every iteration by rotating
adding a rotation by the same angle each time, yes..?

>            private function axisRotate(obj:DisplayObject, axis:Point,
> angle:Number):void
>            {
>                validateAxisLen(axis);
> 
>                var temp:Matrix = obj.transform.matrix;
> 
>                var pivotX:Number = obj.x + axis.x;
>                var pivotY:Number = obj.y + axis.y;
> 
>                temp.translate(0 - pivotX, 0 - pivotY);
> 
>                temp.rotate(angle);
> 
>                temp.translate(pivotX, pivotY);
> 
>                obj.transform.matrix = temp;
>            }

If I've read the code correctly, this is your problem.  Rounding errors
will build up in these calculations over time and cause problems like
those you've seen (object gradually heading towards (0,0) etc.)

Normal practice is to 'set' rather than 'update' the transform on every
frame, so at the top of the above method, you should reset the transform
to the identity-metrix.  Then you just need to arrange that the 'angle'
value changes over time, i.e. it doesn't represent the change per frame
any more, but the absolute rotation for the given point in time.


Hope that helps, and that I understood your code correctly!
dave

-- 
http://david.holroyd.me.uk/



More information about the osflash mailing list