   Removing bullets after they hit?
  I want to remove the bullets which are the triangles after they hit the castle mesh in this video, when they hit the game turns to wireframe but i want todo i = Bs.erase(i); , because i only do that after a certain amount of time has alasped , so if i try this on the triangle collide with castle code it just crashs?, does anyone know how to solve this?heres the code on how i do my collisionvoid PropsDemo::Bullets(){if(gCamera->fire){//MessageBox(NULL,"Fire","",0);BulletStruct bs;bs.Dir = gCamera->mLookW;bs.FPos = gCamera->mPosW; //position of cambs.life = 0.0f;bs.BulletTime = mTime;Bs.push_back(bs);gCamera->fire = false; //reset}std::list<BulletStruct>::iterator i = Bs.begin();while( i != Bs.end() ){sprintf(Buffer,"%s %d\n","Bs.Size()",Bs.size());//WriteConsole(Console,Buffer,strlen(Buffer),&Written,0);i->life += mTime;if(i->life >= 1000000.0f){//MessageBox(NULL,"Lived for 10 seconds","",0);//i->Bullet->Release();    i = Bs.erase(i);}else{D3DXMatrixTranslation(&i->Pos,   i->FPos.x + i->Dir.x*(mTime-i->BulletTime)*10.1f,    i->FPos.y +i->Dir.y*(mTime-i->BulletTime)*10.1f,  i->FPos.z +  i->Dir.z*(mTime-i->BulletTime)*10.1f);    gd3dDevice->SetTransform(D3DTS_WORLD,&i->Pos);//i->FPos.y -= 0.1f;sprintf(Buffer,"%s %f\n","i->Pos._43",i->Pos._43);//WriteConsole(Console,Buffer,strlen(Buffer),&Written,0);MadeEffect->Begin(NULL, NULL);    // begin using the effect   MadeEffect->BeginPass(0);    // begin the passstatic float col2 = 0.0f; col2 += 1.01f;   D3DXHANDLE cHandle2= MadeEffect->GetParameterByName(0, "color");   MadeEffect->SetFloat(cHandle2,col2);MadeEffect->SetMatrix("View",&gCamera->mView);   MadeEffect->SetMatrix("Projection",&gCamera->mProj);   MadeEffect->SetMatrix("World",&(i->Pos));gd3dDevice->SetFVF(CUSTOMFVF);gd3dDevice->SetStreamSource(0,  LineBuffer, 0, sizeof(CUSTOMVERTEX3D));//gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);gd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);//gd3dDevice->DrawPrimitive(D3DPT_POINTLIST, 0, 1);MadeEffect->EndPass();    // end the passMadeEffect->End();    // end the effect//Transform the raypos and raydir to the same space as the castle..D3DXMATRIX ObjCastle;D3DXMatrixInverse(&ObjCastle,NULL,&mCastleWorld);D3DXVECTOR3 RayPos(i->Pos._41,i->Pos._42,i->Pos._43);D3DXVECTOR3 RayDir(i->Dir.x,i->Dir.y,i->Dir.z);D3DXVECTOR3 ObjRayPos;D3DXVECTOR3 ObjDir;D3DXVec3TransformCoord(&ObjRayPos,&RayPos,&ObjCastle);D3DXVec3TransformNormal(&ObjDir,&RayDir,&ObjCastle);D3DXVec3Normalize(&ObjDir,&ObjDir);D3DVIEWPORT9 View9;gd3dDevice->GetViewport(&View9);D3DXVECTOR3 Out;D3DXVec3Project(&Out,&RayPos,&View9,&gCamera->mProj,&gCamera->mView,&i->Pos);//SetCursorPos((int)Out.x,(int)Out.y);BOOL Hit;DWORD Face;float Dist;static int HitCount = 0;D3DXIntersect(mCastle.mesh,&ObjRayPos,&ObjDir,&Hit,&Face,NULL,NULL,&Dist,NULL,NULL);if(Hit){//gCamera->mPosW = RayPos+gCamera->mLookW;if(Dist < 1.0f){   HitCount += 1;SetRect(&textbox, 100, 100, 800, 600); sprintf(Buffer,"%s %d %s %d\n","Face",Face,"HitCount",HitCount);// draw the Hello World text    dxfont->DrawTextA(NULL,Buffer,  strlen(Buffer),  &textbox,  DT_CENTER | DT_VCENTER,  D3DCOLOR_ARGB(255, 255, 0, 0));//MessageBox(NULL,"Hit Close","",0);gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);//i = Bs.erase(i);//pushback these ObjRayPos to a new list....//EffectStruct es;//es.Pos = i->Pos;//Es.push_back(es);}else{gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);}}else{gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);}i++; //increment}}}}[Edited by - Anddos on June 6, 2010 8:31:19 PM]
  We need to see the part of loop where you're creating i from Bs and incrementing it.   ;  ok will change the code..   ;  Is the problem that it crashes if you un-comment the //i = Bs.erase(i); line?It look like if that line is un-commented, then it's possible to erase an item and still run i++ in the one iteration. Most of the time this means you'd just be accidentally skipping over one item, but if you've just erased the last element in the list, then i==Bs.end(), which means i++ will crash.   ;Quote:Original post by HodgmanIs the problem that it crashes if you un-comment the //i = Bs.erase(i); line?yes, do you know a work around as i need to destory the triangles when the hit , not when time is up..   ;  What I said above: don't increment and erase in the same iteration.i = Bs.erase(i);}else{gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);++i; //ADDED}}else{gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);++i; //ADDED}//UNDONE: i++; //increment;  Thanks thats exactly what i wanted :)Next i want the bullets to intersect with the barricades i make so i am doing a loop likewhile( i != Bs.end() ) //this is within the while bullet loop...as i need to check through each barricade etc..{//do the ray stuff , but it seems to be crashing,}   ;  What do you do inside that loop, and which line does it crash on?   ;  This is the working bullets code , now i also have another list which is used to render the baricades you can place down..., what i want todo now is loop the amount of baricades layed down and test for intersect with each 1..,i am unsure where to put it in this code i can access the iterator of the baricade from here to...//Build the Ray position and Ray Directon...TriRayPos.x = ii->Pos._41;TriRayPos.y = ii->Pos._42;TriRayPos.z = ii->Pos._43;TriRayDir.x = gCamera->mLookW.x;TriRayDir.y = gCamera->mLookW.y;TriRayDir.z = gCamera->mLookW.z;//If Bullets Hit Castle...D3DXMatrixInverse(&ObjCastle,NULL,&mCastleWorld); //mCastleWorldD3DXVec3TransformCoord(&TriObjectRayPos,&TriRayPos,&ObjCastle);D3DXVec3TransformNormal(&TriObjectRayDir,&TriRayDir,&ObjCastle);D3DXVec3Normalize(&TriObjectRayDir,&TriObjectRayDir);D3DXIntersect(mCastle.mesh,&TriObjectRayPos,&TriObjectRayDir,&TriHit,&Face,NULL,NULL,&TriDist,NULL,NULL);if(TriHit){////gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);sprintf(Buffer,"%s %f\n","TriDist",TriDist);WriteConsole(Console,Buffer,strlen(Buffer),&Written,0);if(TriDist < 0.5f){//gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);ii = TriStr.erase(ii);}else{//gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);ii++;}}else{ii++;}}}}This is the Baricade codevoid PropsDemo::Bullets() //ignore the name , its from old code..{CamRayPos = gCamera->mPosW;CamRayDir = gCamera->mLookW;if(PlaceBaricade){mGfxStats->addVertices(tRex.mesh->GetNumVertices());mGfxStats->addTriangles(tRex.mesh->GetNumFaces());//MessageBox(NULL,"Fire","",0);BulletStruct bs; //ignore why the struct is called bullet , its from older code ;)//bs.Dir = gCamera->mLookW;//bs.FPos.z += 15.0f; //infront of cam?bs.FPos = gCamera->mPosW + gCamera->mLookW; //position of ship//bs.FPos.y = 3.0f; //trying to set hieght of terrainbs.FPos.z += 3.0f;bs.life = 0.0f;bs.BulletTime = mTime;Bs.push_back(bs);PlaceBaricade = false; //reset}//std::list<BulletStruct>::iteratori = Bs.begin();while( i != Bs.end() ){sprintf(Buffer,"%s %d\n","Bs.Size()",Bs.size());WriteConsole(Console,Buffer,strlen(Buffer),&Written,0);sprintf(Buffer,"%s %f %f %f\n","i->pos",i->Pos._41,i->Pos._42,i->Pos._43);WriteConsole(Console,Buffer,strlen(Buffer),&Written,0);i->life += mTime;if(i->life >= 100000.0f){//MessageBox(NULL,"Lived for 10 seconds","",0);//i->Bullet->Release();    i->life = 0.0f;i = Bs.erase(i);//mGfxStats->mNumTris -= tRex.mesh->GetNumFaces();//mGfxStats->mNumVertices -= tRex.mesh->GetNumVertices();}else{D3DXMatrixTranslation(&i->Pos,   i->FPos.x,    i->FPos.y + -2.0f,  i->FPos.z + 2.0f);    gd3dDevice->SetTransform(D3DTS_WORLD,&i->Pos);drawObject(tRex, (i->Pos));D3DXMatrixInverse(&ObjCastle,NULL,&i->Pos);D3DXVec3TransformCoord(&ObjectRayPos,&CamRayPos,&ObjCastle);D3DXVec3TransformNormal(&ObjectRayDir,&CamRayDir,&ObjCastle);D3DXVec3Normalize(&ObjectRayDir,&ObjectRayDir);D3DXIntersect(tRex.mesh,&ObjectRayPos,&ObjectRayDir,&Hit,&Face,NULL,NULL,&Dist,NULL,NULL);if(Hit){//if(Dist < 1.0f){//gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);gCamera->close = true;}else{//gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);gCamera->close = false;}}i++; //increment}}}[Edited by - Anddos on June 10, 2010 9:05:47 PM]