diff options
| author | Crydsch <[email protected]> | 2021-06-03 20:15:27 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-06-03 20:15:27 +0200 |
| commit | edeaff4bd4010eaa46f09f47d0b78f27b4ff836e (patch) | |
| tree | dc9c42779ae678ff65f29937dfe502e3ff6312da /examples/models | |
| parent | 2370af598e84c8594b8d1a4f6e17925b48949db7 (diff) | |
| download | raylib-edeaff4bd4010eaa46f09f47d0b78f27b4ff836e.tar.gz raylib-edeaff4bd4010eaa46f09f47d0b78f27b4ff836e.zip | |
Better collisions (#1803)
* review collisions ray-box and ray-sphere
* Applied raysan's refactor
Improved GetRayCollisionBox
* Replace GetRayCollisionGround with GetCollisionQuad
* Update example core_3d_picking
* Update example models_loading
* Fixed issues after merge
* remove debug stuff
Co-authored-by: Cry dsch <[email protected]>
Diffstat (limited to 'examples/models')
| -rw-r--r-- | examples/models/models_mesh_picking.c | 61 |
1 files changed, 40 insertions, 21 deletions
diff --git a/examples/models/models_mesh_picking.c b/examples/models/models_mesh_picking.c index 31500bce..a790e978 100644 --- a/examples/models/models_mesh_picking.c +++ b/examples/models/models_mesh_picking.c @@ -41,16 +41,24 @@ int main(void) Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position BoundingBox towerBBox = GetMeshBoundingBox(tower.meshes[0]); // Get mesh bounding box - bool hitMeshBBox = false; - bool hitTriangle = false; + + // Ground quad + Vector3 g0 = (Vector3){ -50.0f, 0.0f, -50.0f }; + Vector3 g1 = (Vector3){ -50.0f, 0.0f, 50.0f }; + Vector3 g2 = (Vector3){ 50.0f, 0.0f, 50.0f }; + Vector3 g3 = (Vector3){ 50.0f, 0.0f, -50.0f }; // Test triangle - Vector3 ta = (Vector3){ -25.0, 0.5, 0.0 }; - Vector3 tb = (Vector3){ -4.0, 2.5, 1.0 }; - Vector3 tc = (Vector3){ -8.0, 6.5, 0.0 }; + Vector3 ta = (Vector3){ -25.0f, 0.5f, 0.0f }; + Vector3 tb = (Vector3){ -4.0f, 2.5f, 1.0f }; + Vector3 tc = (Vector3){ -8.0f, 6.5f, 0.0f }; Vector3 bary = { 0.0f, 0.0f, 0.0f }; + // Test sphere + Vector3 sp = (Vector3){ -30.0f, 5.0f, 5.0f }; + float sr = 4.0f; + SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second @@ -69,11 +77,11 @@ int main(void) collision.hit = false; Color cursorColor = WHITE; - // Get ray and test against ground, triangle, and mesh + // Get ray and test against objects ray = GetMouseRay(GetMousePosition(), camera); - // Check ray collision aginst ground plane - RayCollision groundHitInfo = GetRayCollisionGround(ray, 0.0f); + // Check ray collision against ground quad + RayCollision groundHitInfo = GetRayCollisionQuad(ray, g0, g1, g2, g3); if ((groundHitInfo.hit) && (groundHitInfo.distance < collision.distance)) { @@ -92,30 +100,37 @@ int main(void) hitObjectName = "Triangle"; bary = Vector3Barycenter(collision.point, ta, tb, tc); - hitTriangle = true; } - else hitTriangle = false; - - RayCollision meshHitInfo = { 0 }; + + // Check ray collision against test sphere + RayCollision sphereHitInfo = GetRayCollisionSphere(ray, sp, sr); + + if ((sphereHitInfo.hit) && (sphereHitInfo.distance < collision.distance)) { + collision = sphereHitInfo; + cursorColor = ORANGE; + hitObjectName = "Sphere"; + } // Check ray collision against bounding box first, before trying the full ray-mesh test - if (GetRayCollisionBox(ray, towerBBox).hit) + RayCollision boxHitInfo = GetRayCollisionBox(ray, towerBBox); + + if ((boxHitInfo.hit) && (boxHitInfo.distance < collision.distance)) { - hitMeshBBox = true; + collision = boxHitInfo; + cursorColor = ORANGE; + hitObjectName = "Box"; // Check ray collision against model // NOTE: It considers model.transform matrix! - meshHitInfo = GetRayCollisionModel(ray, tower); + RayCollision meshHitInfo = GetRayCollisionModel(ray, tower); - if ((meshHitInfo.hit) && (meshHitInfo.distance < collision.distance)) + if (meshHitInfo.hit) { collision = meshHitInfo; cursorColor = ORANGE; hitObjectName = "Mesh"; } } - - hitMeshBBox = false; //---------------------------------------------------------------------------------- // Draw @@ -136,8 +151,11 @@ int main(void) DrawLine3D(tb, tc, PURPLE); DrawLine3D(tc, ta, PURPLE); + // Draw the test sphere + DrawSphereWires(sp, sr, 8, 8, PURPLE); + // Draw the mesh bbox if we hit it - if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME); + if (boxHitInfo.hit) DrawBoundingBox(towerBBox, LIME); // If we hit something, draw the cursor at the hit point if (collision.hit) @@ -154,7 +172,7 @@ int main(void) } DrawRay(ray, MAROON); - + DrawGrid(10, 10.0f); EndMode3D(); @@ -178,7 +196,8 @@ int main(void) 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); + if (triHitInfo.hit && strcmp(hitObjectName, "Triangle") == 0) + DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK); } DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY); |
