From 136408d8b8b096f23953aba3d81b3fbeccdc2680 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 27 Mar 2016 19:42:57 +0200 Subject: [PATCH] Corrected bug on bounding box if mesh is not loaded properly it breaks the game! --- src/models.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/models.c b/src/models.c index 52c68f9b..a515dd86 100644 --- a/src/models.c +++ b/src/models.c @@ -1328,13 +1328,19 @@ bool CheckCollisionRayBox(Ray ray, BoundingBox box) BoundingBox CalculateBoundingBox(Mesh mesh) { // Get min and max vertex to construct bounds (AABB) - Vector3 minVertex = (Vector3){ mesh.vertices[0], mesh.vertices[1], mesh.vertices[2] }; - Vector3 maxVertex = (Vector3){ mesh.vertices[0], mesh.vertices[1], mesh.vertices[2] }; + Vector3 minVertex = { 0 }; + Vector3 maxVertex = { 0 }; - for (int i = 1; i < mesh.vertexCount; i++) + if (mesh.vertices != NULL) { - minVertex = VectorMin(minVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] }); - maxVertex = VectorMax(maxVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] }); + minVertex = (Vector3){ mesh.vertices[0], mesh.vertices[1], mesh.vertices[2] }; + maxVertex = (Vector3){ mesh.vertices[0], mesh.vertices[1], mesh.vertices[2] }; + + for (int i = 1; i < mesh.vertexCount; i++) + { + minVertex = VectorMin(minVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] }); + maxVertex = VectorMax(maxVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] }); + } } // Create the bounding box