[Help] map.erase();

Hi ! I've a std::map<std::string,Obj*> which contains an ObjA, an ObjB and many ObjC.
I made a function which manages collision between ObjB and ObjC (it works) !
but my function which is used for delete my ObjC when they collide with my ObjB didn't :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 //ManagerObj.cpp

void ManagerObj::deleteObj(std::map<std::string, Blocks*>::iterator i)
{
        m_VecMap.erase(i);
}

void ManagerObj::updateAll()
{
        for (std::map<std::string, Obj*>::iterator i = m_VecMap.begin(); i != m_VecMap.end(); ++i)
        {
        if ((i->first == "ObjC") && (i->second->checkCollision() == true)) delete(i);
                i->second->update(); // used for ObjB and ObjC collide and they do
        }
}


1
2
3
4
5
6
7
8
9
10
11
12
//main.cpp

        ManagerObj mapManager;
        mapManager.addObj("ObjA", p_ObjA);
        mapManager.addObj("ObjB", p_ObjB);
        mapManager.addObj("ObjC", p_ObjC);

//...

        mapManager.updateAll();

//... 



Can you help me to erase my ObjC from mapManager when it collides with ObjB ? Thanks you !
In fact my function collision() doesn't work ^^
1
2
3
4
5
6
7
8

bool Blocks::checkCollision()
{

	double circle = sqrt((m_Rectangle.getPosition().x - m_Circle.getPosition().x)*(m_Rectangle.getPosition().x - m_Circle.getPosition().x) + (m_Rectangle.getPosition().y - m_Circle.getPosition().y)*(m_Rectangle.getPosition().y - m_Circle.getPosition().y));
	
        if (circle <= m_Circle.getRadius()) return true;
}



(I just realize that my function is truly bad, help me please)
Last edited on
I think you want deleteObj(i) in updateAll, not delete(i)

As for the collision, you can use the getGlobalBounds method of the shapes and then check for intersection on the rects returned by getGlobalBounds.
1
2
3
4
5
6
bool Blocks::checkCollision()
{
     if(m_Rectangle.getGlobalBounds().intersects(m_Circle.getGlobalBounds()))
         return true;
     return false;
}
Last edited on
Thnaks for your answer, yes it's deleteObj, I failed my copy. But it still doesn't work :)
Can you show more code?

This is dangerous
1
2
3
if ((i->first == "ObjC") && (i->second->checkCollision() == true)) delete(i);
                i->second->update(); // used for ObjB and ObjC collide and they do
        }
after you erase the iterator you dereference it.
Last edited on
Thanks for your edit, i didn't see. Yes wait 2 min please i'm trying your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 //ManagerObj.cpp
ManagerObj::ManagerObj()
{
}


ManagerObj::~ManagerObj()
{
}

void ManagerObj::addObj(std::string nameObj, Blocks* BlockObj)
{

	m_VecMap.insert(std::pair<std::string, Blocks*>(nameObj, BlockObj));

}
void ManagerObj::deleteObj(std::map<std::string, Blocks*>::iterator i)
{
		m_VecMap.erase(i);
}

void ManagerObj::drawAll(sf::RenderWindow & window)
{
	for (std::multimap<std::string, Blocks*>::const_iterator i = m_VecMap.begin(); i != m_VecMap.end(); ++i)
		i->second->Draw(window);
}
void ManagerObj::updateAll(sf::RenderWindow & window)
{
	for (std::map<std::string, Blocks*>::iterator i = m_VecMap.begin(); i != m_VecMap.end(); ++i)
	{
		if ((i->first == "Brick") && (i->second->checkCollision() == true)){ deleteObj(i); }
	
			i->second->update();
			
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 //Blocks.h
class Blocks
{
protected :
	sf::RectangleShape m_Rectangle;
	sf::CircleShape m_Circle;
	sf::Vector2f m_Pos;



public:
	Blocks();
	virtual ~Blocks();
	virtual void Draw(sf::RenderWindow & window) = 0;
	virtual void update() = 0;
	virtual sf::Vector2f getPosition()= 0;
	virtual bool checkCollision() ;
	virtual bool Blocks::checkWindowCollision(sf::RenderWindow & window); // not writted yet
	

};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 //Paddle.cpp derived from Blocks
Paddle::Paddle()
	: m_velocity(0), m_maxVelocity(1.0f)
{
	loadRectangle();
}


Paddle::~Paddle()
{
}

sf::RectangleShape Paddle::loadRectangle()
{

	m_Rectangle.setFillColor(sf::Color(50, 50, 50));
	m_Rectangle.setSize(sf::Vector2f(150,30));
	return m_Rectangle;
}

void Paddle::setPosition(sf::Vector2f & pos)
{
	m_Pos;
	m_Rectangle.setPosition(pos);
}

void Paddle::Draw(sf::RenderWindow & window)
{
	return window.draw(m_Rectangle);
}

void Paddle::update()
{
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
	{
		m_Rectangle.move(-15, 0);
	}
	else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
	{
		m_Rectangle.move(15, 0);
	}
}

sf::Vector2f Paddle::getPosition()
{
	return m_Pos;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Bricks.cpp derived from Blocks
Bricks::Bricks()
{
	loadRectangle();
	
}


Bricks::~Bricks()
{
}

void Bricks::setPosition(sf::Vector2f & pos)
{
	m_Pos;
	m_Rectangle.setPosition(pos);
}

sf::RectangleShape Bricks::loadRectangle()
{
	m_Rectangle.setSize(sf::Vector2f(150,50));
	m_Rectangle.setFillColor(sf::Color(50, 0, 150));
	
	return m_Rectangle;
}
void Bricks::Draw(sf::RenderWindow & window)
{
	return window.draw(m_Rectangle);
}
void Bricks::update()
{

}

sf::Vector2f Bricks::getPosition()
{
	return m_Pos;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Ball.cpp derived from Blocks
Balle::Balle()
	: m_Move(0,-1)
{
	loadShape();
}


Balle::~Balle()
{
}

void Balle::setPosition(sf::Vector2f & pos)
{
	m_Pos;
	m_Circle.setPosition(pos);
}

sf::CircleShape Balle::loadShape()
{
	m_Circle.setRadius(100);
	m_Circle.setFillColor(sf::Color(100, 250, 50));
	return m_Circle;
}
void Balle::Draw(sf::RenderWindow & window)
{
	return window.draw(m_Circle);
}
void Balle::update()
{
	
	if (checkCollision() == true)
	{
		m_Move.x = m_Move.x*-1;
		m_Move.y = m_Move.y*-1;
		m_Circle.move(m_Move);
	}
	else
	{
		m_Circle.move(m_Move);
	}
	
}

sf::Vector2f Balle::getPosition()
{
	return m_Pos;
}




"after you erase the iterator you dereference it."

I don't know how to do it, i'm a newbie

(if there is some mistake of syntaxe it's maybe because I did a translation for post it)
Last edited on
Topic archived. No new replies allowed.