//============================================================================= // Rotated Box collision detection method // Called by collision() // Post: returns true if collision, false otherwise // sets collisionVector if collision // Uses Separating Axis Test to detect collision. // The separating axis test: // Two boxes are not colliding if their projections onto a line do not overlap. //============================================================================= bool Entity::collideRotatedBox(Entity &ent, VECTOR2 &collisionVector) { computeRotatedBox(); // prepare rotated box ent.computeRotatedBox(); // prepare rotated box if (projectionsOverlap(ent, collisionVector) && ent.projectionsOverlap(*this, collisionVector)) return true; return false; }
//============================================================================= // Rotated Box collision detection method // Called by collision() // Uses Separating Axis Test to detect collision. // The separating axis test: // Two boxes are not colliding if their projections onto a line do not overlap. // The current entity is A the other entity is B // Post: returns true if collision, false otherwise // sets collisionVector if collision //============================================================================= bool Entity::collideRotatedBox(Entity &entB, VECTOR2 &collisionVector) { float overlap01, overlap03; computeRotatedBox(); // prepare rotated box entB.computeRotatedBox(); // prepare rotated box if (projectionsOverlap(entB, collisionVector) && entB.projectionsOverlap(*this, collisionVector)) { // If we get to here the entities are colliding. The edge with the // smallest overlapping section is the edge where the collision is // occuring. The collision vector is created perpendicular to the // collision edge. The projection edges are 01 and 03. // // entA01min // / entB01min // / / entA01max // / / / entB01max // / / / / // 0--------------------1 // entB03min..| ____ // entA03min..| _____|_ B | // | | A | | | // entA03max..| |_____|_| | // entB03max..| |____| // | // | // 3 // if (entA01min < entB01min) // if A left of B { overlap01 = entA01max - entB01min; collisionVector = corners[1] - corners[0]; } else // else, A right of B { overlap01 = entB01max - entA01min; collisionVector = corners[0] - corners[1]; } if (entA03min < entB03min) // if A above B { overlap03 = entA03max - entB03min; if (overlap03 < overlap01) collisionVector = corners[3] - corners[0]; } else // else, A below B { overlap03 = entB03max - entA03min; if (overlap03 < overlap01) collisionVector = corners[0] - corners[3]; } return true; } return false; }
//============================================================================= // Rotated Box collision detection method // Called by collision() // Post: returns true if collision, false otherwise // sets collisionVector if collision // Uses Separating Axis Test to detect collision. // The separating axis test: // Two boxes are not colliding if their projections onto a line do not overlap. //============================================================================= bool Entity::collideRotatedBox(Entity &ent, VECTOR2 &collisionVector) { computeRotatedBox(); // prepare rotated box ent.computeRotatedBox(); // prepare rotated box if (projectionsOverlap(ent) && ent.projectionsOverlap(*this)) { // set collision vector collisionVector = *ent.getCenter() - *getCenter(); return true; } return false; }