PDA

View Full Version : Issue with Point.polar() method?



QuickQuestion
June 8th, 2009, 07:22 PM
I am attempting to move movieclips 1 pixel by a given angle using the Point.polar() method. I am under the impression that moving a movieclip 1 pixel by a certain angle and then increasing that angle by 1 degree 360 times in a row should result in the movieclip returning to its original position. I am finding, however, that the movieclip ends at (191.20000000000002, 191.20000000000002) if its original point was (200, 200).

I must have missed something, and would love to understand this method properly. Any interest in my question is greatly appreciated.

This code takes for granted the existence of a movieclip "libraryItem" in the library.


stop();
import fl.motion.MatrixTransformer;
import flash.geom.*

var visibleGraphic:libraryItem = new libraryItem;
visibleGraphic.x = 200, visibleGraphic.y = 200;
var movementTimer:Timer = new Timer (25, 360);
var movementAngle:Number = 0;

this.addChild( visibleGraphic );

movementTimer.addEventListener(TimerEvent.TIMER, movementFunction);
movementTimer.start();

function movementFunction(event:Event):void {
var movementMatrix = visibleGraphic.transform.matrix;
var destinationPoint:Point = Point.polar(1, 2 * Math.PI * (movementAngle / 360));
movementMatrix.translate(destinationPoint.x, destinationPoint.y);
visibleGraphic.transform.matrix = movementMatrix;
movementAngle += 1;
trace(visibleGraphic.x, visibleGraphic.y);
}

creatify
June 8th, 2009, 09:28 PM
I'm not going to be the one able of going into all the technical details, but I'm pretty sure this is due to the way that floating point numbers are working within the translate method in Flash, and possibly a combination of how the object is rendered due to pixel snapping or antialiasing type items...

see these threads:
http://www.kirupa.com/forum/showthread.php?t=322132

http://www.kirupa.com/forum/showthread.php?t=289109

QuickQuestion
June 8th, 2009, 09:28 PM
I have been made aware of the inherent floating-point errors that I am dealing with, and have implemented the standard function to work around this limitation. However, it does not seem to be actually affecting the values I am passing to that function. The overall effect after applying the correctFloatingPointError() function each time is exactly the same.

Again, any help is greatly appreciated.


stop();
import fl.motion.MatrixTransformer;
import flash.geom.*

var visibleGraphic:libraryItem = new libraryItem;
visibleGraphic.x = 200, visibleGraphic.y = 200;
var movementTimer:Timer = new Timer (25, 360);
var movementAngle:Number = 0;

this.addChild( visibleGraphic );

movementTimer.addEventListener(TimerEvent.TIMER, movementFunction);
movementTimer.start();

function movementFunction(event:Event):void {
var movementMatrix = visibleGraphic.transform.matrix;
var destinationPoint:Point = Point.polar(1, 2 * Math.PI * (movementAngle / 360));
correctFloatingPointError(destinationPoint.x);
trace(destinationPoint.x);
correctFloatingPointError(destinationPoint.y);
trace(destinationPoint.y);
movementMatrix.translate(destinationPoint.x, destinationPoint.y);
visibleGraphic.transform.matrix = movementMatrix;
movementAngle += 1;
trace(visibleGraphic.x, visibleGraphic.y);
}

function correctFloatingPointError(number:Number, precision:int = 5):Number {
var correction:Number = Math.pow(10, precision);
return Math.round(correction * number) / correction;
}

QuickQuestion
June 8th, 2009, 09:32 PM
Thanks for your response and the info. Seems I was posting just as you were. I'll get to those links and see what I can turn up.

Thanks again.