game/sdlpp/sdlpp_collision.cpp

84 lines
2.6 KiB
C++
Raw Normal View History

2020-11-21 19:57:40 +00:00
#include "sdlpp_collision.hpp"
2021-03-13 14:05:16 +00:00
#include "sdlpp_geometry.hpp"
2020-11-21 19:57:40 +00:00
namespace SDLPP {
CollisionPolygon::CollisionPolygon( double x, double y )
: CollisionPolygon( Vec2D< double >( x, y ) ) {}
CollisionPolygon::CollisionPolygon( const Vec2D< double > &input ) {
original = input;
position = { 0, 0 };
2020-11-21 19:57:40 +00:00
}
2021-04-26 19:57:31 +00:00
CollisionPolygon::CollisionPolygon( double x, double y, uint64_t id )
: CollisionPolygon( Vec2D< double >( x, y ), id ) {}
CollisionPolygon::CollisionPolygon( const Vec2D< double > &input, uint64_t id )
: CollisionPolygon( input ) {
_id = id;
}
2020-11-21 19:57:40 +00:00
bool CollisionPolygon::isInfinite() const {
return infinite;
}
void CollisionPolygon::setInfinite() {
infinite = true;
}
2021-04-26 19:57:31 +00:00
void CollisionPolygon::updateCollision( int x, int y, int w, int h,
uint64_t id ) {
if ( _id == static_cast< uint64_t >( -1 ) )
_id = id;
position = Vec2D< int >( original.getX() * w + x, original.getY() * h + y );
2020-11-21 19:57:40 +00:00
}
int CollisionPolygon::getX() const {
return position.getX();
2020-11-21 19:57:40 +00:00
}
int CollisionPolygon::getY() const {
return position.getY();
2020-11-21 19:57:40 +00:00
}
void CollisionPolygon::setColor( const std::string &color ) {
sdl_color = getSDLColorHEX( color );
}
void CollisionPolygon::setOutlineColor( const std::string &color ) {
sdl_outline = getSDLColorHEX( color );
}
2021-04-26 19:57:31 +00:00
void CollisionPolygon::setId( uint64_t id ) {
_id = id;
}
uint64_t CollisionPolygon::getId() {
return _id;
}
2020-11-21 19:57:40 +00:00
bool infinityIntersection( const SDLPP::CollisionPolygon &infinite,
const SDLPP::CollisionPolygon &other ) {
int ileft = infinite.leftmost();
int iright = infinite.rightmost();
int itop = infinite.topmost();
int ibottom = infinite.bottommost();
bool ret =
ileft != -1 && ileft <= other.rightmost() && ileft >= other.leftmost();
ret |= iright != -1 && iright >= other.leftmost() &&
iright <= other.rightmost();
ret |= itop != -1 && itop <= other.bottommost() && itop >= other.topmost();
ret |= ibottom != -1 && ibottom >= other.topmost() &&
ibottom <= other.bottommost();
ret |= ileft == -1 && iright == -1 && itop == -1 && ibottom == -1;
return ret;
}
bool intersects( const SDLPP::CollisionPolygon &p1,
const SDLPP::CollisionPolygon &p2 ) {
if ( p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() ||
p1.bottommost() < p2.topmost() || p2.bottommost() < p1.topmost() )
2021-03-13 14:05:16 +00:00
return false;
for ( auto &line : p1.getLines() ) {
for ( auto &line2 : p2.getLines() ) {
if ( linesCross( line, line2 ) )
2021-03-13 14:05:16 +00:00
return true;
}
}
return false;
2020-11-21 19:57:40 +00:00
}
} // namespace SDLPP