diff options
| author | victorfisac <victorfisac@gmail.com> | 2016-01-07 16:18:24 +0100 |
|---|---|---|
| committer | victorfisac <victorfisac@gmail.com> | 2016-01-07 16:18:24 +0100 |
| commit | 1793f2c3b87f1ed487216e23f3074085753ee346 (patch) | |
| tree | 56b24a868de2522c8ea50930f562b6a28e326ce2 | |
| parent | 4a637191f21112de17d92055cd06d645f31627d3 (diff) | |
| download | raylib-1793f2c3b87f1ed487216e23f3074085753ee346.tar.gz raylib-1793f2c3b87f1ed487216e23f3074085753ee346.zip | |
Added collision check between ray and box
- Added CheckCollisionRayBox() function.
- Updated and improved core 3d picking example (currently working as
expected).
| -rw-r--r-- | examples/core_3d_picking.c | 14 | ||||
| -rw-r--r-- | src/models.c | 20 | ||||
| -rw-r--r-- | src/raylib.h | 1 |
3 files changed, 32 insertions, 3 deletions
diff --git a/examples/core_3d_picking.c b/examples/core_3d_picking.c index 2fc05e81..0d6f4ac7 100644 --- a/examples/core_3d_picking.c +++ b/examples/core_3d_picking.c @@ -24,9 +24,12 @@ int main() Camera camera = {{ 0.0, 10.0, 10.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }}; Vector3 cubePosition = { 0.0, 1.0, 0.0 }; + Vector3 cubeSize = { 2.0, 2.0, 2.0 }; Ray ray; // Picking line ray + bool collision = false; + SetCameraMode(CAMERA_FREE); // Set a free camera mode SetCameraPosition(camera.position); // Set internal camera position to match our camera position @@ -45,7 +48,10 @@ int main() // NOTE: This function is NOT WORKING properly! ray = GetMouseRay(GetMousePosition(), camera); - // TODO: Check collision between ray and box + // Check collision between ray and box + collision = CheckCollisionRayBox(ray, + (Vector3){cubePosition.x - cubeSize.x / 2,cubePosition.y - cubeSize.y / 2,cubePosition.z - cubeSize.z / 2}, + (Vector3){cubePosition.x + cubeSize.x / 2,cubePosition.y + cubeSize.y / 2,cubePosition.z + cubeSize.z / 2}); } //---------------------------------------------------------------------------------- @@ -57,8 +63,8 @@ int main() Begin3dMode(camera); - DrawCube(cubePosition, 2, 2, 2, GRAY); - DrawCubeWires(cubePosition, 2, 2, 2, DARKGRAY); + DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY); + DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY); DrawGrid(10.0, 1.0); @@ -67,6 +73,8 @@ int main() End3dMode(); DrawText("Try selecting the box with mouse!", 240, 10, 20, GRAY); + + if(collision) DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, screenHeight * 0.1f, 30, GREEN); DrawFPS(10, 10); diff --git a/src/models.c b/src/models.c index f78be41d..dd170e0b 100644 --- a/src/models.c +++ b/src/models.c @@ -1336,6 +1336,26 @@ bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSph return collision; } +// Detect collision between ray and box +bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox) +{ + bool collision = false; + + float t[8]; + t[0] = (minBBox.x - ray.position.x) / ray.direction.x; + t[1] = (maxBBox.x - ray.position.x) / ray.direction.x; + t[2] = (minBBox.y - ray.position.y) / ray.direction.y; + t[3] = (maxBBox.y - ray.position.y) / ray.direction.y; + t[4] = (minBBox.z - ray.position.z) / ray.direction.z; + t[5] = (maxBBox.z - ray.position.z) / ray.direction.z; + t[6] = fmax(fmax(fmin(t[0], t[1]), fmin(t[2], t[3])), fmin(t[4], t[5])); + t[7] = 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]); + + return collision; +} + // TODO: Useful function to check collision area? //BoundingBox GetCollisionArea(BoundingBox box1, BoundingBox box2) diff --git a/src/raylib.h b/src/raylib.h index 72211b59..b6900a97 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -756,6 +756,7 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB); // Detect collision between two spheres bool CheckCollisionBoxes(Vector3 minBBox1, Vector3 maxBBox1, Vector3 minBBox2, Vector3 maxBBox2); // Detect collision between two boxes bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSphere, float radiusSphere); // Detect collision between box and sphere +bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox); // Detect collision between ray and box Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition, float radius); // Detect collision of player radius with cubicmap // NOTE: Return the normal vector of the impacted surface |
