PDA

View Full Version : Advanced coordinate rotation is't too accurate


yyy98
06-10-2008, 07:33 AM
In Chapter 10 of “Making Things Move!”, I have learned how to use advanced coordinate rotation in my Script, but I found that it has a little inaccurate. In the file of Rotate3.as, I changed the random arrangement to linear arrange with 45 degrees, and set the ball’s size a little smaller, here’s the full document class(Rotate3.as):
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.StageScaleMode;
import flash.display.StageAlign;

public class Rotate3 extends Sprite
{
private var balls:Array;
private var numBalls:uint = 10;
private var vr:Number = .05;

public function Rotate3()
{
init();
}

private function init():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
balls = new Array();
for(var i:uint = 0; i < numBalls; i++)
{
var ball:Ball = new Ball(10);
balls.push(ball);
addChild(ball);
ball.x = stage.stageWidth / 2 + (i + 1) * 15
ball.y = stage.stageHeight / 2 + (i + 1) * 15

}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(event:Event):void
{
var angle:Number = (mouseX - stage.stageWidth / 2) * .01;
var cos:Number = Math.cos(angle);
var sin:Number = Math.sin(angle);
for(var i:uint = 0; i < numBalls; i++)
{
var ball:Ball = balls[i];
var x1:Number = ball.x - stage.stageWidth / 2;
var y1:Number = ball.y - stage.stageHeight / 2;
var x2:Number = cos * x1 - sin * y1;
var y2:Number = cos * y1 + sin * x1;
ball.x = stage.stageWidth / 2 + x2;
ball.y = stage.stageHeight / 2 + y2;
}
}
}
}


Ball Class:
package {
import flash.display.Sprite;
public class Ball extends Sprite {
public var radius:Number;
private var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public function Ball(radius:Number=40, color:uint=0xff0000) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}

I make the rotation speed higher, test this example. Put your mouse to the right side of the stage. Two minutes later, you’ll find the position of those balls getting a little offset. If you continue to test it, the “offset-situation” would be more and more. Why advanced coordinate rotation has a little inaccurate somehow? Thanks.

yyy98
06-11-2008, 02:29 AM
The method is not inaccurate. But the computer is. It stores numbers as 32-bit values. This is pretty good, but tiny errors will build up over time to be something you notice.


For something like what you did, coordinate rotation would not be the best method anyway. since all the balls are in a line, just change the angle of that line and move the balls out along that angle. Then they will always be in alignment.


Keith Peters

ASWC
06-11-2008, 09:41 AM
What is that????:o