From d0146fdfbb931d565fc6e976da2213dc7500f348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Tue, 5 Feb 2019 18:52:00 -0800 Subject: [PATCH] Cleanup. --- examples/29-debugdraw/debugdraw.cpp | 8 +- examples/common/bounds.cpp | 166 ++++++++++++++-------------- examples/common/bounds.h | 84 +++++++------- 3 files changed, 134 insertions(+), 124 deletions(-) diff --git a/examples/29-debugdraw/debugdraw.cpp b/examples/29-debugdraw/debugdraw.cpp index 3f4756570..dca437064 100644 --- a/examples/29-debugdraw/debugdraw.cpp +++ b/examples/29-debugdraw/debugdraw.cpp @@ -1268,15 +1268,15 @@ public: { Capsule capsuleA = { - { px+kStepX*6.0f, py-1.0f, pz+kStepZ*3.0f }, - { px+kStepX*6.0f, py+1.0f, pz+kStepZ*3.0f }, + { px+kStepX*6.0f, py-1.0f, pz+kStepZ*6.0f }, + { px+kStepX*6.0f, py+1.0f, pz+kStepZ*6.0f }, 0.5f, }; Capsule capsuleB = { - { xx+kStepX*5.9f, yy-1.0f, zz+kStepZ*3.0f+0.1f }, - { xx+kStepX*6.0f, yy+1.0f, zz+kStepZ*3.0f }, + { xx+kStepX*5.9f, yy-1.0f, zz+kStepZ*6.0f+0.1f }, + { xx+kStepX*6.0f, yy+1.0f, zz+kStepZ*6.0f }, 0.2f, }; diff --git a/examples/common/bounds.cpp b/examples/common/bounds.cpp index 853a79e53..42e791f4f 100644 --- a/examples/common/bounds.cpp +++ b/examples/common/bounds.cpp @@ -1126,89 +1126,6 @@ Vec3 closestPoint(const Triangle& _triangle, const Vec3& _point) return cartesian(_triangle, clamp(uvw, 0.0f, 1.0f) ); } -bool overlap(const Sphere& _sphere, const Vec3& _pos) -{ - const Vec3 ba = sub(_sphere.center, _pos); - const float rsq = square(_sphere.radius); - return dot(ba, ba) <= rsq; -} - -bool overlap(const Sphere& _sphereA, const Sphere& _sphereB) -{ - const Vec3 ba = sub(_sphereA.center, _sphereB.center); - const float rsq = square(_sphereA.radius + _sphereB.radius); - return dot(ba, ba) <= rsq; -} - -bool overlap(const Sphere& _sphere, const Aabb& _aabb) -{ - const Vec3 pos = closestPoint(_aabb, _sphere.center); - return overlap(_sphere, pos); -} - -bool overlap(const Sphere& _sphere, const Plane& _plane) -{ - return bx::abs(distance(_plane, _sphere.center) ) <= _sphere.radius; -} - -bool overlap(const Sphere& _sphere, const Triangle& _triangle) -{ - Plane plane; - calcPlane(plane, _triangle); - - if (!overlap(_sphere, plane) ) - { - return false; - } - - const Vec3 pos = closestPoint(plane, _sphere.center); - const Vec3 uvw = barycentric(_triangle, pos); - const float nr = -_sphere.radius; - - return uvw.x >= nr - && uvw.y >= nr - && uvw.z >= nr - ; -} - -bool overlap(const Sphere& _sphere, const Cylinder& _cylinder) -{ - BX_UNUSED(_sphere, _cylinder); - return false; -} - -bool overlap(const Sphere& _sphere, const Capsule& _capsule) -{ - const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _sphere.center); - return overlap(_sphere, Sphere{pos, _capsule.radius}); -} - -bool overlap(const Sphere& _sphere, const Cone& _cone) -{ - float tt; - const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, _sphere.center, tt); - return overlap(_sphere, Sphere{pos, lerp(_cone.radius, 0.0f, tt)}); -} - -bool overlap(const Sphere& _sphere, const Disk& _disk) -{ - if (!overlap(_sphere, Sphere{_disk.center, _disk.radius}) ) - { - return false; - } - - Plane plane; - calcPlane(plane, _disk.normal, _disk.center); - - return overlap(_sphere, plane); -} - -bool overlap(const Sphere& _sphere, const Obb& _obb) -{ - const Vec3 pos = closestPoint(_obb, _sphere.center); - return overlap(_sphere, pos); -} - bool overlap(const Aabb& _aabb, const Vec3& _pos) { const Vec3 ac = getCenter(_aabb); @@ -1469,6 +1386,89 @@ bool overlap(const Capsule& _capsule, const Obb& _obb) return false; } +bool overlap(const Sphere& _sphere, const Vec3& _pos) +{ + const Vec3 ba = sub(_sphere.center, _pos); + const float rsq = square(_sphere.radius); + return dot(ba, ba) <= rsq; +} + +bool overlap(const Sphere& _sphereA, const Sphere& _sphereB) +{ + const Vec3 ba = sub(_sphereA.center, _sphereB.center); + const float rsq = square(_sphereA.radius + _sphereB.radius); + return dot(ba, ba) <= rsq; +} + +bool overlap(const Sphere& _sphere, const Aabb& _aabb) +{ + const Vec3 pos = closestPoint(_aabb, _sphere.center); + return overlap(_sphere, pos); +} + +bool overlap(const Sphere& _sphere, const Plane& _plane) +{ + return bx::abs(distance(_plane, _sphere.center) ) <= _sphere.radius; +} + +bool overlap(const Sphere& _sphere, const Triangle& _triangle) +{ + Plane plane; + calcPlane(plane, _triangle); + + if (!overlap(_sphere, plane) ) + { + return false; + } + + const Vec3 pos = closestPoint(plane, _sphere.center); + const Vec3 uvw = barycentric(_triangle, pos); + const float nr = -_sphere.radius; + + return uvw.x >= nr + && uvw.y >= nr + && uvw.z >= nr + ; +} + +bool overlap(const Sphere& _sphere, const Cylinder& _cylinder) +{ + BX_UNUSED(_sphere, _cylinder); + return false; +} + +bool overlap(const Sphere& _sphere, const Capsule& _capsule) +{ + const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _sphere.center); + return overlap(_sphere, Sphere{pos, _capsule.radius}); +} + +bool overlap(const Sphere& _sphere, const Cone& _cone) +{ + float tt; + const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, _sphere.center, tt); + return overlap(_sphere, Sphere{pos, lerp(_cone.radius, 0.0f, tt)}); +} + +bool overlap(const Sphere& _sphere, const Disk& _disk) +{ + if (!overlap(_sphere, Sphere{_disk.center, _disk.radius}) ) + { + return false; + } + + Plane plane; + calcPlane(plane, _disk.normal, _disk.center); + + return overlap(_sphere, plane); +} + +bool overlap(const Sphere& _sphere, const Obb& _obb) +{ + const Vec3 pos = closestPoint(_obb, _sphere.center); + return overlap(_sphere, pos); +} + bool overlap(const Triangle& _triangle, const Vec3& _pos) { const Vec3 uvw = barycentric(_triangle, _pos); diff --git a/examples/common/bounds.h b/examples/common/bounds.h index 338ab41e7..1997e3855 100644 --- a/examples/common/bounds.h +++ b/examples/common/bounds.h @@ -8,19 +8,14 @@ #include +/// struct Aabb { bx::Vec3 min; bx::Vec3 max; }; -struct Cylinder -{ - bx::Vec3 pos; - bx::Vec3 end; - float radius; -}; - +/// struct Capsule { bx::Vec3 pos; @@ -28,6 +23,7 @@ struct Capsule float radius; }; +/// struct Cone { bx::Vec3 pos; @@ -35,6 +31,15 @@ struct Cone float radius; }; +/// +struct Cylinder +{ + bx::Vec3 pos; + bx::Vec3 end; + float radius; +}; + +/// struct Disk { bx::Vec3 center; @@ -42,17 +47,20 @@ struct Disk float radius; }; +/// struct Obb { float mtx[16]; }; +/// struct Sphere { bx::Vec3 center; float radius; }; +/// struct Triangle { bx::Vec3 v0; @@ -60,12 +68,14 @@ struct Triangle bx::Vec3 v2; }; +/// struct Ray { bx::Vec3 pos; bx::Vec3 dir; }; +/// struct Hit { bx::Vec3 pos; @@ -165,36 +175,6 @@ bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit = NULL); /// Intersect ray / triangle. bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit = NULL); -/// -bool overlap(const Sphere& _sphere, const bx::Vec3& _pos); - -/// -bool overlap(const Sphere& _sphereA, const Sphere& _sphereB); - -/// -bool overlap(const Sphere& _sphere, const Aabb& _aabb); - -/// -bool overlap(const Sphere& _sphere, const bx::Plane& _plane); - -/// -bool overlap(const Sphere& _sphere, const Triangle& _triangle); - -/// -bool overlap(const Sphere& _sphere, const Cylinder& _cylinder); - -/// -bool overlap(const Sphere& _sphere, const Capsule& _capsule); - -/// -bool overlap(const Sphere& _sphere, const Cone& _cone); - -/// -bool overlap(const Sphere& _sphere, const Disk& _disk); - -/// -bool overlap(const Sphere& _sphere, const Obb& _obb); - /// bool overlap(const Aabb& _aabb, const bx::Vec3& _pos); @@ -255,6 +235,36 @@ bool overlap(const Capsule& _capsule, const Disk& _disk); /// bool overlap(const Capsule& _capsule, const Obb& _obb); +/// +bool overlap(const Sphere& _sphere, const bx::Vec3& _pos); + +/// +bool overlap(const Sphere& _sphereA, const Sphere& _sphereB); + +/// +bool overlap(const Sphere& _sphere, const Aabb& _aabb); + +/// +bool overlap(const Sphere& _sphere, const bx::Plane& _plane); + +/// +bool overlap(const Sphere& _sphere, const Triangle& _triangle); + +/// +bool overlap(const Sphere& _sphere, const Cylinder& _cylinder); + +/// +bool overlap(const Sphere& _sphere, const Capsule& _capsule); + +/// +bool overlap(const Sphere& _sphere, const Cone& _cone); + +/// +bool overlap(const Sphere& _sphere, const Disk& _disk); + +/// +bool overlap(const Sphere& _sphere, const Obb& _obb); + /// bool overlap(const Triangle& _triangle, const bx::Vec3& _pos);