Flash Actionscript 3.0:数字类型和小数精度(矢量场和粒子)
我正在 Flash 中对矢量场进行建模,并生成一堆粒子来可视化场的流动。使用向量场F(x,y)=yi-xj
这个向量场有一个旋度,粒子将按其圆周运动。我的问题是粒子从原点发散,尽管这个特定的矢量场没有发散。我怀疑我的数据类型可能会在该字段的基本计算过程中丢失小数精度,或者我可能犯了一些我不确定的逻辑错误。
此代码在屏幕(800x450)上生成粒子。这段代码可能没有问题,但为了完整性我将其包含在内。
//spawn particles
var i:int;
var j:int;
//spread is the spacing between particles
var spread:Number;
spread = 10.0;
//spawn the particles
for (i=0; i<=800/spread; i++)
{
for (j=0; j<=450/spread; j++)
{
//computes the particles position and then constructs the particle.
var iPos:Number = spread * Number(i) - 400.0;
var jPos:Number = 225.0 - spread * Number(j);
var particle:dot = new dot(iPos,jPos,10.0);
addChild(particle);
}
}
这是“点”类,其中包含有关所生成的粒子的所有重要信息。
package
{
//import stuff
import flash.display.MovieClip;
import flash.events.Event;
public class dot extends MovieClip
{
//variables
private var xPos:Number;
private var yPos:Number;
private var xVel:Number;
private var yVel:Number;
private var mass:Number;
//constructor
public function dot(xPos:Number, yPos:Number, mass:Number)
{
//Defines the function to be called when the stage advances a frame.
this.addEventListener(Event.ENTER_FRAME, moveDot);
//Sets variables from the constructor's arguments.
this.xPos = xPos;
this.yPos = yPos;
this.mass = mass;
//Set these equal to 0.0 so the Number type knows I want a decimal (hopefully).
xVel = 0.0;
yVel = 0.0;
}
//Controlls the particle's behavior when the stage advances a frame.
private function moveDot(event:Event)
{
//The vector field is a force field. F=ma, so we add F/m to the velocity. The mass acts as a time dampener.
xVel += yPos / mass;
yVel += - xPos / mass;
//Add the velocity to the cartesian coordinates.
xPos += xVel;
yPos += yVel;
//Convert the cartesian coordinates to the stage's native coordinates.
this.x = xPos + 400.0;
this.y = 225.0 + yPos;
}
}
}
理想情况下,粒子将永远绕着原点做圆周运动。但代码创建了一种情况,粒子围绕原点旋转并向外螺旋,最终离开舞台。我真的很感激您的帮助。 谢谢!
I'm modeling a vector field in flash and spawning a mess of particles to visualize the flow of the field. Using the vector field F(x,y)=yi-xj
This vector field has a curl, the particles are to move in circles which they do. My problem is that the particles diverge from the origin, though this particular vector field has no divergence. I suspect that my data types may be losing decimal precision during the very basic caluclations for this field or perhaps I'm making some logic mistake I am unsure.
This code spawns the particles on the screen (which is 800x450). This code probably isn't in trouble, but for completeness I included it.
//spawn particles
var i:int;
var j:int;
//spread is the spacing between particles
var spread:Number;
spread = 10.0;
//spawn the particles
for (i=0; i<=800/spread; i++)
{
for (j=0; j<=450/spread; j++)
{
//computes the particles position and then constructs the particle.
var iPos:Number = spread * Number(i) - 400.0;
var jPos:Number = 225.0 - spread * Number(j);
var particle:dot = new dot(iPos,jPos,10.0);
addChild(particle);
}
}
This is the "dot" class which contains everything important about the particles being spawned.
package
{
//import stuff
import flash.display.MovieClip;
import flash.events.Event;
public class dot extends MovieClip
{
//variables
private var xPos:Number;
private var yPos:Number;
private var xVel:Number;
private var yVel:Number;
private var mass:Number;
//constructor
public function dot(xPos:Number, yPos:Number, mass:Number)
{
//Defines the function to be called when the stage advances a frame.
this.addEventListener(Event.ENTER_FRAME, moveDot);
//Sets variables from the constructor's arguments.
this.xPos = xPos;
this.yPos = yPos;
this.mass = mass;
//Set these equal to 0.0 so the Number type knows I want a decimal (hopefully).
xVel = 0.0;
yVel = 0.0;
}
//Controlls the particle's behavior when the stage advances a frame.
private function moveDot(event:Event)
{
//The vector field is a force field. F=ma, so we add F/m to the velocity. The mass acts as a time dampener.
xVel += yPos / mass;
yVel += - xPos / mass;
//Add the velocity to the cartesian coordinates.
xPos += xVel;
yPos += yVel;
//Convert the cartesian coordinates to the stage's native coordinates.
this.x = xPos + 400.0;
this.y = 225.0 + yPos;
}
}
}
Ideally the particles would all move in circles about the origin forever. But the code creates a situation where the particles rotate around the origin and spiral outwards, eventually leaving the stage. I'd really appreciate a helping hand.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以标准化每个粒子距原点的距离(也许不是在每个步骤上以节省计算)。另外,您的代码似乎没有优化 - 您正在为每个粒子创建 ENTER_FRAME 侦听器,并且有 3600 个粒子。一名听众就足够了。我会将所有这些除法改为乘以逆值。
You can normalize each particle's distance from origin (maybe not on each step to save calculations). Also it seems your code is not optimized - you are creating ENTER_FRAME listener for every particle, and there is 3600 of them. One listener should be enough. And I would change all these divisions to multiplication by inverse value.
要设置小数位,请使用 toFixed() 函数。
希望有帮助
To set decimal places use the toFixed() function.
Hope it helps