game/sdlpp/sdlpp_collision.cpp

61 lines
1.9 KiB
C++
Raw Normal View History

2020-11-21 19:57:40 +00:00
#include "sdlpp_collision.hpp"
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
}
bool CollisionPolygon::isInfinite() const {
return infinite;
}
void CollisionPolygon::setInfinite() {
infinite = true;
}
void CollisionPolygon::updateCollision( int x, int y, int w, int h ) {
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 );
}
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 ) {
return !(
p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() ||
p1.topmost() > p2.bottommost() || p2.topmost() > p1.bottommost() );
}
} // namespace SDLPP