74 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "KRect.h"
 | |
| 
 | |
| #include "KCircle.h"
 | |
| #include "Utils.h"
 | |
| 
 | |
| namespace KapitanGame {
 | |
| 	KRect::KRect(const KVector2D& position, const KVector2D& velocity, const KTexture& texture, const float width,
 | |
| 		const float height) : KShape(position, velocity, texture), Width(width), Height(height) {
 | |
| 	}
 | |
| 
 | |
| 	void KRect::CollisionDetectionWithMapStep(const SDL_Rect& map) {
 | |
| 	}
 | |
| 
 | |
| 	float KRect::GetWidth() const {
 | |
| 		return Width;
 | |
| 	}
 | |
| 
 | |
| 	float KRect::GetHeight() const {
 | |
| 		return Height;
 | |
| 	}
 | |
| 
 | |
| 	KVector2D KRect::GetSeparationVector(const float left, const float right, const float top, const float bottom) {
 | |
| 		auto x = left < right ? -left : right;
 | |
| 		auto y = top < bottom ? -top : bottom;
 | |
| 		if (fabs(x) < fabs(y))
 | |
| 			y = 0;
 | |
| 		if (fabs(x) > fabs(y))
 | |
| 			x = 0;
 | |
| 		return { x, y };
 | |
| 	}
 | |
| 
 | |
| 	KVector2D KRect::GetSeparationVector(const KCircle& other) const {
 | |
| 		const float l = Position.X - Width / 2;
 | |
| 		const float r = Position.X + Width / 2;
 | |
| 		const float t = Position.Y - Height / 2;
 | |
| 		const float b = Position.Y + Height / 2;
 | |
| 		const auto f = KVector2D(Utils::Clamp(other.GetPosition().X, l, r),
 | |
| 			Utils::Clamp(other.GetPosition().Y, t, b));
 | |
| 
 | |
| 		if (other.GetPosition() == f) {
 | |
| 			const auto left = other.GetPosition().X - l + other.GetRadius();
 | |
| 			const auto right = r - other.GetPosition().X + other.GetRadius();
 | |
| 			const auto top = other.GetPosition().Y - t + other.GetRadius();
 | |
| 			const auto bottom = b - other.GetPosition().Y + other.GetRadius();
 | |
| 			return GetSeparationVector(left, right, top, bottom) * -1.f;
 | |
| 		}
 | |
| 		return (other.GetPosition() - f) / (other.GetPosition() - f).Length() * (other.GetRadius() - (other.GetPosition() - f).Length()) * 1.f;
 | |
| 	}
 | |
| 
 | |
| 	KVector2D KRect::GetSeparationVector(const KRect& other) const {
 | |
| 		const auto left = Position.X + Width / 2 - (other.Position.X - other.Width / 2);
 | |
| 		const auto right = other.Position.X + other.Width / 2 - (Position.X - Width / 2);
 | |
| 		const auto top = Position.Y + Height / 2 - (other.Position.Y - other.Height / 2);
 | |
| 		const auto bottom = other.Position.Y + other.Height / 2 - (Position.Y - Height / 2);
 | |
| 
 | |
| 		return GetSeparationVector(left, right, top, bottom);
 | |
| 	}
 | |
| 
 | |
| 	bool KRect::IsCollision(const KCircle& other) const {
 | |
| 		const auto f = KVector2D(Utils::Clamp(other.GetPosition().X, Position.X - Width / 2, Position.X + Width / 2),
 | |
| 			Utils::Clamp(other.GetPosition().Y, Position.Y - Height / 2, Position.Y + Height / 2));
 | |
| 
 | |
| 		return (Position - f).Length() < other.GetRadius();
 | |
| 	}
 | |
| 
 | |
| 	bool KRect::IsCollision(const KRect& other) const {
 | |
| 		return Position.X + Width / 2 > other.Position.X - other.Width / 2
 | |
| 			&& other.Position.X + other.Width / 2 > Position.X - Width / 2
 | |
| 			&& Position.Y + Height / 2 > other.Position.Y - other.Height / 2
 | |
| 			&& other.Position.Y + other.Height / 2 > Position.Y - Height / 2;
 | |
| 	}
 | |
| 
 | |
| }
 |