Add line to rectangle collision detection in geom2d header.

pull/28/head
sigonasr2 1 year ago
parent 5a8527f51b
commit d9e8c6ddbd
  1. 2
      Crawler/Version.h
  2. 24
      Crawler/olcUTIL_Geometry2D.h

@ -2,7 +2,7 @@
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 2 #define VERSION_MINOR 2
#define VERSION_PATCH 0 #define VERSION_PATCH 0
#define VERSION_BUILD 221 #define VERSION_BUILD 231
#define stringify(a) stringify_(a) #define stringify(a) stringify_(a)
#define stringify_(a) #a #define stringify_(a) #a

@ -594,11 +594,10 @@ namespace olc::utils::geom2d
template<typename T1, typename T2> template<typename T1, typename T2>
inline constexpr bool overlaps(const rect<T1>& r, const line<T2>& l) inline constexpr bool overlaps(const rect<T1>& r, const line<T2>& l)
{ {
return contains(r, l.start) return overlaps(r.left(),l)||
|| contains(r, l.end); overlaps(r.top(),l)||
overlaps(r.bottom(),l)||
// TODO: This method is no good, it cant detect lines whose start and end overlaps(r.right(),l);
// points are outside the rectangle
} }
// Check if circle overlaps line segment // Check if circle overlaps line segment
@ -649,8 +648,16 @@ namespace olc::utils::geom2d
template<typename T1, typename T2> template<typename T1, typename T2>
inline std::vector<olc::v2d_generic<T2>> intersects(const rect<T1>& r, const line<T2>& l) inline std::vector<olc::v2d_generic<T2>> intersects(const rect<T1>& r, const line<T2>& l)
{ {
// TODO: std::vector<olc::v2d_generic<T2>>intersections;
return {}; std::vector<olc::v2d_generic<T2>>result=intersects(r.left(),l);
if(result.size()>0)intersections.push_back(result[0]);
result=intersects(r.right(),l);
if(result.size()>0)intersections.push_back(result[0]);
result=intersects(r.top(),l);
if(result.size()>0)intersections.push_back(result[0]);
result=intersects(r.bottom(),l);
if(result.size()>0)intersections.push_back(result[0]);
return intersections;
} }
// Get intersection points where circle intersects with line segment // Get intersection points where circle intersects with line segment
@ -789,8 +796,7 @@ namespace olc::utils::geom2d
template<typename T1, typename T2> template<typename T1, typename T2>
inline std::vector<olc::v2d_generic<T2>> intersects(const line<T1>& l, const rect<T2>& r) inline std::vector<olc::v2d_generic<T2>> intersects(const line<T1>& l, const rect<T2>& r)
{ {
// TODO: return intersects(r,l);
return {};
} }
// Get intersection points where rectangle intersects with rectangle // Get intersection points where rectangle intersects with rectangle

Loading…
Cancel
Save