diff --git a/examples/models/models_mesh_picking.c b/examples/models/models_mesh_picking.c index 5dda8df5..31500bce 100644 --- a/examples/models/models_mesh_picking.c +++ b/examples/models/models_mesh_picking.c @@ -63,53 +63,53 @@ int main(void) UpdateCamera(&camera); // Update camera // Display information about closest hit - RayHitInfo nearestHit = { 0 }; + RayCollision collision = { 0 }; char *hitObjectName = "None"; - nearestHit.distance = FLT_MAX; - nearestHit.hit = false; + collision.distance = FLT_MAX; + collision.hit = false; Color cursorColor = WHITE; // Get ray and test against ground, triangle, and mesh ray = GetMouseRay(GetMousePosition(), camera); // Check ray collision aginst ground plane - RayHitInfo groundHitInfo = GetCollisionRayGround(ray, 0.0f); + RayCollision groundHitInfo = GetRayCollisionGround(ray, 0.0f); - if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance)) + if ((groundHitInfo.hit) && (groundHitInfo.distance < collision.distance)) { - nearestHit = groundHitInfo; + collision = groundHitInfo; cursorColor = GREEN; hitObjectName = "Ground"; } // Check ray collision against test triangle - RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc); + RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc); - if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance)) + if ((triHitInfo.hit) && (triHitInfo.distance < collision.distance)) { - nearestHit = triHitInfo; + collision = triHitInfo; cursorColor = PURPLE; hitObjectName = "Triangle"; - bary = Vector3Barycenter(nearestHit.position, ta, tb, tc); + bary = Vector3Barycenter(collision.point, ta, tb, tc); hitTriangle = true; } else hitTriangle = false; - RayHitInfo meshHitInfo = { 0 }; + RayCollision meshHitInfo = { 0 }; // Check ray collision against bounding box first, before trying the full ray-mesh test - if (CheckCollisionRayBox(ray, towerBBox)) + if (GetRayCollisionBox(ray, towerBBox).hit) { hitMeshBBox = true; // Check ray collision against model // NOTE: It considers model.transform matrix! - meshHitInfo = GetCollisionRayModel(ray, tower); + meshHitInfo = GetRayCollisionModel(ray, tower); - if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance)) + if ((meshHitInfo.hit) && (meshHitInfo.distance < collision.distance)) { - nearestHit = meshHitInfo; + collision = meshHitInfo; cursorColor = ORANGE; hitObjectName = "Mesh"; } @@ -128,7 +128,7 @@ int main(void) // Draw the tower // WARNING: If scale is different than 1.0f, - // not considered by GetCollisionRayModel() + // not considered by GetRayCollisionModel() DrawModel(tower, towerPos, 1.0f, WHITE); // Draw the test triangle @@ -140,17 +140,17 @@ int main(void) if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME); // If we hit something, draw the cursor at the hit point - if (nearestHit.hit) + if (collision.hit) { - DrawCube(nearestHit.position, 0.3f, 0.3f, 0.3f, cursorColor); - DrawCubeWires(nearestHit.position, 0.3f, 0.3f, 0.3f, RED); + DrawCube(collision.point, 0.3f, 0.3f, 0.3f, cursorColor); + DrawCubeWires(collision.point, 0.3f, 0.3f, 0.3f, RED); Vector3 normalEnd; - normalEnd.x = nearestHit.position.x + nearestHit.normal.x; - normalEnd.y = nearestHit.position.y + nearestHit.normal.y; - normalEnd.z = nearestHit.position.z + nearestHit.normal.z; + normalEnd.x = collision.point.x + collision.normal.x; + normalEnd.y = collision.point.y + collision.normal.y; + normalEnd.z = collision.point.z + collision.normal.z; - DrawLine3D(nearestHit.position, normalEnd, RED); + DrawLine3D(collision.point, normalEnd, RED); } DrawRay(ray, MAROON); @@ -162,21 +162,21 @@ int main(void) // Draw some debug GUI text DrawText(TextFormat("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK); - if (nearestHit.hit) + if (collision.hit) { int ypos = 70; - DrawText(TextFormat("Distance: %3.2f", nearestHit.distance), 10, ypos, 10, BLACK); + DrawText(TextFormat("Distance: %3.2f", collision.distance), 10, ypos, 10, BLACK); DrawText(TextFormat("Hit Pos: %3.2f %3.2f %3.2f", - nearestHit.position.x, - nearestHit.position.y, - nearestHit.position.z), 10, ypos + 15, 10, BLACK); + collision.point.x, + collision.point.y, + collision.point.z), 10, ypos + 15, 10, BLACK); DrawText(TextFormat("Hit Norm: %3.2f %3.2f %3.2f", - nearestHit.normal.x, - nearestHit.normal.y, - nearestHit.normal.z), 10, ypos + 30, 10, BLACK); + collision.normal.x, + collision.normal.y, + collision.normal.z), 10, ypos + 30, 10, BLACK); if (hitTriangle) DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK); } diff --git a/src/models.c b/src/models.c index 101677ca..9217cc2d 100644 --- a/src/models.c +++ b/src/models.c @@ -2979,53 +2979,32 @@ bool CheckCollisionBoxSphere(BoundingBox box, Vector3 center, float radius) return collision; } -// Detect collision between ray and sphere -bool CheckCollisionRaySphere(Ray ray, Vector3 center, float radius) +// Get collision info between ray and sphere +RayCollision GetRayCollisionSphere(Ray ray, Vector3 center, float radius) { - bool collision = false; + RayCollision collision = { 0 }; Vector3 raySpherePos = Vector3Subtract(center, ray.position); float distance = Vector3Length(raySpherePos); float vector = Vector3DotProduct(raySpherePos, ray.direction); float d = radius*radius - (distance*distance - vector*vector); - if (d >= 0.0f) collision = true; - - return collision; -} - -// Detect collision between ray and sphere with extended parameters and collision point detection -bool CheckCollisionRaySphereEx(Ray ray, Vector3 center, float radius, Vector3 *collisionPoint) -{ - bool collision = false; - - Vector3 raySpherePos = Vector3Subtract(center, ray.position); - float distance = Vector3Length(raySpherePos); - float vector = Vector3DotProduct(raySpherePos, ray.direction); - float d = radius*radius - (distance*distance - vector*vector); - - if (d >= 0.0f) collision = true; + if (d >= 0.0f) collision.hit = true; // Check if ray origin is inside the sphere to calculate the correct collision point - float collisionDistance = 0; - - if (distance < radius) collisionDistance = vector + sqrtf(d); - else collisionDistance = vector - sqrtf(d); + if (distance < radius) collision.distance = vector + sqrtf(d); + else collision.distance = vector - sqrtf(d); // Calculate collision point - Vector3 cPoint = Vector3Add(ray.position, Vector3Scale(ray.direction, collisionDistance)); - - collisionPoint->x = cPoint.x; - collisionPoint->y = cPoint.y; - collisionPoint->z = cPoint.z; + collision.point = Vector3Add(ray.position, Vector3Scale(ray.direction, collision.distance)); return collision; } -// Detect collision between ray and bounding box -bool CheckCollisionRayBox(Ray ray, BoundingBox box) +// Get collision info between ray and box +RayCollision GetRayCollisionBox(Ray ray, BoundingBox box) { - bool collision = false; + RayCollision collision = { 0 }; float t[8] = { 0 }; t[0] = (box.min.x - ray.position.x)/ray.direction.x; @@ -3037,14 +3016,17 @@ bool CheckCollisionRayBox(Ray ray, BoundingBox box) t[6] = (float)fmax(fmax(fmin(t[0], t[1]), fmin(t[2], t[3])), fmin(t[4], t[5])); t[7] = (float)fmin(fmin(fmax(t[0], t[1]), fmax(t[2], t[3])), fmax(t[4], t[5])); - collision = !(t[7] < 0 || t[6] > t[7]); + collision.hit = !(t[7] < 0 || t[6] > t[7]); + + // TODO: Calculate other RayCollision data return collision; } + // Get collision info between ray and mesh -RayHitInfo GetCollisionRayMesh(Ray ray, Mesh mesh, Matrix transform) +RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform) { - RayHitInfo result = { 0 }; + RayCollision collision = { 0 }; // Check if mesh vertex data on CPU for testing if (mesh.vertices != NULL) @@ -3074,47 +3056,49 @@ RayHitInfo GetCollisionRayMesh(Ray ray, Mesh mesh, Matrix transform) b = Vector3Transform(b, transform); c = Vector3Transform(c, transform); - RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, a, b, c); + RayCollision triHitInfo = GetRayCollisionTriangle(ray, a, b, c); if (triHitInfo.hit) { // Save the closest hit triangle - if ((!result.hit) || (result.distance > triHitInfo.distance)) result = triHitInfo; + if ((!collision.hit) || (collision.distance > triHitInfo.distance)) collision = triHitInfo; } } } - return result; + + return collision; } // Get collision info between ray and model -RayHitInfo GetCollisionRayModel(Ray ray, Model model) +RayCollision GetRayCollisionModel(Ray ray, Model model) { - RayHitInfo result = { 0 }; + RayCollision collision = { 0 }; for (int m = 0; m < model.meshCount; m++) { - RayHitInfo meshHitInfo = GetCollisionRayMesh(ray, model.meshes[m], model.transform); + RayCollision meshHitInfo = GetRayCollisionMesh(ray, model.meshes[m], model.transform); if (meshHitInfo.hit) { // Save the closest hit mesh - if ((!result.hit) || (result.distance > meshHitInfo.distance)) result = meshHitInfo; + if ((!collision.hit) || (collision.distance > meshHitInfo.distance)) collision = meshHitInfo; } } - return result; + return collision; } // Get collision info between ray and triangle // NOTE: Based on https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm -RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3) +RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3) { #define EPSILON 0.000001 // A small number - Vector3 edge1, edge2; + RayCollision collision = { 0 }; + Vector3 edge1 = { 0 }; + Vector3 edge2 = { 0 }; Vector3 p, q, tv; float det, invDet, u, v, t; - RayHitInfo result = {0}; // Find vectors for two edges sharing V1 edge1 = Vector3Subtract(p2, p1); @@ -3127,7 +3111,7 @@ RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3) det = Vector3DotProduct(edge1, p); // Avoid culling! - if ((det > -EPSILON) && (det < EPSILON)) return result; + if ((det > -EPSILON) && (det < EPSILON)) return collision; invDet = 1.0f/det; @@ -3138,7 +3122,7 @@ RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3) u = Vector3DotProduct(tv, p)*invDet; // The intersection lies outside of the triangle - if ((u < 0.0f) || (u > 1.0f)) return result; + if ((u < 0.0f) || (u > 1.0f)) return collision; // Prepare to test v parameter q = Vector3CrossProduct(tv, edge1); @@ -3147,29 +3131,28 @@ RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3) v = Vector3DotProduct(ray.direction, q)*invDet; // The intersection lies outside of the triangle - if ((v < 0.0f) || ((u + v) > 1.0f)) return result; + if ((v < 0.0f) || ((u + v) > 1.0f)) return collision; t = Vector3DotProduct(edge2, q)*invDet; if (t > EPSILON) { // Ray hit, get hit point and normal - result.hit = true; - result.distance = t; - result.hit = true; - result.normal = Vector3Normalize(Vector3CrossProduct(edge1, edge2)); - result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, t)); + collision.hit = true; + collision.distance = t; + collision.normal = Vector3Normalize(Vector3CrossProduct(edge1, edge2)); + collision.point = Vector3Add(ray.position, Vector3Scale(ray.direction, t)); } - return result; + return collision; } // Get collision info between ray and ground plane (Y-normal plane) -RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight) +RayCollision GetRayCollisionGround(Ray ray, float groundHeight) { #define EPSILON 0.000001 // A small number - RayHitInfo result = { 0 }; + RayCollision collision = { 0 }; if (fabsf(ray.direction.y) > EPSILON) { @@ -3177,15 +3160,15 @@ RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight) if (distance >= 0.0) { - result.hit = true; - result.distance = distance; - result.normal = (Vector3){ 0.0, 1.0, 0.0 }; - result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, distance)); - result.position.y = groundHeight; + collision.hit = true; + collision.distance = distance; + collision.normal = (Vector3){ 0.0, 1.0, 0.0 }; + collision.point = Vector3Add(ray.position, Vector3Scale(ray.direction, distance)); + collision.point.y = groundHeight; } } - return result; + return collision; } //---------------------------------------------------------------------------------- diff --git a/src/raylib.h b/src/raylib.h index 63d1bdf6..5f57496c 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -399,12 +399,12 @@ typedef struct Ray { } Ray; // RayCollision, ray hit information -typedef struct RayHitInfo { +typedef struct RayCollision { bool hit; // Did the ray hit something? float distance; // Distance to nearest hit - Vector3 position; // Position of nearest hit + Vector3 point; // Point of nearest hit Vector3 normal; // Surface normal of hit -} RayHitInfo; +} RayCollision; // BoundingBox typedef struct BoundingBox { @@ -1445,13 +1445,12 @@ RLAPI void DrawBillboardPro(Camera camera, Texture2D texture, Rectangle source, RLAPI bool CheckCollisionSpheres(Vector3 center1, float radius1, Vector3 center2, float radius2); // Detect collision between two spheres RLAPI bool CheckCollisionBoxes(BoundingBox box1, BoundingBox box2); // Detect collision between two bounding boxes RLAPI bool CheckCollisionBoxSphere(BoundingBox box, Vector3 center, float radius); // Detect collision between box and sphere -RLAPI bool CheckCollisionRaySphere(Ray ray, Vector3 center, float radius); // Detect collision between ray and sphere -RLAPI bool CheckCollisionRaySphereEx(Ray ray, Vector3 center, float radius, Vector3 *collisionPoint); // Detect collision between ray and sphere, returns collision point -RLAPI bool CheckCollisionRayBox(Ray ray, BoundingBox box); // Detect collision between ray and box -RLAPI RayHitInfo GetCollisionRayMesh(Ray ray, Mesh mesh, Matrix transform); // Get collision info between ray and mesh -RLAPI RayHitInfo GetCollisionRayModel(Ray ray, Model model); // Get collision info between ray and model -RLAPI RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle -RLAPI RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight); // Get collision info between ray and ground plane (Y-normal plane) +RLAPI RayCollision GetRayCollisionSphere(Ray ray, Vector3 center, float radius); // Get collision info between ray and sphere +RLAPI RayCollision GetRayCollisionBox(Ray ray, BoundingBox box); // Get collision info between ray and box +RLAPI RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform); // Get collision info between ray and mesh +RLAPI RayCollision GetRayCollisionModel(Ray ray, Model model); // Get collision info between ray and model +RLAPI RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle +RLAPI RayCollision GetRayCollisionGround(Ray ray, float groundHeight); // Get collision info between ray and ground plane (Y-normal plane) //------------------------------------------------------------------------------------ // Audio Loading and Playing Functions (Module: audio)