summaryrefslogtreecommitdiffhomepage
path: root/src/shapes.c
diff options
context:
space:
mode:
authorRay <[email protected]>2017-01-05 17:33:23 +0100
committerGitHub <[email protected]>2017-01-05 17:33:23 +0100
commit0369bb4c8cfe8988634a09d56c307b73be281452 (patch)
tree0861780ddb9b024fff87bdd22e7be37d60d7facb /src/shapes.c
parent81388363fa844956d0e1d023693f803c5ec21c7a (diff)
parentd5d391faaf69027b8fecb26f30754c3bff83c311 (diff)
downloadraylib-0369bb4c8cfe8988634a09d56c307b73be281452.tar.gz
raylib-0369bb4c8cfe8988634a09d56c307b73be281452.zip
Merge pull request #212 from joeld42/jbd_picking
Raycast Picking Utilities
Diffstat (limited to 'src/shapes.c')
-rw-r--r--src/shapes.c83
1 files changed, 82 insertions, 1 deletions
diff --git a/src/shapes.c b/src/shapes.c
index 3d3333c1..74480c83 100644
--- a/src/shapes.c
+++ b/src/shapes.c
@@ -533,4 +533,85 @@ Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
}
return retRec;
-} \ No newline at end of file
+}
+
+
+RayHitInfo RaycastGroundPlane( Ray ray, float groundHeight )
+{
+ RayHitInfo result = {0};
+
+ if (fabs(ray.direction.y) > EPSILON)
+ {
+ float t = (ray.position.y - groundHeight) / -ray.direction.y;
+ if (t >= 0.0) {
+ Vector3 rayDir = ray.direction;
+ VectorScale( &rayDir, t );
+ result.hit = true;
+ result.distance = t;
+ result.hitNormal = (Vector3){ 0.0, 1.0, 0.0};
+ result.hitPosition = VectorAdd( ray.position, rayDir );
+ }
+ }
+ return result;
+}
+// Adapted from:
+// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
+RayHitInfo RaycastTriangle( Ray ray, Vector3 a, Vector3 b, Vector3 c )
+{
+ Vector3 e1, e2; //Edge1, Edge2
+ Vector3 p, q, tv;
+ float det, inv_det, u, v;
+ float t;
+ RayHitInfo result = {0};
+
+ //Find vectors for two edges sharing V1
+ e1 = VectorSubtract( b, a);
+ e2 = VectorSubtract( c, a);
+
+ //Begin calculating determinant - also used to calculate u parameter
+ p = VectorCrossProduct( ray.direction, e2);
+
+ //if determinant is near zero, ray lies in plane of triangle or ray is parallel to plane of triangle
+ det = VectorDotProduct(e1, p);
+
+ //NOT CULLING
+ if(det > -EPSILON && det < EPSILON) return result;
+ inv_det = 1.f / det;
+
+ //calculate distance from V1 to ray origin
+ tv = VectorSubtract( ray.position, a );
+
+ //Calculate u parameter and test bound
+ u = VectorDotProduct(tv, p) * inv_det;
+
+ //The intersection lies outside of the triangle
+ if(u < 0.f || u > 1.f) return result;
+
+ //Prepare to test v parameter
+ q = VectorCrossProduct( tv, e1 );
+
+ //Calculate V parameter and test bound
+ v = VectorDotProduct( ray.direction, q) * inv_det;
+
+ //The intersection lies outside of the triangle
+ if(v < 0.f || (u + v) > 1.f) return result;
+
+ t = VectorDotProduct(e2, q) * inv_det;
+
+
+ if(t > EPSILON) {
+ // ray hit, get hit point and normal
+ result.hit = true;
+ result.distance = t;
+
+ result.hit = true;
+ result.hitNormal = VectorCrossProduct( e1, e2 );
+ VectorNormalize( &result.hitNormal );
+ Vector3 rayDir = ray.direction;
+ VectorScale( &rayDir, t );
+ result.hitPosition = VectorAdd( ray.position, rayDir );
+ }
+
+ return result;
+}
+