A Collection of Game Dev Geometry based snippets
When I started building games in Unity (C#) I found myself needing many functions to calculate common geometry functions. I was hoping such a repo existed somewhere, and I could just use it, but I didn't find one. So I wanted to collect the best, optimized examples from the Internet/experts... and organize them for fellow developers that might want to use them.
Although my focus here is in C#, for Unity development, if this is of any value to others, I will add C++, Java/Kotlin, Python examples as well.
- Determine which side of a line (2D) a point is on
- Determine if a point is inside a (2D) shape (rectangle, triangle, circle, polygon)
- Determine the centroid of a (2D) shape (rectangle, triangle, circle, polygon)
- Determine the normal for a line
- Determine the angle between 2 lines
- Determine if 2 lines intersect, and if so where
- Determine if a point is within a (3D) shape (rectangular prism/cuboid, sphere, cone, cylinder, pyramid)
- Determine which side of (3D) plane a point is on
- Determine the midpoint of a line
- Determine if a point is on a line (with a fuzz factor)
- Determine the normal for a plane
- Determine if 2 lines intersect, and if so where
- Determine where a line intersects a plane
Yo! This is awesome, but what gives? camelCase method names, OTB brace style, and tabs for indentation?... this isn't the C# way! Yup, this was intentional. I come from a Java/JavaScript world where methods start lowercase and its just so much easier for me to work with... and well the brace/indentaion styling are just my preferences.