package { /** * @author nicoptere */ import flash.display.*; public class Repell extends Force { static public var STIFFNESS:Number = .01; private var angle:Number; public function Repell( X:Number, Y:Number ) { super( X, Y ); color = 0xFF0000; } override public function equation( v:Vector ):void { if( ( v.x < x + R ) && ( v.x > x - R ) && ( v.y < y + R ) && ( v.y > y - R ) ){ var dx:Number; var dy:Number; var dist:Number; //check the distance to emmitter dx = x - v.x; dy = y - v.y; dist = Math.sqrt( dx*dx + dy*dy ); //if the distance is inferior to the radius if( dist < R ) { angle = Math.atan2( dy, dx ); //force adds its strength to alter the path v.vx += Math.cos( angle ) * -( R - dist ) * STIFFNESS; v.vy += Math.sin( angle ) * -( R - dist ) * STIFFNESS; } } } } }