trying to get collision with bullets

trying to get my bullet's to hit my player, had a friend come over and he made a iterator thing out of my bullet's fire function and now when ever I press the fire button for my player my game crashes and the error message is "Unhandled exception at 0x77a215de in GL_Template_d.exe: 0xC0000005: Access violation reading location 0xcdcdce01."


bullet update function:

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
	if ( keysArray[ SDLK_LCTRL ] == true && m_iBulletAmount < 2000)
	{
		m_iBulletAmount++;
		Bullet* pBullet = new Bullet("./Images/Blast_1.png", NULL);
		Matrix3 TempM3 = GetWorldTransform();
		pBullet->SetPosRot(TempM3);
		m_pLBullets.push_back(pBullet);
	}

	for( m_BulletITR = m_pLBullets.begin(); m_BulletITR != m_pLBullets.end(); m_BulletITR++ )
	{
		Bullet* pBullet = (*m_BulletITR);
		pBullet->Update();

		pBullet->GetBulletPos();

		//if bullet not on screen remove from list;
		if(  pBullet->IsAlive() )
		{	
			m_iBulletAmount--;
			delete pBullet;
			m_BulletITR = m_pLBullets.erase(m_BulletITR);
		}
	}
}
	


collision code for walls and player.
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
	if( m_transform.GetTranslations().x > 1024  || 
		m_transform.GetTranslations().x < 0	    ||
		m_transform.GetTranslations().y < 0	    ||
		m_transform.GetTranslations().y > 768	 )
	{
		m_bFired = false;
	}	
	//if(m_bFired = m_bHit)
	//{
	//   
	//}

	//ow this isn't where i parked my tank!
	if(m_transform.GetTranslations().x > m_pPlayer->GetPlayerPos().x)
	{	
		m_pPlayer->ResetPos();
	;}
	if(m_transform.GetTranslations().x < m_pPlayer->GetPlayerPos().x)
	{	
		m_pPlayer->ResetPos();
	;}
	if(m_transform.GetTranslations().y > m_pPlayer->GetPlayerPos().y)
	{	
		m_pPlayer->ResetPos();
	;}
	if(m_transform.GetTranslations().y < m_pPlayer->GetPlayerPos().y)
	{	
		m_pPlayer->ResetPos();
	;}
A: You're not destroying the bullet if it's not on the screen. You're destroying it if it's alive (whatever that means for a bullet.)

B. m_plBullets.erase(M_BulletItr) can return m_plBullets.end() in which case the for loop attempts to increment it before comparing it to end, resulting in undefined behavior.

Should look something like the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    auto i = m_plBullets.begin() ;

    while ( i != m_plBullets.end() )
    {
        (*i)->Update() ;

        if ( (*i)->isAlive() )
        {
            --m_iBulletAmount ;  // why keep this variable?  Just use m_plBullets.size()
            delete *i ;
            i = m_plBullets.erase(i) ;
        }
        else
            ++i ;
    }

Last edited on
cool thanks for that it's not breaking any more =D
Topic archived. No new replies allowed.