# collision between lines

2 replies to this topic

### #1 ChaosMaker

ChaosMaker

GMC Member

• GMC Member
• 288 posts
• Version:GM:HTML5

Posted 14 July 2011 - 01:58 AM

There is some mathematical formula to check collision between two lines?
without using sprites or function collision_line native game maker.
• 0

### #2 Gamer3D

Gamer3D

Human* me = this;

• GMC Member
• 1590 posts
• Version:GM8.1

Posted 14 July 2011 - 02:19 AM

Non-vertical lines may be expressed in the form $y = m x + b$. Where (if the line passes through points <x1, y1> and <x2, y2>) $m = \frac{y2 - y1}{x2 - x1}$ and $b = y_1 - x_1 * m$

If line 1 is $y = m_1 x + b_1$ and line 2 is $y = m_2 x + b_2$, then because we want a point on both lines (x1 = x2, y1 = y2) we substitute to get: $m_1 x + b_1 = m_2 x + b_2$. Solving, we get $x = \frac{b_2 - b_1}{m_1 - m_2}$. With x known, we now substitute that value back into one of the line equations to get y.

For vertical lines, substitute the x of the vertical line into the other line's equation.

Another more general way to do it is (without error-checking):
//  line_intersect (line1x1, line1y1, line1x2, line1y2, line2x1, line2y1, line2x2, line2y2);
argument2 -= argument0;
argument3 -= argument1;
argument6 -= argument4;
argument7 -= argument5;
var t;
t = (argument2 * (argument5 - argument1) + argument3 * (argument0 - argument4)) / (argument6 * argument3 - argument2 * argument7);
global.x = argument6 * t + argument4;
global.y = argument7 * t + argument5;


• 0

### #3 ChaosMaker

ChaosMaker

GMC Member

• GMC Member
• 288 posts
• Version:GM:HTML5

Posted 14 July 2011 - 02:30 AM

Thank you very much, works fine.
*.*
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users