Skip to content

Instantly share code, notes, and snippets.

@antoineMoPa
Last active April 15, 2024 17:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoineMoPa/3d01f215046dcd4a0011594765d1785d to your computer and use it in GitHub Desktop.
Save antoineMoPa/3d01f215046dcd4a0011594765d1785d to your computer and use it in GitHub Desktop.
/*
Copyright 2021 Antoine Morin-Paulhus
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
Find the intersection of a plane and a line.
Parameters:
P0 : A point on the plane
P01: Another point of the plane
P02: Another point of the plane
la: Line point a
lb: Line point b
.P01_________
| |
| lb |
| / |
P0 .______/_____.P02
/
/la
Returns a vector vec.xyzw where x is an indicator. x < 0 if there is no intersection or
infinite intersections (line is on plane).
Otherwise, the intersection point coordinates are returned in vec.yzw.
*/
vec4 planeLineIntersection(vec3 P0, vec3 P01, vec3 P02, vec3 la, vec3 lb) {
// Many thanks to wikipedia
// Reference:
// https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection
vec3 lab = lb - la;
float det = - dot(lab, cross(P01,P02));
if (abs(det) < 0.01) {
return vec4(-1.0,0.0,0.0,0.0);
}
float t = 1.0/det * dot(cross(P01, P02), la - P0);
float u = 1.0/det * dot(cross(P02, -lab), la - P0);
float v = 1.0/det * dot(cross(-lab, P01), la - P0);
vec3 x = P0 + P01 * u + P02 * v;
float indicator = 1.0;
vec4 ret;
ret.xyzw = vec4(indicator, x);
return ret;
}
@coded-aesthetics
Copy link

@antoineMoPa Hey thanks for putting this up! What I don't get is the part in line 58... why is the indicator negative (no solution) for this? Also, why do we need to calculate u and v if the solution can be found by la + t * lab? Is there some information hidden in u and v that I do not understand?

@antoineMoPa
Copy link
Author

@coded-aesthetics I always love it when someone comments on an old file I had forgotten about ๐Ÿ˜†

Line 58 is strange, reading https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection I have no idea why I would have done that. Could be that it helped achieve some visual result I wanted in my tiny hobby game here.

Let me know if you tested and it would make sense to remove for the next people landing here ๐Ÿ™‚

@coded-aesthetics
Copy link

Haha, nice one ๐Ÿ˜† @antoineMoPa
Thanks for your answer! Yeah, in the context of your game this makes sense as this checks for intersection with the rectangle delimited by the points p0, p1 and p2 which can be used to describe a wall... So this function should be named intersectRectangleLine or the code in line 58 should be removed...

Fun fact: This was returned to me as the top search result on google for the keywords "intersect plane line glsl" ๐Ÿ’ช

@antoineMoPa
Copy link
Author

@coded-aesthetics Thanks, I did some cleanup ๐Ÿ˜ƒ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment