diff --git a/C++/scripts/build.sh b/C++/scripts/build.sh index fe86185..bbdb7f6 100755 --- a/C++/scripts/build.sh +++ b/C++/scripts/build.sh @@ -1,6 +1,11 @@ #Compiles the entire program then runs it, producing an executable. If the "test" argument is included, will try and run tests too (in the test folder) #C++ printf "Running program...\n\n\n" +output=$(dpkg -l | grep libx11-dev) +if [[ -z $output ]] +then + sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev +fi if [ "$1" = "test" ] then printf "Running tests...\n" diff --git a/C++/scripts/debug.sh b/C++/scripts/debug.sh index eca8844..2854ad6 100755 --- a/C++/scripts/debug.sh +++ b/C++/scripts/debug.sh @@ -1,6 +1,11 @@ #Compiles the entire program with debug flags then runs it in gdb. If the "test" argument is included, will try and run tests too (in the test folder) #C++ printf "Running program...\n\n\n" +output=$(dpkg -l | grep libx11-dev) +if [[ -z $output ]] +then + sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev +fi if [ "$1" = "test" ] then printf "Running tests...\n" diff --git a/C++/scripts/md5 b/C++/scripts/md5 index 47f3924..45f0dae 100644 --- a/C++/scripts/md5 +++ b/C++/scripts/md5 @@ -1,7 +1,7 @@ -build.sh:6fbec04ed16be42bab846f1eed603133 - +build.sh:d5e4e98a6f677aa0a9759ba0afb2fb77 - commit.sh:d03a46e721060c22ccb146e19d27e70a - -debug.sh:7f57c6640be5f262988961c3b45dce97 - +debug.sh:849488515cab075948653c15eec4177b - lines.sh:3b907786f7fc9204025993016c9080de - -release.sh:0a525311cc14b9c8aefc6f2b816129a1 - +release.sh:6a96fb84ba64ed60d31be436ec069f05 - temp:d41d8cd98f00b204e9800998ecf8427e - -web.sh:96f2c316536011a3defac50aecae487d - +web.sh:b982212b88b80a59ad607b47d2ff3e94 - diff --git a/C++/scripts/release.sh b/C++/scripts/release.sh index 807a40b..2f84395 100755 --- a/C++/scripts/release.sh +++ b/C++/scripts/release.sh @@ -1,6 +1,11 @@ #Creates a release build that focuses on high runtime performance. #C++ printf "Running program...\n\n\n" +output=$(dpkg -l | grep libx11-dev) +if [[ -z $output ]] +then + sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev +fi if g++ $(find . -type f -name "*.cpp" -not -path "./test/*") ${CUSTOM_PARAMS} -O3 -s -DNDEBUG -o ${PROJECT_NAME}; then ./${PROJECT_NAME} "$@" fi diff --git a/C++/scripts/web.sh b/C++/scripts/web.sh index 991d44f..c46d605 100755 --- a/C++/scripts/web.sh +++ b/C++/scripts/web.sh @@ -1,5 +1,10 @@ #Compiles emscripten instance of this project for the web. #C++ +output=$(dpkg -l | grep libx11-dev) +if [[ -z $output ]] +then + sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev +fi if [ -d "assets" ]; then em++ -std=c++17 -O2 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s USE_SDL_MIXER=2 -s USE_LIBPNG=1 $(find . -type f -name "*.cpp" -not -path "./test/*") -o ${PROJECT_NAME}.html -I pixelGameEngine.h --preload-file ./assets else diff --git a/C++ProjectTemplate b/C++ProjectTemplate index 6814313..c1aa29d 100755 Binary files a/C++ProjectTemplate and b/C++ProjectTemplate differ diff --git a/Path.cpp b/Path.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Path.h b/Path.h new file mode 100644 index 0000000..e69de29 diff --git a/main.cpp b/main.cpp index 8eb04dd..8ab7e53 100644 --- a/main.cpp +++ b/main.cpp @@ -1,188 +1,89 @@ #define OLC_PGE_APPLICATION #include "pixelGameEngine.h" +#include "olcutils.h" -// PGEX Require the presence of olc::PixelGameEngine -#define OLC_PGEX_QUICKGUI -#include "quickgui.h" - -namespace olc::QuickGUI{ - class PictureBox : public olc::QuickGUI::BaseControl{ - private: - olc::vi2d clickPos; - public: - PictureBox(olc::QuickGUI::Manager& manager, // Associate with a Manager - const olc::vf2d& pos, - const olc::vf2d& size, - const olc::vf2d& offset, - olc::Renderable& image); - - public: - // Minium value - olc::vf2d offset; - olc::vf2d pos; - olc::vf2d size; - olc::Renderableℑ - - public: // BaseControl overrides - void Update(olc::PixelGameEngine* pge) override; - void Draw(olc::PixelGameEngine* pge) override; - void DrawDecal(olc::PixelGameEngine* pge) override; - }; - - PictureBox::PictureBox(olc::QuickGUI::Manager& manager, const olc::vf2d& pos, const olc::vf2d& size, const olc::vf2d& offset,olc::Renderable& image) - : BaseControl(manager),pos(pos),size(size),offset(offset),image(image){} - - void PictureBox::Update(olc::PixelGameEngine* pge) - { - bPressed = false; - bReleased = false; - float fElapsedTime = pge->GetElapsedTime(); - - olc::vf2d vMouse = pge->GetMousePos(); - if (m_state != State::Click) - { - if (vMouse.x >= pos.x && vMouse.x < pos.x + size.x && - vMouse.y >= pos.y && vMouse.y < pos.y + size.y) - { - m_fTransition += fElapsedTime * m_manager.fHoverSpeedOn; - m_state = State::Hover; - - bPressed = pge->GetMouse(olc::Mouse::LEFT).bPressed; - if (bPressed) - { - m_state = State::Click; - clickPos = vMouse; - } - - bHeld = pge->GetMouse(olc::Mouse::LEFT).bHeld; - } - else - { - m_fTransition -= fElapsedTime * m_manager.fHoverSpeedOff; - m_state = State::Normal; - } - } - else - { - bHeld = pge->GetMouse(olc::Mouse::LEFT).bHeld; - bReleased = pge->GetMouse(olc::Mouse::LEFT).bReleased; - if (bReleased) m_state = State::Normal; - offset+=clickPos-vMouse; - offset.x=std::clamp((int)offset.x,0,image.Sprite()->width-(int)size.x); - offset.y=std::clamp((int)offset.y,0,image.Sprite()->height-(int)size.y); - clickPos=vMouse; - } - } +using namespace olc; - void PictureBox::Draw(olc::PixelGameEngine* pge) +class Example : public olc::PixelGameEngine +{ +public: + Example() { - if (!bVisible) - return; - switch (m_state) - { - case State::Disabled: { - pge->FillRect(pos,size,m_manager.colDisable); - }break; - case State::Normal: - case State::Hover: { - pge->FillRect(pos,size,olc::PixelLerp(m_manager.colNormal, m_manager.colHover, m_fTransition)); - }break; - case State::Click: { - pge->FillRect(pos,size,m_manager.colClick); - }break; - } - pge->DrawPartialSprite({(int)pos.x+1,(int)pos.y+1},image.Sprite(),offset,{(int)size.x-2,(int)size.y-2}); - pge->DrawRect(pos,size,m_manager.colBorder); + sAppName = "Example"; } - void PictureBox::DrawDecal(olc::PixelGameEngine* pge) - { - if (!bVisible) - return; - - switch (m_state) - { - case State::Disabled: { - pge->FillRectDecal(pos,size,olc::PixelLerp(m_manager.colNormal, m_manager.colHover, m_fTransition)); - }break; - case State::Normal: - case State::Hover: { - pge->FillRectDecal(pos,size,olc::PixelLerp(m_manager.colNormal, m_manager.colHover, m_fTransition)); - }break; - case State::Click: { - pge->FillRectDecal(pos,size,m_manager.colClick); - }break; - } - pge->DrawPartialDecal({pos.x+1,pos.y+1},image.Decal(),offset,{size.x-2,size.y-2}); - pge->SetDecalMode(olc::DecalMode::WIREFRAME); - pge->DrawRectDecal(pos,size,m_manager.colBorder); - pge->SetDecalMode(olc::DecalMode::NORMAL); - } +public: + bool RayVsRect(const vf2d ray_origin, const vf2d ray_dir, const olc::utils::geom2d::rect target, vf2d&contact_point, vf2d&contact_normal, float&t_hit_near){ -} + contact_normal = { 0, 0 }; + contact_point = { 0, 0 }; + vf2d t_near = {(target.pos.x - ray_origin.x) / ray_dir.x, (target.pos.y - ray_origin.y) / ray_dir.y}; + vf2d t_far = {(target.pos.x + target.size.x - ray_origin.x) / ray_dir.x, (target.pos.y + target.size.y - ray_origin.y) / ray_dir.y}; -class olcDemo_QuickGUI : public olc::PixelGameEngine -{ -public: - olcDemo_QuickGUI() - { - sAppName = "olcDemo_QuickGUI"; - } + if (t_near.x > t_far.x) {float b; b = t_near.x; t_near.x = t_far.x; t_far.x = b;}; + if (t_near.y > t_far.y) {float b; b = t_near.y; t_near.y = t_far.y; t_far.y = b;}; -protected: - olc::QuickGUI::Manager guiManager; + if (t_near.x > t_far.y || t_near.y > t_far.x) return false; - olc::QuickGUI::PictureBox* pictureBox = nullptr; - olc::QuickGUI::Slider* pictureBoxSliderX = nullptr; - olc::QuickGUI::Slider* pictureBoxSliderY = nullptr; - olc::Renderable dirtimg; + t_hit_near = fmax(t_near.x, t_near.y); + float t_hit_far = fmin(t_far.x, t_far.y); -public: - bool OnUserCreate() override - { - dirtimg.Load("dirtblock.png"); + if (t_hit_far < 0) return false; - pictureBox = new olc::QuickGUI::PictureBox(guiManager, - { 10.0f, 10.0f }, { 128.0f, 128.0f }, {0,0}, dirtimg); - pictureBoxSliderX = new olc::QuickGUI::Slider(guiManager,{pictureBox->pos.x,pictureBox->pos.y+pictureBox->size.y+16},{pictureBox->pos.x+pictureBox->size.x,pictureBox->pos.y+pictureBox->size.y+16}, - 0,pictureBox->image.Sprite()->width-(int)pictureBox->size.x,pictureBox->offset.x); - pictureBoxSliderY = new olc::QuickGUI::Slider(guiManager,{pictureBox->pos.x+pictureBox->size.x+16,pictureBox->pos.y},{pictureBox->pos.x+pictureBox->size.x+16,pictureBox->pos.y+pictureBox->size.y}, - 0,pictureBox->image.Sprite()->height-(int)pictureBox->size.y,pictureBox->offset.y); + contact_point.x = ray_origin.x + t_hit_near * ray_dir.x; + contact_point.y = ray_origin.y + t_hit_near * ray_dir.y; - guiManager.AddControl(pictureBox); - guiManager.AddControl(pictureBoxSliderX); + if (t_near.x > t_near.y) + if ( 1.0f / ray_dir.x < 0) + contact_normal = { 1, 0 }; + else + contact_normal = { -1, 0}; + else + if ( t_near.x < t_near.y) + if ( 1.0f / ray_dir.y < 0) + contact_normal = { 0, 1 }; + else + contact_normal = { 0, -1 }; + + return true; + + + } + vf2d originPoint={16,16}; + bool OnUserCreate() override + { + // Called once at the start, so create things here return true; } bool OnUserUpdate(float fElapsedTime) override { - guiManager.Update(this); - - if (pictureBoxSliderX->bHeld) { - pictureBox->offset.x=pictureBoxSliderX->fValue; + vf2d velocity={(GetKey(D).bHeld-GetKey(A).bHeld)*20*fElapsedTime,(GetKey(S).bHeld-GetKey(W).bHeld)*20*fElapsedTime}; + vf2d contact_point; + vf2d contact_normal; + float t_hit_near; + + Clear(Pixel(64,64,255)); + if (!olc::utils::geom2d::overlaps(olc::utils::geom2d::circle{originPoint+velocity,5},olc::utils::geom2d::rect{{32,32},{64,32}})) { + originPoint+=velocity; + DrawCircle(originPoint,5); } else { - pictureBoxSliderX->fValue=pictureBox->offset.x; + DrawCircle(originPoint,5,RED); } + DrawLine(originPoint,GetMousePos()); - if (pictureBoxSliderY->bHeld) { - pictureBox->offset.y=pictureBoxSliderY->fValue; - } else { - pictureBoxSliderY->fValue=pictureBox->offset.y; - } - - // Draw Stuff! - Clear(olc::BLACK); - - guiManager.DrawDecal(this); + DrawRect({32,32},{64,32},RayVsRect(originPoint, GetMousePos()-originPoint, olc::utils::geom2d::rect{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE); return true; } }; + int main() { - olcDemo_QuickGUI demo; - if (demo.Construct(256, 240, 4, 4)) + Example demo; + if (demo.Construct(128, 120, 8, 8)) demo.Start(); + return 0; -} \ No newline at end of file +} diff --git a/olcutils.h b/olcutils.h new file mode 100644 index 0000000..4d7e9d7 --- /dev/null +++ b/olcutils.h @@ -0,0 +1,1024 @@ +/* + OneLoneCoder - Geometry 2D v1.00 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + A collection of 2D Geometric primitives and functions to work with + and between them. + + + License (OLC-3) + ~~~~~~~~~~~~~~~ + + Copyright 2018 - 2022 OneLoneCoder.com + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions or derivations of source code must retain the above + copyright notice, this list of conditions and the following disclaimer. + + 2. Redistributions or derivative works in binary form must reproduce + the above copyright notice. This list of conditions and the following + disclaimer must be reproduced in the documentation and/or other + materials provided with the distribution. + + 3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Links + ~~~~~ + YouTube: https://www.youtube.com/javidx9 + Discord: https://discord.gg/WhwHUMV + Twitter: https://www.twitter.com/javidx9 + Twitch: https://www.twitch.tv/javidx9 + GitHub: https://www.github.com/onelonecoder + Homepage: https://www.onelonecoder.com + + Author + ~~~~~~ + David Barr, aka javidx9, �OneLoneCoder 2019, 2020, 2021, 2022 + +*/ + +#include "pixelGameEngine.h" + +namespace olc::utils::geom2d +{ + // Lemon Meringue + const double pi = 3.141592653589793238462643383279502884; + + // Floating point error margin + const double epsilon = 0.001; + + //https://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c + template + constexpr int sgn(T val) { return (T(0) < val) - (val < T(0)); } + + // Defines a line segment + template + struct line + { + olc::v2d_generic start; + olc::v2d_generic end; + + inline line(const olc::v2d_generic& s = { T(0), T(0) }, + const olc::v2d_generic& e = { T(0), T(0) }) + : start(s), end(e) + { } + + + // Get length of line + inline constexpr T length() + { + return (end - start).mag(); + } + + // Get length of line^2 + inline constexpr T length2() + { + return (end - start).mag2(); + } + + inline constexpr olc::v2d_generic vector() const + { + return (end - start); + } + + // Given a real distance, get point along line + inline constexpr olc::v2d_generic rpoint(const T& distance) const + { + return start + (end - start).norm() * distance; + } + + // Given a unit distance, get point along line + inline constexpr olc::v2d_generic upoint(const T& distance) const + { + return start + (end - start) * distance; + } + + // Return which side of the line does a point lie + inline constexpr int32_t side(const olc::v2d_generic& point) const + { + double d = (end - start).cross(point - start); + if (d < 0) + return -1; + else + if (d > 0) + return 1; + else + return 0; + } + }; + + template + struct ray + { + olc::v2d_generic origin; + olc::v2d_generic direction; + }; + + template + struct rect + { + olc::v2d_generic pos; + olc::v2d_generic size; + + inline rect(const olc::v2d_generic& p = { T(0), T(0) }, + const olc::v2d_generic& s = { T(1), T(1) }) + : pos(p), size(s) + { } + + inline olc::v2d_generic middle() const + { + return pos + (size * double(0.5)); + } + + // Get line segment from top side of rectangle + inline line top() const + { + return { pos, {pos.x + size.x, pos.y } }; + } + + // Get line segment from bottom side of rectangle + inline line bottom() const + { + return { {pos.x, pos.y + size.y}, pos + size }; + } + + // Get line segment from left side of rectangle + inline line left() const + { + return { pos, {pos.x, pos.y + size.y} }; + } + + // Get line segment from right side of rectangle + inline line right() const + { + return { {pos.x + size.x, pos.y }, pos + size }; + } + + // Get a line from an indexed side, starting top, going clockwise + inline line side(const size_t i) const + { + if (i & 0b11 == 0) return top(); + if (i & 0b11 == 1) return right(); + if (i & 0b11 == 2) return bottom(); + if (i & 0b11 == 3) return left(); + } + + // Get area of rectangle + inline constexpr T area() const + { + return size.x * size.y; + } + + // Get perimeter of rectangle + inline constexpr T perimeter() const + { + return T(2) * (size.x + size.y); + } + }; + + + template + struct circle + { + olc::v2d_generic pos; + T radius = T(0); + + inline circle(const olc::v2d_generic& p = { T(0), T(0) }, const T r = T(0)) + : pos(p), radius(r) + { } + + // Get area of circle + inline constexpr T area() const + { + return T(pi) * radius * radius; + } + + // Get circumference of circle + inline constexpr T perimeter() const + { + return T(2.0 * pi) * radius; + } + + // Get circumference of circle + inline constexpr T circumference() const + { + return perimeter(); + } + }; + + + template + struct triangle + { + std::array, 3> pos; + + inline triangle( + const olc::v2d_generic& p0 = { T(0), T(0) }, + const olc::v2d_generic& p1 = { T(0), T(0) }, + const olc::v2d_generic& p2 = { T(0), T(0) }) + : pos{ p0,p1,p2 } + { } + + // Get a line from an indexed side, starting top, going clockwise + inline line side(const size_t i) const + { + return line(pos[i % 3], pos[(i + 1) % 3]); + } + + // Get area of triangle + inline constexpr T area() const + { + return double(0.5) * std::abs( + (pos[0].x * (pos[1].y - pos[2].y)) + + (pos[1].x * (pos[2].y - pos[0].y)) + + (pos[2].x * (pos[0].y - pos[1].y))); + } + + // Get perimeter of triangle + inline constexpr T perimeter() const + { + return line(pos[0], pos[1]).length() + + line(pos[1], pos[2]).length() + + line(pos[2], pos[0]).length(); + } + }; + + + template + struct polygon + { + std::vector> vPoints; + }; + + + // ========================================================================================================================= + // Closest(shape, point) =================================================================================================== + + // Returns closest point to point + template + inline olc::v2d_generic closest(const olc::v2d_generic& p1, const olc::v2d_generic& p2) + { + return p1; + } + + // Returns closest point on line to point + template + inline olc::v2d_generic closest(const line& l, const olc::v2d_generic& p) + { + auto d = l.vector(); + double u = std::clamp(double(d.dot(p - l.start) / d.mag2()), 0.0, 1.0); + return l.start + d * u; + } + + // Returns closest point on circle to point + template + inline olc::v2d_generic closest(const circle& c, const olc::v2d_generic& p) + { + return c.pos + (p - c.pos).norm() * c.radius; + } + + // Returns closest point on rectangle to point + template + inline olc::v2d_generic closest(const rect& r, const olc::v2d_generic& p) + { + // This could be a "constrain" function hmmmm + // TODO: Not quite what i wanted, should restrain to boundary + return olc::v2d_generic{ std::clamp(p.x, r.pos.x, r.pos.x + r.size.x), std::clamp(p.y, r.pos.y, r.pos.y + r.size.y) }; + + } + + // Returns closest point on triangle to point + template + inline olc::v2d_generic closest(const triangle& t, const olc::v2d_generic& p) + { + // TODO: + return olc::v2d_generic(); + } + + + + + + + + + + + + // ================================================================================================================ + // POINT ========================================================================================================== + + // Checks if point contains point + template + inline constexpr bool contains(const olc::v2d_generic& p1, const olc::v2d_generic& p2) + { + return (p1 - p2).mag2() < epsilon; + } + + // Checks if line contains point + template + inline constexpr bool contains(const line& l, const olc::v2d_generic& p) + { + double d = ((p.x - l.start.x) * (l.end.y - l.start.y) - (p.y - l.start.y) * (l.end.x - l.start.x)); + if (std::abs(d) < epsilon) + { + // point is on line + double u = l.vector().dot(p - l.start) / l.vector().mag2(); + return (u >= double(0.0) && u <= double(1.0)); + } + + return false; + } + + // Checks if rectangle contains point + template + inline constexpr bool contains(const rect& r, const olc::v2d_generic& p) + { + return !(p.x < r.pos.x || p.y < r.pos.y || + p.x > (r.pos.x + r.size.x) || p.y > (r.pos.y + r.size.y)); + } + + // Checks if circle contains a point + template + inline constexpr bool contains(const circle& c, const olc::v2d_generic& p) + { + return (c.pos - p).mag2() < (c.radius * c.radius); + } + + // Checks if triangle contains a point + template + inline constexpr bool contains(const triangle& t, const olc::v2d_generic& p) + { + // http://jsfiddle.net/PerroAZUL/zdaY8/1/ + T2 A = T2(0.5) * (-t.pos[1].y * t.pos[2].x + t.pos[0].y * (-t.pos[1].x + t.pos[2].x) + t.pos[0].x * (t.pos[1].y - t.pos[2].y) + t.pos[1].x * t.pos[2].y); + T2 sign = A < T2(0) ? T2(-1) : T2(1); + T2 s = (t.pos[0].y * t.pos[2].x - t.pos[0].x * t.pos[2].y + (t.pos[2].y - t.pos[0].y) * p.x + (t.pos[0].x - t.pos[2].x) * p.y) * sign; + T2 v = (t.pos[0].x * t.pos[1].y - t.pos[0].y * t.pos[1].x + (t.pos[0].y - t.pos[1].y) * p.x + (t.pos[1].x - t.pos[0].x) * p.y) * sign; + return s > T2(0) && v > T2(0) && (s + v) < T2(2) * A * sign; + } + + + + + // Check if point overlaps with point (analagous to contains()) + template + inline constexpr bool overlaps(const olc::v2d_generic& p1, const olc::v2d_generic& p2) + { + return contains(p1, p2); + } + + // Checks if line segment overlaps with point + template + inline constexpr bool overlaps(const line& l, const olc::v2d_generic& p) + { + return contains(l, p); + } + + // Checks if rectangle overlaps with point + template + inline constexpr bool overlaps(const rect& r, const olc::v2d_generic& p) + { + return contains(r, p); + } + + // Checks if circle overlaps with point + template + inline constexpr bool overlaps(const circle& c, const olc::v2d_generic& p) + { + return contains(c, p); + } + + // Checks if triangle overlaps with point + template + inline constexpr bool overlaps(const triangle& t, const olc::v2d_generic& p) + { + return contains(t, p); + } + + + + + // Get intersection points where point intersects with point + template + inline std::vector> intersects(const olc::v2d_generic& p1, const olc::v2d_generic& p2) + { + if (contains(p1, p2)) + return { p1 }; + else + return {}; + } + + // Get intersection points where line segment intersects with point + template + inline std::vector> intersects(const line& l, const olc::v2d_generic& p) + { + if (contains(l, p)) + return { p }; + else + return {}; + } + + // Get intersection points where rectangle intersects with point + template + inline std::vector> intersects(const rect& r, const olc::v2d_generic& p) + { + std::vector> vPoints; + if (contains(r.top(), p)) vPoints.push_back(p); + if (contains(r.bottom(), p)) vPoints.push_back(p); + if (contains(r.left(), p)) vPoints.push_back(p); + if (contains(r.right(), p)) vPoints.push_back(p); + return vPoints; + } + + // Get intersection points where circle intersects with point + template + inline std::vector> intersects(const circle& c, const olc::v2d_generic& p) + { + if (std::abs((p - c.pos).mag2() - (c.radius * c.radius)) <= epsilon) + return { p }; + else + return {}; + } + + // Get intersection points where triangle intersects with point + template + inline std::vector> intersects(const triangle& r, const olc::v2d_generic& p) + { + // TODO: + return {}; + } + + + + + + + + + + + + + // ================================================================================================================ + // LINE =========================================================================================================== + + // Check if point contains line segment + template + inline constexpr bool contains(const olc::v2d_generic& p, const line& l) + { + return false; // It can't! + } + + // Check if line segment contains line segment + template + inline constexpr bool contains(const line& l1, const line& l2) + { + // TODO: Check if segments are colinear, and l1 exists within bounds of l2 + return false; + } + + // Check if rectangle contains line segment + template + inline constexpr bool contains(const rect& r, const line& l) + { + return contains(r, l.start) && contains(r, l.end); + } + + // Check if circle contains line segment + template + inline constexpr bool contains(const circle& c1, const line& l) + { + return contains(c1, l.start) && contains(c1, l.end); + } + + // Check if triangle contains line segment + template + inline constexpr bool contains(const triangle& t, const line& l) + { + return contains(t, l.start) && contains(t, l.end); + } + + + + + // Check if point overlaps line segment + template + inline constexpr bool overlaps(const olc::v2d_generic& p, const line& l) + { + return contains(l, p); + } + + // Check if line segment overlaps line segment + template + inline constexpr bool overlaps(const line& l1, const line& l2) + { + // TODO: + return false; + } + + // Check if rectangle overlaps line segment + template + inline constexpr bool overlaps(const rect& r, const line& l) + { + return contains(r, l.start) + || contains(r, l.end); + + // TODO: This method is no good, it cant detect lines whose start and end + // points are outside the rectangle + } + + // Check if circle overlaps line segment + template + inline constexpr bool overlaps(const circle& c, const line& l) + { + // TODO: + return false; + } + + // Check if triangle overlaps line segment + template + inline constexpr bool overlaps(const triangle& t, const line& l) + { + return overlaps(t, l.start) || overlaps(t, l.end); + + // TODO: This method is no good, it cant detect lines whose start and end + // points are outside the triangle + } + + + + + // Get intersection points where point intersects with line segment + template + inline std::vector> intersects(const olc::v2d_generic& p, const line& l) + { + // TODO: + return {}; + } + + // Get intersection points where line segment intersects with line segment + template + inline std::vector> intersects(const line& l1, const line& l2) + { + // TODO: + return {}; + } + + // Get intersection points where rectangle intersects with line segment + template + inline std::vector> intersects(const rect& r, const line& l) + { + // TODO: + return {}; + } + + // Get intersection points where circle intersects with line segment + template + inline std::vector> intersects(const circle& c, const line& l) + { + // TODO: + return {}; + } + + // Get intersection points where triangle intersects with line segment + template + inline std::vector> intersects(const triangle& t, const line& l) + { + // TODO: + return {}; + } + + + + + + + + + + + + + // ================================================================================================================ + // RECTANGLE ====================================================================================================== + + // Check if point contains rectangle + template + inline constexpr bool contains(const olc::v2d_generic& p, const rect& r) + { + return false; // It can't! + } + + // Check if line segment contains rectangle + template + inline constexpr bool contains(const line& l, const rect& r) + { + return false; // It can't + } + + // Check if rectangle contains rectangle + template + inline constexpr bool contains(const rect& r1, const rect& r2) + { + return (r2.pos.x >= r1.pos.x) && (r2.pos.x + r2.size.x < r1.pos.x + r1.size.x) && + (r2.pos.y >= r1.pos.y) && (r2.pos.y + r2.size.y < r1.pos.y + r1.size.y); + } + + // Check if circle contains rectangle + template + inline constexpr bool contains(const circle& c, const rect& r) + { + return contains(c, r.pos) + && contains(c, olc::v2d_generic{ r.pos.x + r.size.x, r.pos.y }) + && contains(c, olc::v2d_generic{ r.pos.x, r.pos.y + r.size.y }) + && contains(c, r.pos + r.size); + } + + // Check if triangle contains rectangle + template + inline constexpr bool contains(const triangle& t, const rect& r) + { + return contains(t, r.pos) + && contains(t, r.pos + r.size) + && contains(t, olc::v2d_generic{ r.pos.x + r.size.x,r.pos.y }) + && contains(t, olc::v2d_generic{ r.pos.x, r.pos.y + r.size.y }); + } + + + + + // Check if point overlaps rectangle + template + inline constexpr bool overlaps(const olc::v2d_generic& p, const rect& r) + { + return overlaps(r, p); + } + + // Check if line segment overlaps rectangle + template + inline constexpr bool overlaps(const line& l, const rect& r) + { + return overlaps(r, l); + } + + // Check if rectangle overlaps rectangle + template + inline constexpr bool overlaps(const rect& r1, const rect& r2) + { + return (r1.pos.x < r2.pos.x + r2.size.x && r1.pos.x + r1.size.x >= r2.pos.x && + r1.pos.y < r2.pos.y + r2.size.y && r1.pos.y + r1.size.y >= r2.pos.y); + } + + // Check if circle overlaps rectangle + template + inline constexpr bool overlaps(const circle& c, const rect& r) + { + // Inspired by this (very clever btw) + // https://stackoverflow.com/questions/45370692/circle-rectangle-collision-response + // But modified to work :P + T2 overlap = (olc::v2d_generic{ std::clamp(c.pos.x, r.pos.x, r.pos.x + r.size.x), std::clamp(c.pos.y, r.pos.y, r.pos.y + r.size.y) } - c.pos).mag2(); + if (std::isnan(overlap)) overlap = T2(0); + return (overlap - (c.radius * c.radius)) < T2(0); + } + + // Check if triangle overlaps rectangle + template + inline constexpr bool overlaps(const triangle& t, const rect& r) + { + return contains(t, r.pos) + || contains(t, r.pos + r.size) + || contains(t, olc::v2d_generic{ r.pos.x + r.size.x, r.pos.y }) + || contains(t, olc::v2d_generic{ r.pos.x, r.pos.y + r.size.y }); + + // TODO: This method is no good, consider rectangle with all vertices + // outside of triangle, but edges still crossing + } + + + + + // Get intersection points where point intersects with rectangle + template + inline std::vector> intersects(const olc::v2d_generic& p, const rect& r) + { + return intersects(r, p); + } + + // Get intersection points where line segment intersects with rectangle + template + inline std::vector> intersects(const line& l, const rect& r) + { + // TODO: + return {}; + } + + // Get intersection points where rectangle intersects with rectangle + template + inline std::vector> intersects(const rect& r1, const rect& r2) + { + // TODO: + return {}; + } + + // Get intersection points where circle intersects with rectangle + template + inline std::vector> intersects(const circle& c, const rect& r) + { + // TODO: + return {}; + } + + // Get intersection points where triangle intersects with rectangle + template + inline std::vector> intersects(const triangle& t, const rect& r) + { + // TODO: + return {}; + } + + + + + + + + + + + + + + // ================================================================================================================ + // CIRCLE ========================================================================================================= + + // Check if point contains circle + template + inline constexpr bool contains(const olc::v2d_generic& p, const circle& c) + { + return false; // It can't! + } + + // Check if line segment contains circle + template + inline constexpr bool contains(const line& l, const circle& c) + { + return false; // It can't! + } + + // Check if rectangle contains circle + template + inline constexpr bool contains(const rect& r, const circle& c) + { + // TODO: + return false; + } + + // Check if circle contains circle + template + inline constexpr bool contains(const circle& c1, const circle& c2) + { + return (c1.pos - c2.pos).mag2() <= (c1.radius - c2.radius) * (c1.radius - c2.radius); + } + + // Check if triangle contains circle + template + inline constexpr bool contains(const triangle& t, const circle& c) + { + // TODO: + return false; + } + + + + + // Check if point overlaps circle + template + inline constexpr bool overlaps(const olc::v2d_generic& p, const circle& c) + { + return overlaps(c, p); + } + + // Check if line segment overlaps circle + template + inline constexpr bool overlaps(const line& l, const circle& c) + { + return overlaps(c, l); + } + + // Check if rectangle overlaps circle + template + inline constexpr bool overlaps(const rect& r, const circle& c) + { + return overlaps(c, r); + } + + // Check if circle overlaps circle + template + inline constexpr bool overlaps(const circle& c1, const circle& c2) + { + return (c1.pos - c2.pos).mag2() <= (c1.radius + c2.radius) * (c1.radius + c2.radius); + } + + // Check if triangle overlaps circle + template + inline constexpr bool overlaps(const triangle& t, const circle& c) + { + // TODO: + return false; + } + + + + + // Get intersection points where point intersects with circle + template + inline std::vector> intersects(const olc::v2d_generic& p, const circle& c) + { + // TODO: + return {}; + } + + // Get intersection points where line segment intersects with circle + template + inline std::vector> intersects(const line& l, const circle& c) + { + // TODO: + return {}; + } + + // Get intersection points where rectangle intersects with circle + template + inline std::vector> intersects(const rect& r, const circle& c) + { + // TODO: + return {}; + } + + // Get intersection points where circle intersects with circle + template + inline std::vector> intersects(const circle& c1, const circle& c2) + { + // TODO: + return {}; + } + + // Get intersection points where triangle intersects with circle + template + inline std::vector> intersects(const triangle& t, const circle& c) + { + // TODO: + return {}; + } + + + + + + + + + + + + + + // ================================================================================================================ + // TRIANGLE ======================================================================================================= + + // Check if point contains triangle + template + inline constexpr bool contains(const olc::v2d_generic& p, const triangle& t) + { + return false; // It can't! + } + + // Check if line segment contains triangle + template + inline constexpr bool contains(const line& l, const triangle& t) + { + return false; // It can't + } + + // Check if rectangle contains triangle + template + inline constexpr bool contains(const rect& r, const triangle& t) + { + // TODO: + return false; + } + + // Check if circle contains triangle + template + inline constexpr bool contains(const circle& c, const triangle& t) + { + // TODO: + return false; + } + + // Check if triangle contains triangle + template + inline constexpr bool contains(const triangle& t1, const triangle& t2) + { + // TODO: + return false; + } + + + + + // Check if point overlaps triangle + template + inline constexpr bool overlaps(const olc::v2d_generic& p, const triangle& t) + { + return overlaps(t, p); + } + + // Check if line segment overlaps triangle + template + inline constexpr bool overlaps(const line& l, const triangle& t) + { + return overlaps(t, l); + } + + // Check if rectangle overlaps triangle + template + inline constexpr bool overlaps(const rect& r, const triangle& t) + { + return overlaps(t, r); + } + + // Check if circle overlaps triangle + template + inline constexpr bool overlaps(const circle& c, const triangle& t) + { + return overlaps(t, c); + } + + // Check if triangle overlaps triangle + template + inline constexpr bool overlaps(const triangle& t1, const triangle& t2) + { + // TODO: + return false; + } + + + + + // Get intersection points where point intersects with triangle + template + inline std::vector> intersects(const olc::v2d_generic& p, const triangle& t) + { + // TODO: + return {}; + } + + // Get intersection points where line segment intersects with triangle + template + inline std::vector> intersects(const line& l, const triangle& t) + { + // TODO: + return {}; + } + + // Get intersection points where rectangle intersects with triangle + template + inline std::vector> intersects(const rect& r, const triangle& t) + { + // TODO: + return {}; + } + + // Get intersection points where circle intersects with triangle + template + inline std::vector> intersects(const circle& c, const triangle& t) + { + // TODO: + return {}; + } + + // Get intersection points where triangle intersects with triangle + template + inline std::vector> intersects(const triangle& t1, const triangle& t2) + { + // TODO: + return {}; + } + +} \ No newline at end of file