Note that there are some explanatory texts on larger screens.

plurals
  1. POFlash Actionscript 3.0 : Number Type and Decimal Precision (Vector Fields and Particles)
    primarykey
    data
    text
    <p>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 <em>F(x,y)=yi-xj</em></p> <p>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.</p> <p>This code spawns the particles on the screen (which is 800x450). This code probably isn't in trouble, but for completeness I included it.</p> <pre><code>//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&lt;=800/spread; i++) { for (j=0; j&lt;=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); } } </code></pre> <p>This is the "dot" class which contains everything important about the particles being spawned.</p> <pre><code>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; } } } </code></pre> <p>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!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload