bool cCollider2D::Collide(cCollisionMesh2D* apMeshA,cCollisionMesh2D* apMeshB, cVector2f &avMTD)
{
    cVector2f vAxis[32];
    int lAxisNum=0;

    //Check separating planes for A
    for(int i=0; i<(int)apMeshA->mvNormal.size(); i++)
        {
            vAxis[lAxisNum] = apMeshA->mvNormal[i];
            if(AxisSeparateMeshes(vAxis[lAxisNum], apMeshA, apMeshB))
                {
                    return false;
                }
            lAxisNum++;
        }

    //Check separating planes for B
    for(int i=0; i<(int)apMeshB->mvNormal.size(); i++)
        {
            vAxis[lAxisNum] = apMeshB->mvNormal[i];
            if(AxisSeparateMeshes(vAxis[lAxisNum], apMeshA, apMeshB))
                {
                    return false;
                }
            lAxisNum++;
        }

    avMTD = FindMTD(vAxis, lAxisNum);


    return true;
}
Exemple #2
0
bool World::IntersectBoxToBox(Sprite* spt1, Sprite* spt2)
{
	// Get bodies' edges
	Edges body1_edges = spt1->GetRigidBody()->GetEdges();
	Edges body2_edges = spt2->GetRigidBody()->GetEdges();

	// init helper variables
	vec3 vec_axis[8];
	int iNumAxis = 0;

	// Check overlapped for the 1st sprite
	for (int i = 0; i < 4; ++i)
	{
		vec_axis[iNumAxis] = vec3(-body1_edges[i].y, body1_edges[i].x);

		if (AxisSeparatePolygons(vec_axis, iNumAxis, spt1->GetRigidBody(), spt2->GetRigidBody()))
			return false;

	}

	// Check overlapped for the 2nd sprite
	for (int i = 0; i < 4; ++i)
	{
		vec_axis[iNumAxis] = vec3(-body2_edges[i].y, body2_edges[i].x);

		if (AxisSeparatePolygons(vec_axis, iNumAxis, spt1->GetRigidBody(), spt2->GetRigidBody()))
			return false;
	}

	//Find munumum transition distance
	mtd = FindMTD(vec_axis, iNumAxis);

	vec3 d = spt1->GetPosition() - spt2->GetPosition();

	// Reverse the mtd's sign
	if (d.DotProduct(mtd) < 0.f)
		mtd = -mtd;

	return true;
}