hitTest to a line ?

  • Pang
  • Born
  • Born
  • Pang
  • Posts: 4
  • Loc: Chicago

Post 3+ Months Ago

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.

Code: [ Select ]
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");
}
  1. var y1:Number = somenumber;
  2. var x1:Number = somenumber;
  3. var x2:Number = somenumber;
  4. var y2:Number = somenumber;
  5. var b1:Number = (y1 - y2) / (x1 - x2);
  6. var b2:Number = (y3 - y4) / (x3 - x4);
  7. var a1:Number = x1 * ((y1 - y2) / (x1 - x2)) - y1;
  8. var a2:Number = x3 * ((y3 - y4) / (x3 - x4)) - y3;
  9. var xi:Number = (a1 - a2) / (b2 - b1);
  10. if (xi != undefined and x1 < xi and xi < x2 and x3 < xi and xi < x4) {
  11.   trace("lines intersect");
  12. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

ohh, and why won't you use the hitTest function from actionscript?
You can either use it either to consider the movieclip as a a rectangle, or to calculate the exact shape. It might be slower in the second case, but in the case of the lines is ok to use it to consider the mc as a rectangle.
i.e.: if myMC.hitTest(anotherMC) { trace("mc's intersect"); }

Of course, if you want your way, to do hitTest between a line and a regulate shape, then it's possible by the means of geometrical formulas. If you want to do hitTest between a line and a non regulate shape, you can do it either by means of matrix (calculating each point of the line if is over the shape) or ... if i'm not wrong, there are some analytic maths formulas to do an aproximation .... but you'll have to study them first.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 7 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.