• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Ray Plane Intersection

5 posts in this topic

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.

[b][size=6]Ray-Triangle Intersection[/size][/b]
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!

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.


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:


Laws of cosines formula


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!

[size=6][b]Ray-Plane Intersection[/b][/size]
[size=6][size=4]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.[/size][/size]

[size=6][size=4]And that's it! Feel free to ask questions.[/size][/size]

Share this post

Link to post
Share on other sites
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. [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] 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)
return Nothing

(Also: angles in a triangle add to 180 degrees, not 360 ;p) Edited by luca-deltodesco

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
[quote name='AgroTemp' timestamp='1341340122' post='4955388']
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

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0