Flash ActionScript 3.0 : Number Type and Decimal Precision (Vector Fields and Particles)

标签 flash actionscript-3 types field particles

我在 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;
  }
 }
}

理想情况下,粒子将永远围绕原点做圆周运动。但是代码创建了一种情况,即粒子围绕原点旋转并向外螺旋,最终离开舞台。我非常感谢您的帮助。
谢谢!

最佳答案

您可以标准化每个粒子与原点的距离(可能不是在每个步骤上以保存计算)。此外,您的代码似乎没有优化 - 您正在为每个粒子创建 ENTER_FRAME 监听器,其中有 3600 个。一个听众就足够了。我会将所有这些除法更改为乘以逆值。

关于Flash ActionScript 3.0 : Number Type and Decimal Precision (Vector Fields and Particles),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4159961/

相关文章:

c++ - C++ 中的奇怪类型

actionscript-3 - 在 AS3 中,将文本文件加载到字符串中

google-chrome - Chrome ://plugins URL no longer works

apache-flex - 逐个比较 AS3 像素中的位图数据

javascript - HTML5 flash canva 无法通过 "mouseout"函数停止我的声音

actionscript-3 - Adobe AIR 应用程序音频

actionscript-3 - ActionScript 3 onMetaData 视频持续时间 - 无法存储值

MySQL:存储长笔记的最有效数据类型?

c++ - 类是保存此数据 C++ 的最佳方式吗?

flash - 用于通过 Java2D 呈现矢量图形格式的开源库?