Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Ray Plane Intersection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 AgroTemp   Members   -  Reputation: 117

Like
0Likes
Like

Posted 03 July 2012 - 11:41 AM

Ray plane intersection is a hard thing that I've seen people struggle with, and I've struggled with it too. When I look up guides on how to have ray plane intersection with a plane given 4 coordinates, I had no success. I only find guides that let you do collision with infinite planes and a lot of complicated math, which I'm not familiar with.

In this guide, I'll be explaining the basics of ray intersection with a plane of 4 coordinates. Its not that hard once you simplify everything down to its simplest form.


Ray-Triangle Intersection
Triangles are the most basic polygons in geometry, as far as I know. You can split any polygon in the world into a number of triangles. How? Well, a plane can be split into two triangles, a pentagon into five triangles and so on. Triangles can even be split into triangles!

Posted Image
As you can see, a square, or plane, can indeed be split into two triangles. So, to achieve ray plane or sphere plane collision, we are going to have to be able to deal with ray triangle intersection, which isn't pretty hard when you look at what you have to do.

Now, first you are going to have to get a collision point relative to a plane. For this, you need the normal of the plane, and the 4 coordinates. I don't want to explain how it works, but it just does.

[source lang="cpp"]bool rayPlaneCollision(Vec3D pn, Vec3D rs, Vec3D dir, Vec3D p1, Vec3D p2, Vec3D p3, Vec3D p4) { float a = dir.x * pn.x + dir.y * pn.y + dir.z * pn.z; if(a == 0) { return false; } float t = ((p1.x * pn.x + p1.y * pn.y + p1.z * pn.z - pn.x * rs.x - pn.y * rs.y - pn.z * rs.z)) / a; if(t < 0) { return false; } float x = rs.x + t * dir.x; float y = rs.y + t * dir.y; float z = rs.z + t * dir.z; Vec3D cp(x,y,z);[/source]
where pn is the plane normal, rs is the ray source, dir is the ray direction, p1-p4 is the 4 plane coordinates. As you can see, using the ray formula, you can get the exact point of collision on the infinite plane. Now we just have to check if its inside the plane, or 2 triangles.

Now, from here on, I will assume that a collision point is on the triangle, so everything makes sense. So, in your big triangle, divide it into 3 more subtriangles with a base point of the collision point. Here's how it looks.

Posted Image

So, as you can see, there are three more triangles based at the collision point. Now, there are 360 degrees in a circle. Look at the collision point. How many degrees are there around it? 360 degrees.

The idea here now is to get the degrees of all the angles based at the collision point and add them all together. If they equal 360(sometimes I check for 359 on a computer because of floating point issues), then we have a collision. If we don't, well, we don't have a collision.

To achieve this, we are going to need 2 formulas, the euclidian distance formula, and 1 formula of the laws of cosine as follows below:

Euclidian Distance formula:

Posted Image

Laws of cosines formula

Posted Image

If we combine these 2 formulas, we can get exactly what we want. How? Well the variables needed in the laws of cosines formula are lengths of the lines, where a and b are connected to the collision point and c is opposite to the collision point. Well, how do we find the lengths? Euclidian distance. So we're going to have to use the euclidian distance formula 9 times and the laws of cosines formula 3 times for all three triangles.

So let's imagine some coordinates here. Let's say a triangle is at

p1(1,1) p2(3,5) p3(5,1)

And a collision point is at cp(3,2). (I hope they're plotted correctly xD) I'm drawing these on a paper as I write, so I think you should too. Now, draw the lines to each corner to create 3 subtriangles from the collision point.

So now we're going to get the angle from p1 to p2 to cp.

[source lang="cpp"]float t1a = distance(p1,cp);float t1b = distance(p2,cp);float t1c = distance(p1,p2);float t1cos = acos((pow(t1a, 2) + pow(t1b, 2) - pow(t1c, 2)) / (2 * t1a * t1b)) * 180 / M_PI;[/source]
Write a distance() function to get the distance between two vectors, it isn't that hard at all. So, we are treating t1a and t1b as the lengths of the lines connected to the collision point, and t1c is the length opposite. Now we just use the laws of cosines formula, except we use arc cosine to get the angle itself. Since C++ uses radians for a degree system, I multiply by 180 and divide by Pi(3.141592653589793) to convert it to degrees, out of 360, not 3.14.

Now, we do this for each subtriangle, using the exact same process and procedures, and we should have ourselves three angle variables. If we add these 3 angle variables, and they are 360 degrees, we have a collision. Make sure to check for 359 some times.

And there you have it! Ray triangle intersection is done!

Ray-Plane Intersection
I won't really explain anything here at all, other than just divide your plane into 2 triangles. And then check if there is a collision between the 2 triangles. If one has a collision, then you have a collision on the plane.

And that's it! Feel free to ask questions.

Sponsor:

#2 luca-deltodesco   Members   -  Reputation: 637

Like
0Likes
Like

Posted 03 July 2012 - 12:01 PM

Firstly, this is a huge misuse of the word 'plane' which refers to an 'infinitely' large flat surface, so it cannot be split into 2 triangles, it cannot be split into 2 million triangles. There is no such thing as a finite plane, nor an infinite plane; all planes are infinite. Posted Image Intersections against a plane of any convex shape (not just a ray) due to the infinitely large nature are very trivial too, there are literally no 'edge' cases to deal with.

Ignoring that;

When doing a ray-triangle intersection we would not normally work with angles for numerical stability and speed.

The usual method is to compute barycentric coordinates (can still suffer), or to simply tast the ray-plane intersection against edge normals treating the triangle as an intersection of 3 half-spaces with a plane in which case there is really not much reason to use triangle intersections for a convex quad since you can treat it as an intersection of 4 half-spaces with a plane as a simple extension of a triangle-ray intersection.

The half space method would be like (unoptimised):

intersect_triangle((t0, t1, t2), (origin, direction)):
	normal = (t1 - t0) cross (t2 - t0)
	factor = (t0 - origin) dot normal / direction dot normal
	intersection = origin + direction * factor

	if ((intersection - t0) dot ((t1 - t0) cross normal) <= 0 &&
		(intersection - t1) dot ((t2 - t1) cross normal) <= 0 &&
		(intersection - t2) dot ((t0 - t2) cross normal) <= 0))
			return (Just p)
	else
			return Nothing


(Also: angles in a triangle add to 180 degrees, not 360 ;p)

Edited by luca-deltodesco, 03 July 2012 - 12:25 PM.


#3 AgroTemp   Members   -  Reputation: 117

Like
0Likes
Like

Posted 03 July 2012 - 12:28 PM

I'm not hugely familiar with any of the terms, but this method works fine for me, and its the best one I found. Also, its 360 degrees around the collision point, not in the triangle.

#4 Álvaro   Crossbones+   -  Reputation: 13934

Like
0Likes
Like

Posted 03 July 2012 - 01:13 PM

I'm not hugely familiar with any of the terms, but this method works fine for me, and its the best one I found. Also, its 360 degrees around the collision point, not in the triangle.


The objections to the terminology are legitimate, and the method is also dubious. Ray-triangle intersection is an affine problem, but your solution uses trigonometry, and trigonometry requires the presence of a metric. In other words, if you need to use trigonometric functions, you are doing it wrong.

The method I've used int the past was to pick an affine reference whose origin is in vertex A of the triangle ABC and such that the first two vectors of the basis are B-A and C-A. You then find the intersection, express the result in the basis and check simply that the coordinates (x,y) along those two vectors satisfy `x>=0 && y>=0 && x+y<=1'.

Edited by alvaro, 03 July 2012 - 01:17 PM.


#5 luca-deltodesco   Members   -  Reputation: 637

Like
0Likes
Like

Posted 03 July 2012 - 01:21 PM

If you want to compare the non-optimised method I posted, and yours you can see that apart from the single division to find intersection on triangles-plane which you must also perform, my test involves only additions/subtractions/multiplications, whereas yours has ontop of all of that, several calls to sqrt() and acos() as well as divisions.

Your method is unstable both because of this, and that you would for a robust implementation require to check that your collision point is not very close/equal to a vertex of the triangle, or else you will have divisions by zero too.

From a numerical standpoint your test is actually against a slightly bloated triangle as points close to, but outside of an edge of the triangel would be classified as internal. etc etc.

#6 jefferytitan   Crossbones+   -  Reputation: 2246

Like
0Likes
Like

Posted 03 July 2012 - 04:56 PM

I think that in summary, your method mostly works but isn't the fastest or most practical approach. I can appreciate that the blizzard of terminology can be confusing. However I think that it's better to use a best-practice approach for collision testing for speed and robustness. Whether you want to put the hard yards in to understand how it works is up to you.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS