I wanted to find out how to find the hitTest between a line and a movie clip, I have found out how to do a hit test between 2 lines. here is the procedure I went through to figure that out. Perhaps it can help you explain how I can apply this to finding a hit test of a line and a movieclip.
Eventually I want to be able to apply this to a curve. so the hitTest to a movieclip and a curve. Lets first do the line though before I get crazy with too complicated stuff.
Here is the work I have done so far.
first the variables you are given
(x1, y1) and (x2, y2) // coordinates of 2 endpoints to line number 1
(x3, y3) and (x4, y4) // coordinates of 2 endpoints to line number 2
y = a + bx // formula for a line
b = (y1 - y2) / (x1- x2) // formula for slope
variable you need
(xi, yi) // intersection point of the 2 lines
find a
y1 = a + x1 * b
y1 = a + x1 * ( (y1-y2)/(x1-x2) )
a = x1 * ( (y1-y2)/(x1-x2) ) - y1
you can calculate the 2 formulas using those equations
so
b1 = (y1 - y2) / (x1- x2) // slope line 1
b2 = (y3 - y4) / (x3- x4) // slope line 2
a1 = x1 * ( (y1-y2)/(x1-x2) ) - y1 // a line 1
a2 = x3 * ( (y3-y4)/(x3-x4) ) - y3 // a line 2
determine xi and yi intersection points.
yi = a1 + x * b1
yi = a2 + x * b2
a1 + xi * b1 = a2 + xi * b2
a1 - a2 = (xi * b2) - (xi * b1)
xi = (a1 - a2) / (b2 - b1)
now that you have all 5 points you need to determine if your 2 lines intersect and if it intersects inbetween the points given.
its hard to visualize but these are the conditions that must be checked to determine if the lines are intersectioning
xi has to be defined, it would not be defined if lines are parrallell
x1< xi
xi < x2
x3 < xi
xi < x4
if all these conditions are met the lines cross and cross from the 4 points given.
now why did I go through all this crap, im hoping there is some way that we can use this procedure to apply to determining if a line crosses over a movie clip. here is the code in full actionscript.
var y1:Number = somenumber;
var x1:Number = somenumber;
var x2:Number = somenumber;
var y2:Number = somenumber;
var b1:Number = (y1 - y2) / (x1 - x2);
var b2:Number = (y3 - y4) / (x3 - x4);
var a1:Number = x1 * ((y1 - y2) / (x1 - x2)) - y1;
var a2:Number = x3 * ((y3 - y4) / (x3 - x4)) - y3;
var xi:Number = (a1 - a2) / (b2 - b1);
if (xi != undefined and x1 < xi and xi < x2 and x3 < xi and xi < x4) {
trace("lines intersect");
}
- var y1:Number = somenumber;
- var x1:Number = somenumber;
- var x2:Number = somenumber;
- var y2:Number = somenumber;
- var b1:Number = (y1 - y2) / (x1 - x2);
- var b2:Number = (y3 - y4) / (x3 - x4);
- var a1:Number = x1 * ((y1 - y2) / (x1 - x2)) - y1;
- var a2:Number = x3 * ((y3 - y4) / (x3 - x4)) - y3;
- var xi:Number = (a1 - a2) / (b2 - b1);
- if (xi != undefined and x1 < xi and xi < x2 and x3 < xi and xi < x4) {
- trace("lines intersect");
- }