diff --git a/examples/models/models_mesh_picking.c b/examples/models/models_mesh_picking.c index f48683e4a..38378bc4c 100644 --- a/examples/models/models_mesh_picking.c +++ b/examples/models/models_mesh_picking.c @@ -121,9 +121,22 @@ int main(void) cursorColor = ORANGE; hitObjectName = "Box"; - // Check ray collision against model - // NOTE: It considers model.transform matrix! - RayCollision meshHitInfo = GetRayCollisionModel(ray, tower); + // Check ray collision against model meshes + RayCollision meshHitInfo = { 0 }; + for (int m = 0; m < tower.meshCount; m++) + { + // NOTE: We consider the model.transform for the collision check but + // it can be checked against any transform Matrix, used when checking against same + // model drawn multiple times with multiple transforms + meshHitInfo = GetRayCollisionMesh(ray, tower.meshes[m], tower.transform); + if (meshHitInfo.hit) + { + // Save the closest hit mesh + if ((!collision.hit) || (collision.distance > meshHitInfo.distance)) collision = meshHitInfo; + + break; // Stop once one mesh collision is detected, the colliding mesh is m + } + } if (meshHitInfo.hit) { diff --git a/src/raylib.h b/src/raylib.h index fca512a6d..1add734b5 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1466,7 +1466,6 @@ RLAPI bool CheckCollisionBoxes(BoundingBox box1, BoundingBox box2); RLAPI bool CheckCollisionBoxSphere(BoundingBox box, Vector3 center, float radius); // Check collision between box and sphere 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 GetRayCollisionModel(Ray ray, Model model); // Get collision info between ray and model RLAPI RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform); // Get collision info between ray and mesh RLAPI RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle RLAPI RayCollision GetRayCollisionQuad(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4); // Get collision info between ray and quad diff --git a/src/rmodels.c b/src/rmodels.c index cd5d01f23..9f5cfd397 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -3640,25 +3640,6 @@ RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform) return collision; } -// Get collision info between ray and model -RayCollision GetRayCollisionModel(Ray ray, Model model) -{ - RayCollision collision = { 0 }; - - for (int m = 0; m < model.meshCount; m++) - { - RayCollision meshHitInfo = GetRayCollisionMesh(ray, model.meshes[m], model.transform); - - if (meshHitInfo.hit) - { - // Save the closest hit mesh - if ((!collision.hit) || (collision.distance > meshHitInfo.distance)) collision = meshHitInfo; - } - } - - return collision; -} - // Get collision info between ray and triangle // NOTE: The points are expected to be in counter-clockwise winding // NOTE: Based on https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm