c++ - 2d collision detection issue -
i'm working on basic square-square collision detection system checks player , bunch of tiles. problem since works going through tiles starting top left tile (in stage) bottom right tile checks collisions tiles above , left of player before else. because of this, if you're moving left , against wall, stuck because corrects players y position tile above before x position tile left (or right). wondering how can go fixing this. collision code follows:
for(std::vector<tile*>::iterator tile = tiles.begin(); tile != tiles.end(); tile++) { if((*tile)->tiletype == tile::tile_wall) { float cx = character->getposition().x; float cy = character->getposition().y; float chw = character->halfwidth(); float chh = character->halfheight(); float thw = tile::tile_width/2; float thh = tile::tile_height/2; float dx = cx - ((*tile)->getposition().x); float dy = cy - ((*tile)->getposition().y); if(fabsf(dy) < chh + thh) { if(fabsf(dx) < chw + thw) { float ox = chw + thw - fabsf(dx); float oy = chh + thh - fabsf(dy); if(ox > oy) { if(dy > 0) { character->setposition(cx, cy + oy); } else { character->setposition(cx, cy - oy); } } else { if(dx > 0) { character->setposition(cx + ox, cy); } else { character->setposition(cx - ox, cy); } } } } } }
also, optimization appreciated.
Comments
Post a Comment