Beispiel #1
0
void Player::intersectedWall(const sf::FloatRect &intersection)
{
    if (intersection.intersects(vert_rect))
    {
        if (intersection.top > boundingBox.top + boundingBox.height / 2)
        {
            boundingBox.top -= intersection.height;
        }
        if (intersection.top < boundingBox.top + boundingBox.height / 2)
        {
            boundingBox.top += intersection.height;
        }
    }

    if (intersection.intersects(horz_rect))
    {
        if (intersection.left < boundingBox.left + boundingBox.width / 2)
        {
            boundingBox.left += intersection.width;
        }
        if (intersection.left > boundingBox.left + boundingBox.width / 2)
        {
            boundingBox.left -= intersection.width;
        }
    }

    updateCross();
}
Beispiel #2
0
bool Game::isCollision(sf::FloatRect box1, sf::FloatRect box2)
{
	bool collision = false;
	if (box1.intersects(box2))
		collision = true;
	return collision;
}
Beispiel #3
0
std::vector<std::shared_ptr<tgd::Entity>> QuadTree::retrieve(sf::FloatRect bounds, sf::Uint16 searchDepth)
{
    searchDepth = mLevel;
    std::vector<std::shared_ptr<tgd::Entity>> foundObjects;
    sf::Int16 index = getIndex(bounds);

    //recursively add objects of child node if bounds is full contained
    if(!mChildren.empty() && index != -1)
    {
        foundObjects = mChildren[index]->retrieve(bounds, searchDepth);
    }
    else
    {
        //add all objects of child nodes which intersect test area
        for(auto& child : mChildren)
        {
            if(bounds.intersects(child->mBounds))
            {
                std::vector<std::shared_ptr<tgd::Entity>> childObjects = child->retrieve(bounds, searchDepth);
                foundObjects.insert(foundObjects.end(), childObjects.begin(), childObjects.end());
            }
        }
    }
    //and append objects in this node
    foundObjects.insert(foundObjects.end(), mObjects.begin(), mObjects.end());

    return foundObjects;
}
Beispiel #4
0
std::vector<xy::Entity> DynamicTreeSystem::query(sf::FloatRect area, std::uint64_t filter) const
{
    Detail::FixedStack<std::int32_t, 256> stack;
    stack.push(m_root);

    std::vector<xy::Entity> retVal;
    retVal.reserve(256);

    while (stack.size() > 0)
    {
        auto treeID = stack.pop();
        if (treeID == TreeNode::Null)
        {
            continue;
        }

        const auto& node = m_nodes[treeID];
        if (area.intersects(node.fatBounds))
        {
            //TODO it would be nice to precache the filter fetch, but it would miss changes at the component level
            if (node.isLeaf() && node.entity.isValid()
                && (node.entity.getComponent<BroadphaseComponent>().m_filterFlags & filter)) 
            {
                //we have a candidate, stash
                retVal.push_back(node.entity);
            }
            else
            {
                stack.push(node.childA);
                stack.push(node.childB);
            }
        }
    }
    return retVal;
}
std::vector<MapObject*> QuadTreeNode::Retrieve(const sf::FloatRect& bounds, sf::Uint16& searchDepth)
{	
	searchDepth = m_level;
	std::vector<MapObject*> foundObjects;
	sf::Int16 index = m_GetIndex(bounds);

	//recursively add objects of child node if bounds is fully contained
	if(!m_children.empty() && index != -1) 
	{
		foundObjects = m_children[index]->Retrieve(bounds, searchDepth);
	}
	else
	{
		//add all objects of child nodes which intersect test area
		for(auto& child : m_children)
		{
			if(bounds.intersects(child->m_bounds))
			{
				std::vector<MapObject*> childObjects = child->Retrieve(bounds, searchDepth);
				foundObjects.insert(foundObjects.end(), childObjects.begin(), childObjects.end());
			}
		}

	}
	//and append objects in this node
	foundObjects.insert(foundObjects.end(), m_objects.begin(), m_objects.end());
	m_debugShape.setOutlineColor(sf::Color::Red);
	return foundObjects;
}
bool CollisionCell::checkCollisionsWithin(std::vector<WorldObject *> *_outputCollisions, sf::FloatRect _bounds)
{
	bool collision = false;

	for (unsigned int i = 0; i < m_TouchingWorldObjects.size(); i += 1)
	{
		if (_bounds.intersects(m_TouchingWorldObjects.at(i)->getBounds()))
		{
			bool present = false;

			for (unsigned int j = 0; j < _outputCollisions->size(); j += 1)
			{
				if (_outputCollisions->at(j) == m_TouchingWorldObjects.at(i))
				{
					present = true;
					break;
				}
			}

			if (!present)
			{
				collision = true;
				_outputCollisions->push_back(m_TouchingWorldObjects.at(i));
			}
		}
	}

	return collision;
}
bool TalkingSprite::intersects(sf::FloatRect rect) const {
    for (auto && it: collisionBoxList) {
        if (rect.intersects(it)) {
            return true;
        }
    }
    return false;
}
Beispiel #8
0
bool Level::boundsCheck(sf::FloatRect& bounds)
{
    for (int i = 0; i < activeTiles.size(); ++i) {
        if (bounds.intersects(activeTiles[i].getGlobalBounds()))
            return true;
    }

    return false;
}
Beispiel #9
0
bool WorldObjects::Intersects(const sf::FloatRect & bounds)
{
	for (int i = 0; i < Count(); ++i)
	{
		if (bounds.intersects(objects[i]->getGlobalBounds()))
			return true;
	}
	return false;
}
Beispiel #10
0
void Quadtree::queryRegion(std::vector<QuadtreeOccupant*> &result, const sf::FloatRect &region) {
	// Query outside root elements
	for (std::unordered_set<QuadtreeOccupant*>::iterator it = _outsideRoot.begin(); it != _outsideRoot.end(); it++) {
		QuadtreeOccupant* oc = *it;
		sf::FloatRect r = oc->getAABB();

		if (oc != nullptr && region.intersects(oc->getAABB()))
			// Intersects, add to list
			result.push_back(oc);
	}

	std::list<QuadtreeNode*> open;

	open.push_back(_pRootNode.get());

	while (!open.empty()) {
		// Depth-first (results in less memory usage), remove objects from open list
		QuadtreeNode* pCurrent = open.back();
		open.pop_back();

		if (region.intersects(pCurrent->_region)) {
			for (std::unordered_set<QuadtreeOccupant*>::iterator it = pCurrent->_occupants.begin(); it != pCurrent->_occupants.end(); it++) {
				QuadtreeOccupant* oc = *it;

				if (oc != nullptr && region.intersects(oc->getAABB()))
					// Visible, add to list
					result.push_back(oc);
			}

			// Add children to open list if they intersect the region
			if (pCurrent->_hasChildren)
			for (int i = 0; i < 4; i++)
			if (pCurrent->_children[i]->getNumOccupantsBelow() != 0)
				open.push_back(pCurrent->_children[i].get());
		}
	}
}
Beispiel #11
0
void MapLayer::cull(sf::FloatRect bounds){
	if (m_type == MapLayerType::TileLayer){
		int tilePatchCount = m_tilePatches.size();
		for (int i = 0; i < tilePatchCount; i++){
			if (bounds.intersects(m_tilePatches[i].aabb))
				m_tilePatches[i].visible = true;
			else
				m_tilePatches[i].visible = false;
		}
		sf::RectangleShape test;
		test.setSize(sf::Vector2f(bounds.width, bounds.height));
		test.setFillColor(sf::Color(255, 0,0, 25));
		test.setOutlineColor(sf::Color::Magenta);
		test.setOutlineThickness(8.f);
		test.setPosition(bounds.left, bounds.top);
		testShapes.back() = test;
	}
}
//private
sf::Vector3f PlayerController::getManifold(const sf::FloatRect& worldRect)
{
    sf::FloatRect overlap;
    sf::FloatRect playerBounds = m_entity->getComponent<lm::CollisionComponent>()->globalBounds();

    //we know we intersect, but we want the overlap
    worldRect.intersects(playerBounds, overlap);
    auto collisionNormal = sf::Vector2f(worldRect.left + (worldRect.width / 2.f), worldRect.top + (worldRect.height / 2.f)) - m_entity->getPosition();

    sf::Vector3f manifold;

    if (overlap.width < overlap.height)
    {
        manifold.x = (collisionNormal.x < 0) ? 1.f : -1.f;
        manifold.z = overlap.width;
    }
    else
    {
        manifold.y = (collisionNormal.y < 0) ? 1.f : -1.f;
        manifold.z = overlap.height;
    }

    return manifold;
}
Beispiel #13
0
void stateUpdate()
{
	while(server.hasGameStarted())
	{
		float elapsedTime = deltaClock.getElapsedTime().asMilliseconds();
		float ballElapsedTime = ballClock.getElapsedTime().asMilliseconds();
		ballSendTimer += elapsedTime;
		ballClock.restart();
		if(elapsedTime >= 16.6)
		{
			deltaClock.restart();
			//Update the ball's position.
			ballRect = sf::FloatRect(ballRect.left + ballVelocity.x, ballRect.top + ballVelocity.y, ballRect.width, ballRect.height);
		}

		//Check for paddle collisions.
		if(ballRect.intersects(onePaddleRect) && ballVelocity.x < 0)
		{
			ballVelocity.x *= (-1.0f);
			std::cout << "Ball rebound off Paddle One." << std::endl;
			ballCollisionDetected = true;
		}
		else if(ballRect.intersects(twoPaddleRect) && ballVelocity.x > 0)
		{
			ballVelocity.x *= (-1.0f);
			std::cout << "Ball rebound off Paddle Two." << std::endl;
			ballCollisionDetected = true;
		}

		checkBounds();

		//Check and manage points for both players.
		if(ballRect.left <= LEFT_WALL + 3 && ballVelocity.x < 0)
		{
			server.sendAll("3 2");
			
			player2Points++;

			std::cout << "Player 2 Score update: " << player2Points << std::endl;

			//Send the end game message if player 2 wins.
			if(player2Points >= MAX_POINTS)
			{
				server.sendAll("4 2");
				
				server.stopGame();

				std::cout << "Player Two win with points." << std::endl;
			}

			ballRect = sf::FloatRect(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, BALL_WIDTH, BALL_HEIGHT);
			ballCollisionDetected = true;
		}
		if(ballRect.left + ballRect.width >= SCREEN_WIDTH - 3 && ballVelocity.x > 0)
		{
			server.sendAll("3 1");

			player1Points++;

			std::cout << "Player 1 Score update: " << player1Points << std::endl;

			//Send the end game message if player 1 wins.
			if(player1Points >= MAX_POINTS)
			{
				server.sendAll("4 1");

				server.stopGame();

				std::cout << "Player One win with points." << std::endl;
			}

			ballRect = sf::FloatRect(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, BALL_WIDTH, BALL_HEIGHT);
			ballCollisionDetected = true;

			std::cout << "Collided" << std::endl;
		}

		if(ballCollisionDetected || ballSendTimer >= (1/60))
		{
			ballSendTimer = 0;
			std::time_t rawtime;
			std::time(&rawtime);

			//Send the ball packet.
			std::stringstream ballPacketStringStream;
			
			ballPacketStringStream << "1 " << ballRect.left << " " << ballRect.top << " " << ballVelocity.x << " " << ballVelocity.y << " " << (rawtime - ((playerOneLag + playerTwoLag)/2));

			server.sendAll(ballPacketStringStream.str());

			ballCollisionDetected = false;
		}
	}
}
Beispiel #14
0
bool AABBCollision(const sf::FloatRect& box1, const sf::FloatRect& box2)
{
    return box1.intersects(box2);
}
Beispiel #15
0
bool Button::intersects(const sf::FloatRect& boundingBox) const
{
    return boundingBox.intersects(getBoundingBox());
}
Beispiel #16
0
bool Game::onCollision(sf::FloatRect rect1, sf::FloatRect rect2)
{
    return rect1.intersects(rect2);
}
Beispiel #17
0
sf::Vector2f MapCollider::tryMove(sf::Vector2f position, sf::Vector2f velocity, sf::FloatRect collisionBox)
{
	sf::Vector2f newPos = position;
	int oldX = newPos.x;
	int oldY = newPos.y;

	//check if box is outside of the screen.
	if (newPos.x  - collisionBox.width / 2 < 0)
	{ 
		newPos.x = collisionBox.width / 2;
	}
	if (newPos.y - collisionBox.height / 2 < 0)
	{
		newPos.y = collisionBox.height / 2;
	}
	if (newPos.x + collisionBox.width / 2 > mMap->getWidth() * tileSize)
	{
		newPos.x = (mMap->getWidth() * tileSize) - collisionBox.width / 2;
	}
	if (newPos.y + collisionBox.height / 2 > mMap->getHeight() * tileSize)
	{
 		newPos.y = (mMap->getHeight() * tileSize) - collisionBox.height / 2;
	}

	collisionBox.left = newPos.x - collisionBox.width / 2;
	collisionBox.top = newPos.y - collisionBox.height / 2;


	for (int iterY = (newPos.y - collisionBox.height / 2) / tileSize; iterY < (newPos.y + collisionBox.height / 2) / tileSize; iterY++)
	{
		for (int iterX = (newPos.x- collisionBox.width / 2) / tileSize; iterX < (newPos.x + collisionBox.width / 2) / tileSize; iterX++)
		{
			//If tile is outside of the collisionMap, ignore that tile
			if (iterX < 0 || iterX >= mMap->getWidth() ||
				iterY < 0 || iterY >= mMap->getHeight())
				continue;

			if ((*mMap)[iterX][iterY].getCollibable() == false)
				continue;

			sf::FloatRect tileBox = getRectFromTile(iterX, iterY);
			sf::FloatRect interRect;

			if (collisionBox.intersects(tileBox, interRect))
			{
				sf::Vector2f distance = distanceRectToRect(tileBox, collisionBox);
				sf::Vector2f major = getMajorVector(distance);

				//this tile is an internal edge if the next tile is also collidable.
				//if this tile is NOT such a tile, then handle collision.
				int nextTileX = iterX + major.x;
				int nextTileY = iterY + major.y;
				if ((*mMap)[nextTileX][nextTileY].getCollibable() == false)
				{
					//do not be tempted to use interRect, it won't move the position excactly right.
					if (major.x < -.5)
					{
						newPos.x = tileBox.left - collisionBox.width / 2;
						collisionBox.left = newPos.x - collisionBox.width / 2;
					}
					else if (major.x > .5)
					{
						newPos.x = tileBox.left + tileBox.width + collisionBox.width / 2;
						collisionBox.left = newPos.x - collisionBox.width / 2;
					}
					else if (major.y < -.5)
					{
						newPos.y = tileBox.top - collisionBox.height / 2;
						collisionBox.top = newPos.y - collisionBox.height / 2;

					}
					else if (major.y > .5)
					{
						newPos.y = tileBox.top + tileBox.height + collisionBox.height / 2;
						collisionBox.top = newPos.y - collisionBox.height / 2;
					}
				}
			
			}
		}
	}

	
	return newPos;
}