
Thus, if the denominator is 0, both \(u_a\) and \(u_b\) are undefined, and no collision between these lines exist (the lines are parallel). You’ll notice that the denominator for these two equations is the same. Solving for the point where \(P_a = P_b\) (the intersection between these lines), we get two equations: With \(u_b\)being any real number and \(B_1\) and \(B_2\) as two points on line \(B\). Similarly, we define point \(P_b\), any point on line \(B\), as: Conversely, if \(u_a\)is between 0 and 1, \(P_a\) is between points \(A_1\) and \(A_2\) (this is important later). Note that, since \(u_a\) is any real number, this point (and therefore the intersection between the lines) can lie on any point on either side of \(A_1\) and \(A_2\). With a line \(A\) of infinite length, and two points on that line, \( A_1\) and \(A_2\), we define point \(P_a\), any point on line \(A\) as: Testing collision between two lines is slightly more complicated that the two methods described above, requiring some algebra to develop an algorithm, but is still otherwise fairly simple.Īll lines used in this method will be represented as two points along the line (or the two endpoints if the lines are to be considered line segments instead of lines of infinite length). Occasionally, one would need to represent objects in the game as simple lines (or perhaps a compound group of lines). (Rect1.Right < Rect2.Left) ) Line-Line intersection test OutsideRight = Rect1.Right Rect2.Bottom) OR OutsideBottom = Rect1.Bottom Rect2.Bottom Return squaredist <= (R1 + R2) * (R1 + R2) Bounding box testīounding box collision.

Squaredist = (distX * distX) + (distY * distY) The above method can be optimized somewhat by comparing the square distance with the square of the sum of the radii instead, saving a comparatively slow square root operation, as shown below. R2ğloating point Radius of second circleĭist = sqrt((distX * distX) + (distY * distY))

So there comes a time in almost every game’s development cycle when an important choice needs to be made: how accurate should the collision detection be, and which method should be used to achieve that accuracy. Whether it’s simply to prevent the player character from walking through the walls of your maze with a simple collision grid array, or if it’s to test if any of the hundreds of projectiles fired by a boss character in a top-down shoot-’em-up have struck the player’s ship, your game will likely require a collision detection system of one sort or another. This article originally appeared in Dev.Mag Issue 28, released in January 2009.Īlmost every video game needs to respond to objects touching each other in some sense, a practice commonly known as collision detection. – Feb 03, 12:23 PMġ3 Apr, 2009 in Development / Mathematics / Programming tagged collision detection / explanation / issue28 / math / tutorial by Claudio de Sa

#Vector 2d line collision code
code promotion I do not know if it's just me or if perhaps everybody else experiencing issues with your website.Unity (?) - Alexey Gulev Practices - ?, ? (? 2016 ? - ?.Procedural Tree – Poisson Disk Sampling in 3D (C#) – Thomas Kübler is a nice and general implementation for the 2D case available at with some hints for extending it to 3D.
#Vector 2d line collision how to
How to Choose Colours Procedurally (Algorithms) 50 Tips for Working with Unity (Best Practices)
