3

ベクター フィールドをフラッシュでモデリングし、パーティクルの混乱を生成してフィールドの流れを視覚化しています。ベクトル場F(x,y)=yi-xj の使用

このベクトル フィールドにはカールがあり、粒子は円を描くように移動します。私の問題は、この特定のベクトル場には発散がありませんが、粒子が原点から発散することです。このフィールドの非常に基本的な計算中に、データ型の 10 進数の精度が失われている可能性があると思われます。または、不確かな論理ミスを犯している可能性があります。

このコードは、画面 (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;
  }
 }
}

理想的には、パーティクルはすべて、原点を中心に永遠に円を描いて移動します。しかし、このコードは、パーティクルが原点を中心に回転し、外側にらせん状になり、最終的にステージを離れる状況を作り出します。助けていただければ幸いです。ありがとう!

4

2 に答える 2

1

原点からの各パーティクルの距離を正規化できます(計算を保存するために、各ステップではない場合があります)。また、コードが最適化されていないようです。すべてのパーティクルに対してENTER_FRAMEリスナーを作成しており、3600個あります。1人のリスナーで十分です。そして、これらすべての除算を逆値による乗算に変更します。

于 2010-11-12T07:40:12.113 に答える
0

小数点以下の桁数を設定するには、toFixed() 関数を使用します。

//Set these equal to 0.0 so the Number type knows I want a decimal (hopefully).
 xVel = 0;
 yVel = 0;
 xVel.toFixed(1); //(specifies decimal places)
 yVel.toFixed(1);

それが役に立てば幸い

于 2011-02-09T18:48:52.393 に答える