To detect collision I'm using a rectangle for each entity in the game, but Im having trouble reacting to it in an appropiate way. What I was trying to do was find the smallest distance for the player to get out of the wall and then move it out, but I get disastrous results, here is the code:
What's a good way to react to collision in a game like this?Code:void Samus::collided(Entity *him) { int x1_x1 = m_rect.x1 - him->getRect().x1; // distance from my x1 to his x1 int x1_x2 = m_rect.x1 - him->getRect().x2; // distance from my x1 to his x2 int y1_y1 = m_rect.y1 - him->getRect().y1; // distance from my y1 to his y1 int y1_y2 = m_rect.y1 - him->getRect().y2; // distance from my y1 to his y2 if (x1_x1 < 0) x1_x1 *= -1; if (x1_x2 < 0) x1_x2 *= -1; if (y1_y1 < 0) y1_y1 *= -1; if (y1_y2 < 0) y1_y2 *= -1; switch (him->getInst()) { case inst_floor: if (y1_y1 < y1_y2 && y1_y1 < x1_x1 && y1_y1 < x1_x2) { m_y = him->getRect().y1 - height; return; } if (y1_y2 < y1_y1 && y1_y2 < x1_x1 && y1_y2 < x1_x2) { m_y = him->getRect().y2; return; } if (x1_x1 < x1_x2 && x1_x1 < y1_y1 && x1_x1 < y1_y2) { m_x = him->getRect().x1 - width; return; } if (x1_x2 < x1_x1 && x1_x2 < y1_y1 && x1_x2 < y1_y2) { m_x = him->getRect().x2; return; } break; case inst_entity: break; case inst_samus: break; } }
Here is the entire project if anyone is interested (Uses SDL)
http://files.upl.silentwhisper.net/u...descroller.zip
Sprites by Rogultgot.



LinkBack URL
About LinkBacks


