5
\$\begingroup\$

I currently have some 2D terrain in my game defined by 2D coordinates with lines drawn in between them (linear interpolation too keep it simple).

Now I want to cast a ray from anywhere above the terrain and see if it collides with the terrain for drawing bullet tracers.

Here is a quickly drawn example:

enter image description here

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

The answer you're looking for is covered by many answers on this page:

https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect

Here is the exact text of the most popular answer at the time that I'm posting this, posted by user Gareth Rees:

There’s a nice approach to this problem that uses vector cross products. Define the 2-dimensional vector cross product v × w to be vxwy − vywx (this is the magnitude of the 3-dimensional cross product).

Suppose the two line segments run from p to p + r and from q to q + s. Then any point on the first line is representable as p + tr (for a scalar parameter t) and any point on the second line as q + us (for a scalar parameter u).

The two lines intersect if we can find t and u such that:

p + tr = q + us

Cross both sides with s, getting

(p + tr) × s = (q + us) × s

And since s×s = 0, this means

t(r × s) = (q − p) × s

And therefore, solving for t:

t = (q − p) × s / (r × s)

In the same way, we can solve for u:

u = (q − p) × r / (r × s)

Now if r × s = 0 then the two lines are parallel. (There are two cases: if (q − p) × r = 0 too, then the lines are collinear, otherwise they never intersect.)

Otherwise the intersection point is on the original pair of line segments if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1.

(Credit: this method is the 2-dimensional specialization of the 3D line intersection algorithm from the article "Intersection of two lines in three-space" by Ronald Graham, published in Graphics Gems, page 304. In three dimensions, the usual case is that the lines are skew (neither parallel nor intersecting) in which case the method gives the points of closest approach of the two lines.)

\$\endgroup\$
5
  • 1
    \$\begingroup\$ In case that question ever gets deleted (since it was closed as off topic), it would be useful to copy a good answer to this site to preserve it. \$\endgroup\$
    – Tetrad
    Commented Feb 9, 2012 at 16:59
  • \$\begingroup\$ Good idea, consider it done. \$\endgroup\$
    – Nic Foster
    Commented Feb 9, 2012 at 17:19
  • \$\begingroup\$ Thats not the problem, the problem is that I don't know with which line it intersects and since this equition solves even non intersecting lines I have a problem. \$\endgroup\$ Commented Feb 9, 2012 at 19:33
  • 1
    \$\begingroup\$ You have to run this query with the line segment you're testing against every other line in your game. That may sound inefficient, and it likely will be. To optimize you will need an implementation of spatial partitioning, which will let you determine which line segments for terrain are in the nearby vicinity, at which point you only have to test against those lines. This article covers the basic idea (it covers many types of shapes though), cg.informatik.uni-freiburg.de/course_notes/sim_08_sp.pdf. \$\endgroup\$
    – Nic Foster
    Commented Feb 9, 2012 at 19:37
  • \$\begingroup\$ Thx so much for this, really helped me solving a problem! :) \$\endgroup\$
    – Ben
    Commented Feb 10, 2012 at 19:31
0
\$\begingroup\$

I guess you can handle the rest, but the main idea is written in this wiki page.

formula

using the formula above you can find the intersection point of a ray and a segment; note that ray is defined by (x1,y1) and (x2,y2) and the line segment is between (x3,y3) and(x4,y4).

\$\endgroup\$
2
  • \$\begingroup\$ Can you post at least the main idea behind what you linked to. If that link goes dead this post is of absolutely no help. :( \$\endgroup\$ Commented Feb 9, 2012 at 23:25
  • \$\begingroup\$ @RichardMarskell-Drackir added, though I doubt this question lasts longer than wikipage! \$\endgroup\$
    – Ali1S232
    Commented Feb 10, 2012 at 10:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .