Ejemplo n.º 1
0
/**********************************************************
		C# code from
		http://www.codeproject.com/Articles/15573/2D-Polyhedron-Collision-Detection
*/
bool Polyhedron::DoesCollide(Vector2* Velocity, Polyhedron* CheckWith)
{
	bool Intersect = true;
	bool WillIntersect = true;

	int edgeCountA = Edges->count;
	int edgeCountB = CheckWith->Edges->count;
	// float minIntervalDistance = 99999999;
	// Vector2* translationAxis = new Vector2( 0, 0 );
	Vector2* edge;

	// Loop through all the edges of both Polyhedrons
	for( int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++ )
	{
		if( edgeIndex < edgeCountA )
		{
			edge = Edges->ItemAt<Vector2*>(edgeIndex);
		} else {
			edge = CheckWith->Edges->ItemAt<Vector2*>(edgeIndex - edgeCountA);
		}

		// ===== 1. Find if the Polyhedrons are currently intersecting =====

		// Find the axis perpendicular to the current edge
		Vector2* axis = new Vector2(-edge->Y, edge->X);
		axis->Normalise();

		// Find the projection of the Polyhedron on the current axis
		float minA = 0; float minB = 0; float maxA = 0; float maxB = 0;
		Project(axis, &minA, &maxA);
		CheckWith->Project(axis, &minB, &maxB);

		// Check if the Polyhedron projections are currentlty intersecting
		if( IntervalDistance(minA, maxA, minB, maxB) > 0 )
		{
			Intersect = false;
		}

		// ===== 2. Now find if the Polyhedrons *will* intersect =====

		// Project the velocity on the current axis
		float velocityProjection = axis->DotProduct( Velocity );

		// Get the projection of Polyhedron A during the movement
		if( velocityProjection < 0 )
		{
			minA += velocityProjection;
		} else {
			maxA += velocityProjection;
		}

		// Do the same test as above for the new projection
		float intervalDistance = IntervalDistance( minA, maxA, minB, maxB );
		if( intervalDistance > 0 )
		{
			WillIntersect = false;
		}

		delete axis;

		// If the Polyhedrons are not intersecting and won't intersect, exit the loop
		if( !Intersect && !WillIntersect)
		{
			break;
		}

		/*
		// Check if the current interval distance is the minimum one. If so store
		// the interval distance and the current distance.
		// This will be used to calculate the minimum translation vector
		intervalDistance = Math.Abs(intervalDistance);
		if (intervalDistance < minIntervalDistance) {
			minIntervalDistance = intervalDistance;
			translationAxis = axis;

			Vector d = PolyhedronA.Center - PolyhedronB.Center;
			if (d.DotProduct(translationAxis) < 0) translationAxis = -translationAxis;
		}
		*/
		// int xcv = 1;

	}

	// The minimum translation vector can be used to push the Polyhedrons appart.
	// First moves the Polyhedrons by their velocity
	// then move PolyhedronA by MinimumTranslationVector.
	// if (result.WillIntersect) result.MinimumTranslationVector = translationAxis * minIntervalDistance;

	return (Intersect | WillIntersect);
}
Ejemplo n.º 2
0
 double DotProduct( Vector2 const& i_lhs, Vector2 const& i_rhs )
 {
     return i_lhs.DotProduct( i_rhs );
 }