In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

obstacle avoidance test

idea from AI Game Programming Wisdom 2
still imperfect
Get Adobe Flash player
by codeonwort 06 Oct 2011

    Talk

    gameegg at 11 Oct 2011 16:35
    오른쪽으로만 도네요~_~ㅋ
    Embed
/**
 * Copyright codeonwort ( http://wonderfl.net/user/codeonwort )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/b7q7z
 */

package {
    import flash.events.Event
    import flash.display.Shape
    import flash.display.Sprite
    
    public class AvoidTest extends Sprite {
        
        private var a:Circle, b:Circle
        private var mark:Circle
        
        public function AvoidTest() {
            a = new Circle(20, 0xff0000)
            a.x = 100 ; a.y = 100
            addChild(a)
            
            b = new Circle(40, 0x0000ff)
            b.x = 250 ; b.y = 220
            addChild(b)
            
            mark = new Circle(5, 0x00ff00)
            mark.x = tx
            mark.y = ty
            addChild(mark)
            
            addEventListener("enterFrame", yesitsjustaloooooooooooooop)
            stage.addEventListener("mouseDown", setTargetPoint)
        }
        
        private var tx:Number = 400, ty:Number = 300
        private function yesitsjustaloooooooooooooop(e:Event):void {
            if(Math.pow(ty - a.y, 2) + Math.pow(tx - a.x, 2) < 16){
                return
            }
            graphics.clear()
            // step 1
            setInitDirection(a, tx, ty)
            // step 2
            adjustDirection(a, b)
            // step 3
            a.vel.normalize(7)
            a.move()
        }
        
        private function setInitDirection(player:Circle, tx:Number, ty:Number):void {
            player.vel.x = tx - player.x
            player.vel.y = ty - player.y
            player.vel.normalize(1)
            
            graphics.lineStyle(2, 0x0)
            graphics.moveTo(player.x, player.y)
            graphics.lineTo(player.x + player.vel.x * 50, player.y + player.vel.y * 50)
        }
        
        private function adjustDirection(player:Circle, obstacle:Circle):void {
            var imp:vec2 = new vec2(player.x - obstacle.x, player.y - obstacle.y)
            imp.normalize(1)
            
            
            graphics.lineStyle(2, 0xff)
            graphics.moveTo(player.x, player.y)
            graphics.lineTo(player.x + imp.x * 50, player.y + imp.y * 50)
            
            var del:vec2 = new vec2(imp.x - player.vel.x, imp.y - player.vel.y)
            if(del.length <= 1.414) return
            
            imp = imp.rotate()
            
            graphics.lineStyle(2, 0xff0000)
            graphics.moveTo(player.x, player.y)
            graphics.lineTo(player.x + imp.x * 50, player.y + imp.y * 50)
            
            a.vel.x += imp.x
            a.vel.y += imp.y
            a.vel.normalize(1)
        }
        
        private function setTargetPoint(e:Event):void {
            tx = mouseX ; ty = mouseY
            mark.x = tx ; mark.y = ty
        }
        
    }
    
}

import flash.display.Shape
class Circle extends Shape {
    public var radius:Number
    public var vel:vec2
    public function Circle(radius:Number, color:uint) {
        graphics.beginFill(color)
        graphics.drawCircle(0, 0, radius)
        this.radius = radius
        vel = new vec2
    }
    public function move():void {
        x += vel.x
        y += vel.y
    }
}

import flash.geom.Point
class vec2 extends Point {
    public function vec2(x0:Number=0, y0:Number=0) { super(x0, y0) }
    public function rotate():vec2 {
        return new vec2(-y, x)
    }
}