Search code examples
actionscript-3simulationphysicsparticle-system

Can't stop the particles from overshooting


I am trying to create a simple particle simulation. There are two types of particles static and moving. Static particles attract moving particles towards their centre. Static particles have a strength attribute which dictates how hard they are pulling the moving particles

var angle:Number = Math.atan2(moving.y - static.y , moving.x - static.x);
var dist = Point.distance(new Point(moving.x,moving.y) , new Point(static.x,static.y));

moving.velX += Math.cos(angle + Math.PI) * static.strength / dist;
moving.velY += Math.sin(angle + Math.PI) * static.strength / dist;

The problem is a when a particle is just passing through the centre the distance is very small that results in very large velocity values.

I added an extra check for distance before calculating velocity.

if (dist < 1)
    dist = 1;

But the problem still persists. I cant figure out the problem.

Here is a snapshot of an overshoot happening.

enter image description here


Solution

  • Normal force fields use square of distance as modifier, you here use single power of distance, of course the force field performs differently. You should change the var dist line to the following:

    var dist:Number = (moving.x-static.x)*(moving.x-static.x) + (moving.y-static.y)*(moving.y-static.y);
    

    This way dist will hold square of actual distance, so dividing by dist would give you the proper force field configuration.

    And, please rename static, as it's a reserved word in AS3.