void QuadTreeNode::Insert(const MapObject& object)
{	
	//check if an object falls completely outside a node
	if(!object.GetAABB().intersects(m_bounds)) return;
	
	//if node is already split add object to corresponding child node
	//if it fits
	if(!m_children.empty())
	{
		sf::Int16 index = m_GetIndex(object.GetAABB());
		if(index != -1)
		{
			m_children[index]->Insert(object);
			return;
		}
	}
	//else add object to this node
	m_objects.push_back(const_cast<MapObject*>(&object));


	//check number of objects in this node, and split if necessary
	//adding any objects that fit to the new child node
	if(m_objects.size() > MAX_OBJECTS && m_level < MAX_LEVELS)
	{
		//split if there are no child nodes
		if(m_children.empty()) m_Split();

		sf::Uint16 i = 0;
		while(i < m_objects.size())
		{
			sf::Int16 index = m_GetIndex(m_objects[i]->GetAABB());
			if(index != -1)
			{
				m_children[index]->Insert(*m_objects[i]);
				m_objects.erase(m_objects.begin() + i);
			}
			else
			{
				i++; //we only increment i when not erasing, because erasing moves
				//everything up one index inside the vector
			}
		}
	}
}