bool Map::hasBarrierOnPath(int xFirst, int yFirst, int xSecond, int ySecond) const { int minX = std::min( xFirst, xSecond ), maxX = std::max( xFirst, xSecond ), minY = std::min( yFirst, ySecond ), maxY = std::max( yFirst, ySecond ); std::pair<int, int> prevCoordinates = std::make_pair( xFirst * 10 + 5, yFirst * 10 + 5 ); std::pair<int, int> curCoordinates = std::make_pair( xSecond * 10 + 5, ySecond * 10 + 5 ); for( int i = minX; i <= maxX; ++i ) { for( int j = minY; j <= maxY; ++j ) { if( cells[j][i] == 0 ) { continue; } std::pair<int, int> firstPoint( i * 10, j * 10 ), secondPoint( (i + 1) * 10, j * 10 ), thirdPoint( (i + 1) * 10, (j + 1) * 10 ), fourthPoint( i * 10, (j + 1) * 10 ); if( isIntersects( prevCoordinates, curCoordinates, firstPoint, secondPoint ) || isIntersects( prevCoordinates, curCoordinates, secondPoint, thirdPoint ) || isIntersects( prevCoordinates, curCoordinates, thirdPoint, fourthPoint ) || isIntersects( prevCoordinates, curCoordinates, fourthPoint, firstPoint ) ) { return true; } } } if( wrongFinishLineIntersection( xFirst, yFirst, xSecond, ySecond ) ) { return true; } return false; }
void CPowerupManager::handleWalls( CPlayer& player, std::set<CPlayer*>& crashedPlayers ) { CCoordinates playersPreviousCoordinates = player.GetPreviousPosition( ); CCoordinates playersCoordinates = player.GetPosition(); for( auto powerup : powerups ) { CCoordinates realCoordinates( playersCoordinates.y * 10 + 5, playersCoordinates.x * 10 + 5 ), realPreviousCoordinates( playersPreviousCoordinates.y * 10 + 5, playersPreviousCoordinates.x * 10 + 5 ); if( powerup.second != WALL ) { continue; } CCoordinates firstPoint( powerup.first.y * 10, powerup.first.x * 10 ), secondPoint( (powerup.first.y + 1) * 10, powerup.first.x * 10 ), thirdPoint( (powerup.first.y + 1) * 10, (powerup.first.x + 1) * 10 ), fourthPoint( powerup.first.y * 10, (powerup.first.x + 1) * 10 ); if( isIntersects( realPreviousCoordinates, realCoordinates, firstPoint, secondPoint ) || isIntersects( realPreviousCoordinates, realCoordinates, secondPoint, thirdPoint ) || isIntersects( realPreviousCoordinates, realCoordinates, thirdPoint, fourthPoint ) || isIntersects( realPreviousCoordinates, realCoordinates, fourthPoint, firstPoint ) ) { powerups.erase( powerup.first ); crashedPlayers.insert( &player ); return; } } }
bool Game::playerOutOfTrack( size_t num ) { Coordinates playersPreviousCoordinates = players[num].getPreviousPosition(); Coordinates playersCoordinates = players[num].getPosition(); int minX = std::min( playersPreviousCoordinates.x, playersCoordinates.x ), maxX = std::max( playersPreviousCoordinates.x, playersCoordinates.x ), minY = std::min( playersPreviousCoordinates.y, playersCoordinates.y ), maxY = std::max( playersPreviousCoordinates.y, playersCoordinates.y ); Coordinates realCoordinates( playersCoordinates.x * 10 + 5, playersCoordinates.y * 10 + 5 ), realPreviousCoordinates( playersPreviousCoordinates.x * 10 + 5, playersPreviousCoordinates.y * 10 + 5 ); for( int i = minX; i <= maxX; ++i ) { for( int j = minY; j <= maxY; ++j ) { if( map.isEmpty( i, j ) ) { continue; } Coordinates firstPoint( i * 10, j * 10 ), secondPoint( ( i + 1 ) * 10, j * 10 ), thirdPoint( ( i + 1 ) * 10, ( j + 1 ) * 10 ), fourthPoint( i * 10, ( j + 1 ) * 10 ); if( isIntersects( realPreviousCoordinates, realCoordinates, firstPoint, secondPoint ) || isIntersects( realPreviousCoordinates, realCoordinates, secondPoint, thirdPoint ) || isIntersects( realPreviousCoordinates, realCoordinates, thirdPoint, fourthPoint ) || isIntersects( realPreviousCoordinates, realCoordinates, fourthPoint, firstPoint ) ) { return true; } } } return false; }
bool Game::finishLineIntersectsWithPlayer( size_t num ) { // Происходит проверка: // 1. Проекции отрезков на оси пересекаются // 2. Считается ориентированная площадь треугольников. Нужно, чтобы эти площади были разных знаков. Coordinates playersPreviousCoordinates = players[num].getPreviousPosition(); Coordinates playersCoordinates = players[num].getPosition(); return isIntersects( playersPreviousCoordinates, playersCoordinates, finishLine.firstPoint, finishLine.secondPoint ); }
void CollisionManager::update() { std::set<int32> closedNodes; m_pairs.clear(); for(CollisionHullMap::iterator it = m_collisionHulls.begin(); it != m_collisionHulls.end(); ++it) { Vector3 position = it->second->getPosition(); Cell<ICollisionHull*>* myCell = m_cellGrid.getCell(position); ICollisionHull* a = it->second.get(); ICollisionHull* b = 0; for(int32 i = 0; i < 9; i++) { Cell<ICollisionHull*>* currentCell = myCell->getSibling(i); if(currentCell == 0) continue; for(Cell<ICollisionHull*>::ObjectListIt iit = currentCell->begin(); iit != currentCell->end(); ++iit) { b = *iit; if(a == b) continue; if(closedNodes.count(b->getId()) > 0) continue; //TODO: collision groups filter if(a->getCollisionGroup() == b->getCollisionGroup()) continue; int32 id_a = a->getId(); int32 id_b = b->getId(); int32 hash = std::max(id_a, id_b) << 16 | std::min(id_a, id_b); if(isIntersects(a, b)) { if(m_previousCollisionPairs.count(hash) > 0) { continue; } CollisionPair pair(std::max(id_a, id_b), std::min(id_a, id_b)); m_pairs.push_back(pair); m_previousCollisionPairs.insert(hash); } else { m_previousCollisionPairs.erase(hash); } }//for(Cell<ICollisionHull*>::ObjectListIt iit = currentCell->begin(); iit != currentCell->end(); ++iit) }//for(int32 i = 0; i < 9; i++) closedNodes.insert(a->getId()); }//for(CollisionHullMap::iterator it = m_collisionHulls.begin(); it != m_collisionHulls.end(); ++it) }
int SpringStroke::getIntersection(const ofVec2f &p1, const ofVec2f &p2) { // check if the line intersects with any line we have vector<Particle*> particles = line->getPoints(); for (int i=2; i<particles.size(); i++) { if (isIntersects(p1, p2, *particles[i-1], *particles[i])) { return i; } } return -1; }