bool World::AxisSeparatePolygons(vec3* axis, int& index, RigidBody* body1, RigidBody* body2) { float min_a, max_a; float min_b, max_b; // Get intervals CalculateInterval(axis[index], body1, min_a, max_a); CalculateInterval(axis[index], body2, min_b, max_b); if (min_a > max_b || min_b > max_a) return true; // find the interval overlap float d0 = max_a - min_b; float d1 = max_b - min_a; float depth = d0 < d1 ? d0 : d1; // conver the separation axis into a push vector (re-normaliz // the axis and multiply by interval overlap) float axis_length_sqaured = axis[index].DotProduct(axis[index]); axis[index] *= depth / axis_length_sqaured; // Increase the index index++; return false; }
bool IsIntervalIntersection(vector<Vector2> *pPolygon1Vertices, vector<Vector2> *pPolygon2Vertices, Vector2 offset, Vector2 axis, CollisionParameter *pParam) { double minDistance1; double maxDistance1; double minDistance2; double maxDistance2; CalculateInterval(pPolygon1Vertices, axis, &minDistance1, &maxDistance1); CalculateInterval(pPolygon2Vertices, axis, &minDistance2, &maxDistance2); double h = offset * axis; minDistance1 += h; maxDistance1 += h; double distance1 = minDistance1 - maxDistance2; double distance2 = minDistance2 - maxDistance1; if (fabs(distance1) < 0.01) { distance1 = 0; } if (fabs(distance2) < 0.01) { distance2 = 0; } pParam->OverlapDistance = max(distance1, distance2); return distance1 <= 0 && distance2 <= 0; }
void WPJDirector::Draw() { CalculateInterval(); // update events WPJEvent *pNowEvent = WPJInputUtil::GetSharedInst()->NextTriggedEvent(); while (pNowEvent != NULL) { m_pNextScene->RunEvent(pNowEvent); pNowEvent = WPJInputUtil::GetSharedInst()->NextTriggedEvent(); } // update scheduler m_pScheduler->Update(m_fDeltaTime); // update actions m_pActionManager->Update(m_fDeltaTime); // draw, using visit() al_clear_to_color(al_map_rgb_f(0,0,0)); m_pNextScene->Visit(); al_flip_display(); ShowStatus(); }
bool cCollider2D::AxisSeparateMeshes(cVector2f &avAxis,cCollisionMesh2D* apMeshA, cCollisionMesh2D* apMeshB) { float fMinA,fMaxA; float fMinB,fMaxB; CalculateInterval(avAxis,apMeshA,fMinA, fMaxA); CalculateInterval(avAxis,apMeshB,fMinB, fMaxB); if(fMinA >= fMaxB || fMinB >= fMaxA) return true; float fD0 = fMaxA - fMinB; float fD1 = fMaxB - fMinA; float fDepth = (fD0 < fD1)? fD0 : fD1; avAxis *= fDepth; return false; }
BOOL CCollisionManager::SpanIntersect( CCube* cube1, CCube* cube2, vec3 &axisc, float* minPenetration /*= NULL*/, vec3* axisPenetration /*= NULL*/, float* pen /*= NULL*/ ) { vec3 axis = axisc; auto squaredLength = length(axis); squaredLength *= squaredLength; float lq = squaredLength; if (lq <= 0.02f) { if (pen) *pen = 100000.0f; return true; } axis = normalize( axis ); float mina, maxa; float minb, maxb; CalculateInterval( cube1, axis, mina, maxa ); CalculateInterval( cube2, axis, minb, maxb ); float lena = maxa - mina; float lenb = maxb - minb; float minv = std::min( mina, minb ); float maxv = std::max( maxa, maxb ); float lenv = maxv - minv; if ( lenv > (lena + lenb) ) { // Collision return false; } float penetration = (lena + lenb) - lenv; if (pen) { *pen = penetration; } if (minPenetration && axisPenetration) { if (penetration < *minPenetration) { *minPenetration = penetration; *axisPenetration = axis; // BoxA pushes BoxB away in the correct Direction if (minb < mina) { *axisPenetration *= -1; } } } // Collision return TRUE; }