Esempio n. 1
0
void SteerLib::GJK_EPA::EPA(float& return_penetration_depth, Util::Vector& return_penetration_vector, const std::vector<Util::Vector>& _simplex, const std::vector<Util::Vector>& _shapeA, const std::vector<Util::Vector>& _shapeB)
{

	std::vector<Util::Vector> simplex = _simplex; // Copy the original so we can expand it.
	Util::Vector normal;
	Edge closestEdge;

	float epsilon = 0.0001; // Should be a small number.

	while (true)
	{
		closestEdge = findClosestEdge(simplex);
		Util::Vector supportVector = Support(_shapeA, _shapeB, closestEdge.normal);

		float d = DotProduct(supportVector, closestEdge.normal);

		if (d - closestEdge.distance < epsilon)
		{
			return_penetration_vector = closestEdge.normal;
			return_penetration_depth = d;
			return;
		}

		else
			simplex.insert(simplex.begin() + closestEdge.index, supportVector);
	}

}
Esempio n. 2
0
minkowskiDifference_t buildMinkowskiDifference(std::vector<MathVector> a, std::vector<MathVector> b)
{
	MathVector direction = MathVector(1,1);
	std::vector<MathVector> simplex;
	simplex.push_back(getSupportVertex(a, b, direction));
	minkowskiDifference_t difference;

	direction = direction.negate();

	while(true)
	{
		simplex.push_back(getSupportVertex(a, b, direction));
		if(simplex.back().dotProduct(direction) <= 0)
		{
			difference.colliding = false;
			difference.collisionNormal = MathVector(0,0);
			difference.collisionDepth = 0;
			return difference;
		} else if(containsOrigin(simplex, direction) && simplex.size() == 3)
		{
			while(true)
			{
				//Perform EPA to get collision normal and penetration distance
				Edge_t e = findClosestEdge(simplex);

				MathVector p = getSupportVertex(a, b, direction);
				double d = p.dotProduct(e.normal);
//				std::cout << d - e.distance << std::endl;
//				std::cout << "Simplex size: " << simplex.size() << std::endl;
				if(d - e.distance < TOLERANCE)
				{
					difference.collisionNormal = e.normal;
					difference.collisionDepth = d;
					difference.colliding = true;
					return difference;
				} else
				{
//					std::cout << "Closest edge not found in this iteration, adding point to simplex and continuing." << std::endl;
					simplex.insert((simplex.begin()+e.index),p);
				}
			}
		}
	}
}