project2D/CollisionManager.cpp
//--------------------------------------------------------------------------------------
// IsColliding: Check if 2 SAT boundingboxes are colliding.
//
// Checks each corner of both shapes to find out if they are hitting and where.
//
// Param:
//		a: An Entity for an object that can collide
//		b: An Entity for an object that can collide
//
// Returns:
//		bool: Returns a bool of true or falue for if a collision is happening.
//--------------------------------------------------------------------------------------
Check if 2 SAT boundingboxes are colliding.
Checks each corner of both shapes to find out if they are hitting and where.

An Entity for an object that can collide
b: An Entity for an object that can collide

Returns a bool of true or falue for if a collision is happening.
Check if 2 SAT boundingboxes are colliding . Checks each corner of both shapes to find out if they are hitting and where . An Entity for an object that can collide b : An Entity for an object that can collide Returns a bool of true or falue for if a collision is happening .
bool CollisionManager::IsColliding(Entity* a, Entity* b)
{
	rect rcA = a->boundingBox;
	rect rcB = b->boundingBox;
	Vector3 posa = CastTo<Vector3>(a->GetPosition());
	Vector3 posb = CastTo<Vector3>(b->GetPosition());
	Vector3 aUR = rcA.UpperRight + posa;
	Vector3 aUL = rcA.UpperLeft + posa;
	Vector3 aLR = rcA.LowerRight + posa;
	Vector3 aLL = rcA.LowerLeft + posa;
	Vector3 bUR = rcB.UpperRight + posb;
	Vector3 bUL = rcB.UpperLeft + posb;
	Vector3 bLR = rcB.LowerRight + posb;
	Vector3 bLL = rcB.LowerLeft + posb;
	float aMax = 0;
	float aMin = 0;
	float bMax = 0;
	float bMin = 0;
	Vector3 a1 = aUR - aUL;
	Vector3 a2 = aUR - aLR;
	Vector3 a3 = bUL - bLL;
	Vector3 a4 = bUL - bUR;
	vector<Vector3> axes;
	axes.push_back(a1);
	axes.push_back(a2);
	axes.push_back(a3);
	axes.push_back(a4);
	for (int i = 0; i < axes.size(); i++)
	{
		Vector3 aURProj = Project(aUR, axes[i]);
		Vector3 aULProj = Project(aUL, axes[i]);
		Vector3 aLRProj = Project(aLR, axes[i]);
		Vector3 aLLProj = Project(aLL, axes[i]);
		Vector3 bURProj = Project(bUR, axes[i]);
		Vector3 bULProj = Project(bUL, axes[i]);
		Vector3 bLRProj = Project(bLR, axes[i]);
		Vector3 bLLProj = Project(bLL, axes[i]);
		float aURScalar = aURProj.dot(axes[i]);
		float aULScalar = aULProj.dot(axes[i]);
		float aLRScalar = aLRProj.dot(axes[i]);
		float aLLScalar = aLLProj.dot(axes[i]);
		float bURScalar = bURProj.dot(axes[i]);
		float bULScalar = bULProj.dot(axes[i]);
		float bLRScalar = bLRProj.dot(axes[i]);
		float bLLScalar = bLLProj.dot(axes[i]);
		vector<float> aScalars;
		aScalars.push_back(aURScalar);
		aScalars.push_back(aULScalar);
		aScalars.push_back(aLRScalar);
		aScalars.push_back(aLLScalar);
		aMin = MinV(aScalars);
		aMax = MaxV(aScalars);
		vector<float> bScalars;
		bScalars.push_back(bURScalar);
		bScalars.push_back(bULScalar);
		bScalars.push_back(bLRScalar);
		bScalars.push_back(bLLScalar);
		bMin = MinV(bScalars);
		bMax = MaxV(bScalars);
		if (!(bMin <= aMax && bMax >= aMin))
		{
			return false;
		}
	}
	return true;
}
bool CollisionManager :: IsColliding ( Entity * a , Entity * b ) { rect rcA = a -> boundingBox ; rect rcB = b -> boundingBox ; Vector3 posa = CastTo < Vector3 > ( a -> GetPosition ( ) ) ; Vector3 posb = CastTo < Vector3 > ( b -> GetPosition ( ) ) ; Vector3 aUR = rcA . UpperRight + posa ; Vector3 aUL = rcA . UpperLeft + posa ; Vector3 aLR = rcA . LowerRight + posa ; Vector3 aLL = rcA . LowerLeft + posa ; Vector3 bUR = rcB . UpperRight + posb ; Vector3 bUL = rcB . UpperLeft + posb ; Vector3 bLR = rcB . LowerRight + posb ; Vector3 bLL = rcB . LowerLeft + posb ; float aMax = 0 ; float aMin = 0 ; float bMax = 0 ; float bMin = 0 ; Vector3 a1 = aUR - aUL ; Vector3 a2 = aUR - aLR ; Vector3 a3 = bUL - bLL ; Vector3 a4 = bUL - bUR ; vector < Vector3 > axes ; axes . push_back ( a1 ) ; axes . push_back ( a2 ) ; axes . push_back ( a3 ) ; axes . push_back ( a4 ) ; for ( int i = 0 ; i < axes . size ( ) ; i ++ ) { Vector3 aURProj = Project ( aUR , axes [ i ] ) ; Vector3 aULProj = Project ( aUL , axes [ i ] ) ; Vector3 aLRProj = Project ( aLR , axes [ i ] ) ; Vector3 aLLProj = Project ( aLL , axes [ i ] ) ; Vector3 bURProj = Project ( bUR , axes [ i ] ) ; Vector3 bULProj = Project ( bUL , axes [ i ] ) ; Vector3 bLRProj = Project ( bLR , axes [ i ] ) ; Vector3 bLLProj = Project ( bLL , axes [ i ] ) ; float aURScalar = aURProj . dot ( axes [ i ] ) ; float aULScalar = aULProj . dot ( axes [ i ] ) ; float aLRScalar = aLRProj . dot ( axes [ i ] ) ; float aLLScalar = aLLProj . dot ( axes [ i ] ) ; float bURScalar = bURProj . dot ( axes [ i ] ) ; float bULScalar = bULProj . dot ( axes [ i ] ) ; float bLRScalar = bLRProj . dot ( axes [ i ] ) ; float bLLScalar = bLLProj . dot ( axes [ i ] ) ; vector < float > aScalars ; aScalars . push_back ( aURScalar ) ; aScalars . push_back ( aULScalar ) ; aScalars . push_back ( aLRScalar ) ; aScalars . push_back ( aLLScalar ) ; aMin = MinV ( aScalars ) ; aMax = MaxV ( aScalars ) ; vector < float > bScalars ; bScalars . push_back ( bURScalar ) ; bScalars . push_back ( bULScalar ) ; bScalars . push_back ( bLRScalar ) ; bScalars . push_back ( bLLScalar ) ; bMin = MinV ( bScalars ) ; bMax = MaxV ( bScalars ) ; if ( ! ( bMin <= aMax && bMax >= aMin ) ) { return false ; } } return true ; }
IsColliding: Check if 2 SAT boundingboxes are colliding.
IsColliding : Check if 2 SAT boundingboxes are colliding .