Requires base math.
Well, here you will detect if a line is hitting a circle or not. This is done in the
following way:
1)finding the center of the circle
2)calculating the circle's radius
3)calculating the line's equation
4)finding the distance between the line and the center
5)comparing the distance between the line and the circle's center with the circle's radius
Step 1
This is obviously the easiest part by far. The center of a circle is always it's width
devided by two and it's height devided by two. so assuming your circle is a movieclip (which
it should be) all you have to do is type
var cx:Number=(this._x+(this._width+this._x)) /2;
var cy:Number=(this._y+ (this._y+this._height) /2;
Step 2
Finding the radius of a circle is not much harder, the radius is just the diameter devided
by two, and the diameter is the width or the height of the circle (since they are
identical).
var rad:Number=this._width/2;
Step 3
Finding the line's equation. Now in case you don't have the line's equation you can simply
calculate it. calculating line eqations is explained in detail in this thread
a line is ususally in the y=mx+n or the ax+by+c=0 format, I like the first type better
You know a line's increasment is the reduction of it's y devided by it's x reduction, you
can just directly use _width and _height for that
the following is in the line's movieclip
var m:Number=_width/_height;
now to calculate the offset (n value) use one point you know the line has, it's _x and _y.
now y=mx+n so 0=mx+n-y so -n=mx-y so n=y-mx
var n:Number=y - (m*_x);
this is your offset and you have your line's equation since you know M and N.
Step 4
Here comes the harder part, you need to calculate the distance between the center x and
center y and the line.
the formula for doing so is |ax+by+c| / sqrt(a^2+b^2) . where a and b and c can be found
using the line's equation and x and y are the point's values.
let's do this with coding, first of all we need to convert our mx+n=y into a ax+by+c=0, now,
we can just do so by moving y to the other end creating -1*y + m*x + n = 0, our a is equal
to m and our b is equal to -1, our c is equal to n.
var a:Number=lineMC.m;
var b:Number=-1;
var c:Number =lineMC.n;
var x:Number=circleMC.cx;
var y:Number=circleMC.cy;
var distance:Number= Math.abs(a*x + b*y + c) / Math.sqrt(a*a + b*b);
Importent note, THIS CAN BE USED IN 3D to find the distance between a plane and a point or a
vector and a point using |ax+by+cz+d| / Math.sqrt(a*a+b*b+c*c);
now you have the distance between the line and the point
Step 5
Now we compare the distance to the radius. this is based on the fact the radius is the
distance from the center point to every single point on the circle. if the distance is
smaller than the radius the line "enters" the circle. if it is equal to the radius it
touches it in exactly one point (and the angle between it and the line from the point it
touches and the center is 90 degrees) and if the radius is bigger the line and the circle do
not touch eachother.
if(distance > circleMC.radius){
//does not hit
}else{
//does hit
}
take note this can be calculated in another way, you can find the circle's equation and
compare it to the line's, but you'll just end up doing the same thing eventually.
Feel free to ask any questions.
Inglor out.