Simple Mouse Avoiding AlgorithmFiled Under: Uncategorized
Today I’m gonna show you how to make a simple mouse avoiding algorithm. Check out the example below:

We’re gonna run quickly through the code:
package { import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; public class MouseAvoider extends Sprite { private const DISTANCE_DIVISOR:uint = 10; //see moveToDestination() private const FRICTION:Number= .9; private const FEAR_DISTANCE:Number= 150; //if mouse gets closer than this value sprite moves away private const MAX_AVOID_FORCE:uint = 10; //maximum length of avoid vector private var _destinationPoint:Point; //destination point private var _speed:Point = new Point(0, 0); //speed vector public function MouseAvoider():void { drawStuff(); } private function drawStuff():void { graphics.beginFill(Math.random() * 0xFFFFFF) graphics.drawCircle(0, 0, 10); graphics.endFill(); } public function set destinationPoint(value:Point):void { _destinationPoint = value; addEventListener(Event.ENTER_FRAME, move); } protected function move(e:Event):void { moveToDestination(); avoidMouse(); applyFriction(); updatePosition(); } private function moveToDestination():void { _speed.x += (_destinationPoint.x - x) / DISTANCE_DIVISOR; _speed.y += (_destinationPoint.y - y) / DISTANCE_DIVISOR; } private function avoidMouse():void{ var currentPosition:Point = new Point(x, y); var mousePosition:Point = new Point(stage.mouseX, stage.mouseY); var distance:uint = Point.distance(currentPosition, mousePosition); if (distance < FEAR_DISTANCE) { var force:Number = (1 - distance / FEAR_DISTANCE) * MAX_AVOID_FORCE; var gamma:Number = Math.atan2(-mousePosition.y + currentPosition.y, -mousePosition.x + currentPosition.x); var avoidVector:Point = Point.polar(force, gamma); _speed.x += avoidVector.x; _speed.y += avoidVector.y; } } private function applyFriction():void { _speed.x *= FRICTION; _speed.y *= FRICTION; } private function updatePosition():void{ x += _speed.x; y += _speed.y; } } }
_speed is a Point class instance in which we store speed vector components (the x-axis speed and the y-axis speed). I use Point because it has some interesing static functions, which might be helpful time-to-time
- drawStuff function draws a random color circle in our sprite.
- destinationPoint setter initalizes the algorithm and sets Event.ENTER_FRAME handler.
- move function launches one-by-one four functions:
- moveToDestination makes our object move to destinationPoint we previously set.
- avoidMouse - if distance between out object and mouse is less than FEAR_DISTANCE constant this function forces sprite to move away from pointer.
- applyFriction - multiplies speed vector by FRICTION, if we don’t apply this out sprite would act as a pendulum in no-friction enviroment (without this sprite would never stop)
- updatePosition adds speed vector to sprite position.
var force:Number = (1 - distance / FEAR_DISTANCE) * MAX_AVOID_FORCE; var gamma:Number = Math.atan2(-mousePosition.y + currentPosition.y, -mousePosition.x + currentPosition.x); var avoidVector:Point = Point.polar(force, gamma);
force is length of avoidVector. The closer the mouse, the larger the force.
Math.atan2() counts direction in radians of avoidVector
Point.polar() function given length and direction of vector returns a Point with it’s components. This is extremly useful for those, who don’t want to play with trigonometry.
Hope you gonna like it
Here’s the source of example.
- Permalink
- szataniol
- 7 Oct 2008 2:26 PM
- Comments (0)