aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoel Davis <joeld42@gmail.com>2016-12-31 15:06:39 -0800
committerJoel Davis <joeld42@gmail.com>2016-12-31 15:06:39 -0800
commit037da8879a3ae61b09d8388bc2b4a2fe5359256a (patch)
tree15f63d24730128f9a3534d20c866f2898e4490e1 /src
parent202f45415c98df2201202ba8edb10b6496cbeb62 (diff)
downloadraylib-037da8879a3ae61b09d8388bc2b4a2fe5359256a.tar.gz
raylib-037da8879a3ae61b09d8388bc2b4a2fe5359256a.zip
Added RaycastGround and ray picking example
Diffstat (limited to 'src')
-rw-r--r--src/raylib.h15
-rw-r--r--src/shapes.c20
2 files changed, 35 insertions, 0 deletions
diff --git a/src/raylib.h b/src/raylib.h
index e2e4ee13..f291ce85 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -97,6 +97,9 @@
#define DEG2RAD (PI/180.0f)
#define RAD2DEG (180.0f/PI)
+// A small number
+#define EPSILON 0.000001
+
// raylib Config Flags
#define FLAG_FULLSCREEN_MODE 1
#define FLAG_RESIZABLE_WINDOW 2
@@ -491,6 +494,13 @@ typedef struct Ray {
Vector3 direction; // Ray direction
} Ray;
+// Information returned from a raycast
+typedef struct RayHitInfo {
+ bool hit; // Did the ray hit something?
+ Vector3 hitPosition; // Position of nearest hit
+ Vector3 hitNormal; // Surface normal of hit
+} RayHitInfo;
+
// Wave type, defines audio wave data
typedef struct Wave {
unsigned int sampleCount; // Number of samples
@@ -911,6 +921,11 @@ RLAPI bool CheckCollisionRaySphereEx(Ray ray, Vector3 spherePosition, float sphe
RLAPI bool CheckCollisionRayBox(Ray ray, BoundingBox box); // Detect collision between ray and box
//------------------------------------------------------------------------------------
+// Ray Casts
+//------------------------------------------------------------------------------------
+RLAPI RayHitInfo RaycastGroundPlane( Ray ray, float groundHeight );
+
+//------------------------------------------------------------------------------------
// Shaders System Functions (Module: rlgl)
// NOTE: This functions are useless when using OpenGL 1.1
//------------------------------------------------------------------------------------
diff --git a/src/shapes.c b/src/shapes.c
index 3d3333c1..4b2de4f2 100644
--- a/src/shapes.c
+++ b/src/shapes.c
@@ -533,4 +533,24 @@ Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
}
return retRec;
+}
+
+
+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 camDir = ray.direction;
+ VectorScale( &camDir, t );
+ result.hit = true;
+ result.hitNormal = (Vector3){ 0.0, 1.0, 0.0};
+ result.hitPosition = VectorAdd( ray.position, camDir );
+ }
+ }
+
+ return result;
} \ No newline at end of file