flash on 2012-11-12
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cFsa
*/
package {
import flash.text.TextField;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
deb = new TextField();
addChild(deb);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var px:Number = 100;
public var py:Number = 100;
public var deb:TextField;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2,0);
var ax:Number = 200;
var ay:Number = 200;
var ar:Number = 64;
graphics.drawCircle(ax, ay, ar);
// px = 200;
//py = 200;
var mx:Number = 200;
var my:Number = 200;
mx = px;
my = py;
mx = mouseX;
my = mouseY;
var t:Number;
var kx:Number;
var ky:Number;
graphics.moveTo(px,py);
graphics.lineTo(mx,my);
t =0;
t = lineCirc3(px,py,mx,my,ax,ay,ar);
// if (t == 999) { t= 1;}
deb.text = "t: " + t;
if (t > 0 && t < 1)
{
kx = px + (mx - px) * t;
ky = py + (my - py) * t;
}
else
{
kx = mx;
ky = my;
}
graphics.drawCircle(kx,ky, 12);
var dx:Number;
var dy:Number;
var mag:Number;
dx = kx - ax;
dy = ky - ay;
mag = Math.sqrt(dx*dx+dy*dy);
if (mag == 0) { mag = 0.00001;}
dx /= mag;
dy /= mag;
// deb.text = "mag " + mag;
if (mag <= (ar+8))
{
px = kx +((ar+8)-mag) * dx;
py = ky+((ar+8)-mag) * dy;
}
else
{
px = kx;
py = ky;
}
//px += (mx-px)*0.1;
// py += (my-py)*0.1;
graphics.drawCircle(px,py, 4);
}//onenter
//based on
//http://www.geometrictools.com/SamplePhysics/CollisionsMovingSphereTriangle/RTSphereTriangle.cpp
public function lineCirc3(
ax:Number, ay:Number, bx:Number, by:Number,
cx:Number, cy:Number, rad:Number):Number
{
if (ax == bx && ay == by) { return 0; }
var rsq:Number
var vx:Number;
var vy:Number;
var mx:Number;
var my:Number;
var q2:Number;
var q1:Number;
var q0:Number;
var disc:Number;
rsq = rad * rad;
mx = cx - ax;
my = cy - ay;
vx = ax - bx;
vy = ay - by;
q2 = (vx*vx +vy*vy);
q1 = (vx*mx + vy*my);
q0 = (mx*mx + my*my) - rsq;
disc = q1*q1 - q0 * q2;
if (disc < 0) { return 999; }
var invq2:Number;
var rootdisc:Number;
var root0:Number;
var root1:Number;
// deb.text = "v " + ax;
invq2 = 1 / q2;
rootdisc = Math.sqrt(disc);
root0 = (-q1 - rootdisc) * invq2;
root1 = (-q1 + rootdisc) * invq2;
return root0;
}//linecirc3
}//classend
}