Position counted wrong

2 posts

Flag Post

Hello, can anyone help me with the little problem? In my game i have a function which shall count if where the 2 lines will cross. One line is defined by 2 points, another is defined by angle and one point. To make everything easier, i move everything in the way that second line point is (0, 0):

function linescollide(x0, y0, a, x11, y11, x22, y22, obj)
		{
			var x1 = x11 - x0;
			var y1 = (y11 - y0);
			var x2 = x22 - x0;
			var y2 = (y22 - y0);

(First line was defined by 2 points: (x11, y11) and (x22, y22), second line was defined by a – angle and (x0, y0))
That way x0 and y0 will be zero. The function works, but y1 and y2 are counted wrong. I believe that i have to include stage height somehow. So:
1)What is the proper way to count y1 and y2?
2)Is there easier way to count if where 2 lines will cross rather than using math?
3)Is there a way to flip Y direction(so Y will not go from stage top to bottom, but from bottom to top)?

 
Flag Post

You are trying to find if two lines collide. This will always be the case, unless they are parallel.
m1 = (y22-y11)/(x22-x11)
m2 = tan(a (IN RADIANS)) or tan(v2j/v2i)
if(m1!=m2) //they collide

f1:
Y-y1 = m(X-x1)
Y – y11 = m1(X-x11)
Y = (m1)X – (m1*x11) + y11

f2:
Y = (m2)X – (m2*x0) + y0

collision, x = x, y = y

(m1)X – (m1*x11) + y11 = (m2)X – (m2*x0) + y0
(m1-m2)X = y0 + (m1*x11) – y11 – (m2*x0)
X = (y0 + (m1*x11) – y11 – (m2*x0))/(m1-m2)
got X point of collision

Y = (m2)X – (m2*x0) + y0 or (m1)X – (m1*x11) + y11
sub in X
and you get Y point of collision.

function linescollide(x0, y0, a, x11, y11, x22, y22, obj){
    var m1:Number = (y22-y11)/(x22-x11);
    var m2:Number = Math.tan(a*Math.PI/180); //If a is already in Radians, omit *Math.PI/180
    if(m1!=m2){ //they collide
        var colX:Number = (y0 + (m1*x11) – y11 – (m2*x0))/(m1-m2);
        var colY:Number = (m2*colX) – (m2*x0) + y0
    }
}