New collision detection

This commit is contained in:
2021-03-13 15:05:16 +01:00
parent 258ce51cfe
commit d2cf54556e
12 changed files with 186 additions and 125 deletions
+45 -4
View File
@@ -5,10 +5,12 @@
#include "sdlpp_vector.hpp"
#include "sdlpp_line.hpp"
#include <algorithm>
#include <iostream>
namespace SDLPP {
template<typename T>
Vec2D<T> pointProjectionOnLine( const Vec2D<T> &point, const Line<T> &line ) {
template < typename T >
Vec2D< T > pointProjectionOnLine( const Vec2D< T > &point,
const Line< T > &line ) {
/* from here -
* https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
*/
@@ -21,10 +23,49 @@ Vec2D<T> pointProjectionOnLine( const Vec2D<T> &point, const Line<T> &line ) {
length_squared ) );
return line.getStart() + t * ( line.getEnd() - line.getStart() );
}
template<typename T>
double pointLineDistance( const Vec2D<T> &point, const Line<T> &line ) {
template < typename T >
double pointLineDistance( const Vec2D< T > &point, const Line< T > &line ) {
return vecDistance( point, pointProjectionOnLine( point, line ) );
}
template < typename T >
int _determinant( const Line< T > &line, const Vec2D< T > &point ) {
auto res = ( line.getEnd().getX() - line.getStart().getX() ) *
( point.getY() - line.getStart().getY() ) -
( line.getEnd().getY() - line.getStart().getY() ) *
( point.getX() - line.getStart().getX() );
if ( res < 0 )
return -1;
if ( res > 0 )
return 1;
return 0;
}
template < typename T >
bool linesCross( const Line< T > &a, const Line< T > &b ) {
auto det_a_s = _determinant( a, b.getStart() );
auto det_a_e = _determinant( a, b.getEnd() );
if ( det_a_s == det_a_e && det_a_s != 0 )
return false;
// det_a_s == det_a_e == 0, means that lines have the same vector,
// we need to find out if they overlap
if ( det_a_s == det_a_e ) {
if ( a.getStart().getX() == a.getEnd().getX() ) {
// vertical lines
return a.bottomost().getY() > b.topmost().getY() &&
a.topmost().getY() < b.bottomost().getY();
} else {
// horizontal lines
return a.leftmost().getX() < b.rightmost().getX() &&
a.rightmost().getX() > b.leftmost().getX();
}
}
auto det_b_s = _determinant( b, a.getStart() );
auto det_b_e = _determinant( b, a.getEnd() );
if ( det_b_s == det_b_e )
return false;
return true;
}
} // namespace SDLPP
#endif