Hey.
I use this as my collision checking and it works but I guess in the long run it will be rather slow with hundreds of objects. I've searched the internet at times to improve this but I'll ask here too for opinions.
How would one improve such code, can someone give me some pointers/directions what I should be looking at to reduce the amount of iterations?
///////////////////////////////////
//// COLLISION
it->SetX(it->GetX() + it->GetVelX());
if(it->CheckMapCollision())
it->isColliding = true;
float tx = ((it->GetX()) / (TILE_SIZE_W / 2.0f) + ((it->GetY()) / (TILE_SIZE_H / 2.0f))) / 2.0f;
float ty = ((it->GetY()) / (TILE_SIZE_H / 2.0f) - ((it->GetX()) / (TILE_SIZE_W / 2.0f))) / 2.0f;
int hx = tx * TILE_SIZE_H - it->GetColW() / 2;
int hy = ty * TILE_SIZE_H - it->GetColH() / 2;
for(std::vector<CProp>::iterator nit=Prop.begin();nit!=Prop.end();++nit)
{
float ntx = ((nit->GetX()) / (TILE_SIZE_W / 2.0f) + ((nit->GetY()) / (TILE_SIZE_H / 2.0f))) / 2.0f;
float nty = ((nit->GetY()) / (TILE_SIZE_H / 2.0f) - ((nit->GetX()) / (TILE_SIZE_W / 2.0f))) / 2.0f;
int nhx = ntx * TILE_SIZE_H - nit->GetColW() / 2;
int nhy = nty * TILE_SIZE_H - nit->GetColH() / 2;
if(it->Collide(hx,hy,it->GetColW(),it->GetColH(),nhx,nhy,nit->GetColW(),nit->GetColH()))
{
it->isColliding = true;
}
}
if(it->isColliding)
{
it->SetX(it->GetX() - it->GetVelX());
}
it->SetVelX(0);
///////////////////////////////////
it->isColliding = false;
///////////////////////////////////
it->SetY(it->GetY() + it->GetVelY());
if(it->CheckMapCollision())
it->isColliding = true;
tx = ((it->GetX()) / (TILE_SIZE_W / 2.0f) + ((it->GetY()) / (TILE_SIZE_H / 2.0f))) / 2.0f;
ty = ((it->GetY()) / (TILE_SIZE_H / 2.0f) - ((it->GetX()) / (TILE_SIZE_W / 2.0f))) / 2.0f;
hx = tx * TILE_SIZE_H - it->GetColW() / 2;
hy = ty * TILE_SIZE_H - it->GetColH() / 2;
for(std::vector<CProp>::iterator nit=Prop.begin();nit!=Prop.end();++nit)
{
float ntx = ((nit->GetX()) / (TILE_SIZE_W / 2.0f) + ((nit->GetY()) / (TILE_SIZE_H / 2.0f))) / 2.0f;
float nty = ((nit->GetY()) / (TILE_SIZE_H / 2.0f) - ((nit->GetX()) / (TILE_SIZE_W / 2.0f))) / 2.0f;
int nhx = ntx * TILE_SIZE_H - nit->GetColW() / 2;
int nhy = nty * TILE_SIZE_H - nit->GetColH() / 2;
if(it->Collide(hx,hy,it->GetColW(),it->GetColH(),nhx,nhy,nit->GetColW(),nit->GetColH()))
{
it->isColliding = true;
}
}
if(it->isColliding)
{
it->SetY(it->GetY() - it->GetVelY());
}
it->SetVelY(0);
//// COLLISION
///////////////////////////////////
Cheers