bool Entity::IsCollidingWith(Entity& e) { // Ex006.4: Generic Entity Collision routine. // Ex006.4: Does this object collide with the e object? // Ex006.4: Create a circle for each entity (this and e). // Ex006.4: Check for intersection. // Ex006.4: Using circle-vs-circle collision detection. // Ex006.4: Return result of collision. int x1 = e.GetPositionX() + 8; int y1 = e.GetPositionY() + 8; int radius1 = 16; int radius2 = 16; //compare the distance to combined radii if (sqrt((x1 - m_x) * (x1 - m_x) + (y1 - m_y) * (y1 - m_y)) < 32) { return true; //Console.WriteLine("The 2 circles are colliding!"); } //if (sqrt(pow((x1 - m_x), 2) + pow((y1 - m_y), 2) < 32)) //sqrt((x2 – x1) ^ 2 + (y2 – y1) ^ 2) – r1 – r2 float t = sqrt(pow((x1 - m_x), 2) + pow((y1 - m_x), 2) - 1 - 1); if (t <= 0) { //return true; } return (false); // Ex006.4 Change return value! }
bool Wall::IsCollidingWith(Entity& e, bool lockdown) { bool result = false; int eWidth = e.getWidth(); int eHeight = e.getHeight() / 2; int entityX = e.GetPositionX(); int entityY = e.GetPositionY() + eHeight; if (wa_xloc + wa_width <= entityX || wa_xloc >= entityX + eWidth){ return false; } if (wa_yloc + wa_height <= entityY || wa_yloc >= entityY + eHeight){ return false; } if (wa_side == TOP ){ if (wa_door && wa_open && !lockdown){ e.SetPositionX(wa_xloc + 10); } else{ if (wa_yloc + (wa_height - 50) <= entityY && wa_yloc + wa_height >= entityY){ //done //south, blocks moving up e.SetPositionY(wa_yloc + 41); result = true; } else if (wa_xloc <= entityX + eWidth && wa_xloc + 50 >= entityX + eWidth){ //done //west, blocks moveing right e.SetPositionX(wa_xloc - (eWidth + 1)); result = true; } else if (wa_xloc + (wa_width - 50) <= entityX && wa_xloc + wa_width >= entityX){ //done //east, blocks moving left e.SetPositionX(wa_xloc + 151); result = true; } } } else if (wa_side == BOTTOM){ if (wa_door && wa_open && !lockdown){ e.SetPositionX(wa_xloc + 10); } else{ if (wa_yloc <= entityY + eHeight && wa_yloc + 50 >= entityY + eHeight){ //north, blocks moving down e.SetPositionY(wa_yloc - ((eHeight * 2) + 1)); result = true; } else if (wa_xloc <= entityX + eWidth && wa_xloc + 50 >= entityX + eWidth){ //done //west, blocks moveing right e.SetPositionX(wa_xloc - (eWidth + 1)); result = true; } else if (wa_xloc + (wa_width - 50) <= entityX && wa_xloc + wa_width >= entityX){ //done //east, blocks moving left e.SetPositionX(wa_xloc + 151); result = true; } } } else if (wa_side == LEFT){ if (wa_door && wa_open && !lockdown){ e.SetPositionY(wa_yloc - 10); //result = true; } else{ if (wa_xloc + (wa_width - 50) <= entityX && wa_xloc + wa_width >= entityX){ //done //east, blocks moving left e.SetPositionX(wa_xloc + 151); result = true; } else if (wa_yloc <= entityY + eHeight && wa_yloc + 50 >= entityY + eHeight){ //north, blocks moving down e.SetPositionY(wa_yloc - ((eHeight * 2) + 1)); result = true; } else if (wa_yloc + (wa_height - 50) <= entityY && wa_yloc + wa_height >= entityY){ //done //south, blocks moving up e.SetPositionY(wa_yloc + 41); result = true; } } } else if (wa_side == RIGHT){ if (wa_door && wa_open && !lockdown){ e.SetPositionY(wa_yloc - 10); //result = true; } else{ if (wa_xloc <= entityX + eWidth && wa_xloc + 50 >= entityX + eWidth){ //done //west, blocks moveing right e.SetPositionX(wa_xloc - (eWidth + 1)); result = true; } else if (wa_yloc <= entityY + eHeight && wa_yloc + 50 >= entityY + eHeight){ //north, blocks moving down e.SetPositionY(wa_yloc - ((eHeight * 2) + 1)); result = true; } else if (wa_yloc + (wa_height - 50) <= entityY && wa_yloc + wa_height >= entityY){ //done //south, blocks moving up e.SetPositionY(wa_yloc + 41); result = true; } } } return result; }